7

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

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
user460920
  • 887
  • 5
  • 17
  • 27

5 Answers5

20

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).

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • When would you actually want to distinguish between get and post in the servlet? What kind of use case (or requirement) would that be? –  Feb 05 '12 at 10:19
  • 4
    GET is supposed to get a resource. It must be idempotent and is not supposed to modify anything in the server. POST is not idempotent, and is used to create, update or delete something on the server. There's no reason to use a POST when a GET must be used, and using a GET when a POST should be used is bad practice and can cause all sorts of problems if the user refreshes the page or navigates through history. It's common to use GET to display a form, and POST to submit it. – JB Nizet Feb 05 '12 at 10:23
  • `doGet()` for `/users` returns a list, `doPost()` for the same URL (servlet) creates new user. Of course I can delegate both methods into a single one and then use [`HttpServletRequest.getMethod()`](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getMethod()) but it only complicates matters. – Tomasz Nurkiewicz Feb 05 '12 at 10:25
  • Thanks everyone..for your answers..i am clear about the simple difference between the get and post but was not clear about the above code which uses both the methods in one servlet code.. – user460920 Feb 05 '12 at 10:35
  • Ok..so if i write the "method=get" in html and for servlet i use the "doPost()" or vice-versa then will it do..OR i have to use same methods for servlet as well as the html forms.. – user460920 Feb 05 '12 at 10:38
  • 1
    @user460920: 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. – Tomasz Nurkiewicz Feb 05 '12 at 10:41
3

This is to handle both requests type eg. GET and POST of http. depending upon app's requirement people may chose to keep request type as GET or POST so incase you are handling both of them you will get error. and in case you want to handle both of them in similar fashion then you can create another method doSomething and call it from your doGet and doPost methods for more info see this answer

Community
  • 1
  • 1
3

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.

Anand
  • 62
  • 2
1

Isn't it to do with a get request allows the parameters to be seen in the URL in the browser window and the post request incorporation the parameters into the structure of the request and hence hidden from view. How will your request be made from the client as a get or a post. I think it is something to do with security and avoiding sql injections, but it is not my area really. Hopefully, some expert with correct my view/comment as I need to know this myself.

G O'Rilla
  • 268
  • 3
  • 5
0

As you noted here you can indeed call a third method but you also can override the service() method from the HttpServlet motherclass so that it calls alawys one unique method.

Community
  • 1
  • 1
Antoine Martin
  • 1,903
  • 2
  • 16
  • 28