Search code examples
javapostservletsgethttp-method

Why someone calls post() method inside get() method in Servlets?


why we call post() method inside get() method in servlets?

public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

      doGet(request, response);
   }

Solution

  • Simply only because someone wants to have the same behavior disregarding the HTTP method whether it was POST or GET. So requesting resource with POST does the same as GET.

    BUT: doing this - doing the same action - is quite propably wrong. Someone who does this might do it for convenience - for example wants to provide more means to access resource but does not fully understand the difference of GET vs. POST.

    It is a matter of idempotency. Good explanation here.

    In a nutshell GET should be used when GETting stuff and POSTing when you need to change stuff on the server side.

    But what I have experienced some people use GET as long as there too much data for GET and then switch to POST without further thinking about the real difference.