top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How does one choose between overriding the doGet(), doPost(), and service() methods?

+1 vote
327 views
How does one choose between overriding the doGet(), doPost(), and service() methods?
posted Nov 4, 2015 by Joy Nelson

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

The differences between the doGet() and doPost() methods are that they are called in the HttpServlet that your servlet extends by its service() method when it recieves a GET or a POST request from a HTTP protocol request.

A GET request is a request to get a resource from the server. This is the case of a browser requesting a web page. It is also possible to specify parameters in the request, but the length of the parameters on the whole is limited. This is the case of a form in a web page declared this way in html:

or
.

A POST request is a request to post (to send) form data to a resource on the server. This is the case of of a form in a web page declared this way in html:

. In this case the size of the parameters can be much greater.

The GenericServlet has a service() method that gets called when a client request is made. This means that it gets called by both incoming requests and the HTTP requests are given to the servlet as they are (you must do the parsing yourself).
The HttpServlet instead has doGet() and doPost() methods that get called when a client request is GET or POST. This means that the parsing of the request is done by the servlet: you have the appropriate method called and have convenience methods to read the request parameters.

NOTE: the doGet() and doPost() methods (as well as other HttpServlet methods) are called by the service() method.

Concluding, if you must respond to GET or POST requests made by a HTTP protocol client (usually a browser) don't hesitate to extend HttpServlet and use its convenience methods.
If you must respond to requests made by a client that is not using the HTTP protocol, you must use service().

answer Nov 5, 2015 by Karthick.c
...