servlet doGet and doPost methods
Solution 1
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.
Solution 2
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).
Solution 3
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.
user460920
Updated on August 15, 2020Comments
-
user460920 over 3 years
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
-
a_horse_with_no_name about 12 yearsWhen would you actually want to distinguish between get and post in the servlet? What kind of use case (or requirement) would that be?
-
JB Nizet about 12 yearsGET 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.
-
Tomasz Nurkiewicz about 12 years
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 useHttpServletRequest.getMethod()
but it only complicates matters. -
user460920 about 12 yearsThanks 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 about 12 yearsOk..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..
-
Tomasz Nurkiewicz about 12 years@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 implementdoPost()
but the form will use GET, servlet container will throw an exception.