Servlet methods doGet and doPost

I want to know that in servlets we use the doGet and doPost methods together in one program. What is its use?

What does the following code mean?
Why call the doGet method from doPost? I do not quite understand 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

+7
source share
5 answers

Just to make the servlet generalized, so even if we change the request method in the future, there is no need to edit the servlet, which will reduce the effort to modify the application in the future.

+2
source

doGet() processes incoming HTTP GET requests, and doPost() processes ... POST requests. There are also equivalent processing methods for PUT, DELTE, etc.

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

In many programs, the server doesn’t care if the request uses GET or POST, why one method simply delegates to another. This is actually bad practice, because these methods are essentially different from each other, but many textbooks write it like this (for better or for worse).

+20
source

This refers to the type of both queries, for example. GET and POST from http. Depending on the requirements of the application, people can choose the type of request as GET or POST, so that you will process both of them, you will get an error. and in case you want to handle both of them in the same way, you can create another doSomething method and call it from the doGet and doPost methods for more information see this answer

+3
source

Is this related to the get request, which allows you to see the parameters in the URL in the browser window, and the mail request includes parameters in the structure of the request and, therefore, is hidden from view. How your request will be made from the client as a recipient or message. I think this has something to do with security and avoids sql injections, but this is not my area. I hope some expert with the correct view / comment, as I need to know this myself.

+1
source

As you noted here , you can actually call the third method, but you can also override the service () method from the HttpServlet mother class so that it calls alawys with one unique method.

0
source

All Articles