In the initial stage of learning JavaWeb, everyone will encounter the doGet and doPost methods in HttpServlet. I read "Head First Servlets & JSP" two days ago and saw that it talks about the difference between get requests and post requests in Servlets. I summarize it as follows:
1: The size of the requested data is different.
Because the data requested by get is appended to the URL, and the data size of the URL generally cannot exceed 2K, there is a limit to the size of the data using the get request method. The post request method puts the data in the message body, so in theory there is no limit on the amount of data. (But it cannot be too large in actual operation)
2: The security is different
Because the data requested by get is appended to the URL, external users can easily see it. From this perspective, it is not safe. The post method places the request in the message body and will not be displayed directly in the URL, so from this perspective the data will be safer.
3: Bookmark creation
Get requests can create bookmarks; post requests cannot. For example: suppose you have a page that allows users to specify search rules. The user may come back a week later and want to get the original data, but at this time there is new data on the server.
4: Use of methods
get is used to get something, just a simple acquisition, without making any changes to the server. Post means the user sends data for processing and can modify the data on the server.
5: Is the request idempotent?
The get request is idempotent, it just gets something and does not modify the content on the server. It can be performed multiple times without any bad side effects. The post is not idempotent, and the submitted data in the post body may be used for irreversible transactions. So from this perspective, you should use the doPost() function with caution.
If method="POST" is not specified in the form, it will default to an HTTP GET request. That is, the default state is to call the get request.
In the initial stage of learning JavaWeb, everyone will encounter the doGet and doPost methods in HttpServlet.
(1) doGet method: mainly handles Get requests in Http (2) doPost method: mainly handles Post requests in Http
So what is the difference between Get request and Post request?
(1) get has only one stream. The parameters are appended to the url. The number of parameters is strictly limited and can only be strings. For example, http://localhost:8888/javaweb/getServlet?name=123
(2) The parameters of the post are passed through another stream, not through the URL, so it can be very large, and binary data can also be passed, such as file upload.
When to use doGet and doPost methods:
1. For the servlet submitted through the form, see whether the method of the form is get or post.
2. servlet accessed through the link <a href...>, doGet
3. Type the servlet address directly in the IE address bar, doGet