Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

servlet doGet and doPost methods [duplicate]

I want to know that in servlets why we use doGet and doPost methods together in the same program. What is the use of it??

What does the following code means?
Why to call the doGet method from doPost? I am not at all clear about this code.

public class Info extends HttpServlet
{

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

 }


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

Thanks

like image 376
user460920 Avatar asked Feb 05 '12 10:02

user460920


2 Answers

doGet() handles incoming HTTP GET requests while doPost() handles... POST requests. There are also equivalent methods to handle PUT, DELTE, etc.

If you submit your form using GET (default), the doGet() will be called. If you submit using POST, doPost() will be invoked this time. If you only implement doPost() but the form will use GET, servlet container will throw an exception.

In many programs the server doesn't care whether the request uses GET or POST, that's why one method simply delegates to another. This is actually a bad practice since these methods are inherently different, but many tutorials write it this way (for better or worse).

like image 196
Tomasz Nurkiewicz Avatar answered Oct 06 '22 21:10

Tomasz Nurkiewicz


Simply, it is to make the servlet generalized so that even if we change the request method in future, it is not required to edit the servlet, which will reduce the efforts to modify the application in future.

like image 42
Anand Avatar answered Oct 06 '22 22:10

Anand