Summary of the differences between get requests and post requests in JSPS and servlets

  • 2020-04-01 03:25:34
  • OfStack

In the early stages of learning about the JavaWeb, you will encounter the doGet and doPost methods in httpservlets. Two days ago, I saw Head First Servlets & JSP, which talked about the difference between get requests and post requests in Servlets. Here is the summary:

1: the size of the requested data is different.

Because the data of get request is to be appended to the URL, and the data amount of the URL cannot exceed 2K in general, the method of get request has a limit on the size of the data. The post request method, on the other hand, puts the data in the body of the message, so there is theoretically no limit to the amount of data. (but not too big in practice)

2: different security

Since the data requested by get is appended to the URL, external users can easily see it, which is not safe from this perspective. And the post method, because the request is in the body of the message, it's not directly displayed in the URL so the data is safe from that point of view.

3: bookmark creation

Get requests create bookmarks; A post request cannot. For example, suppose you have a page that allows users to specify search rules. The user might come back a week later and want to get the original data, but there is new data already on the server.

4: use of methods

Get is used to get something, it's a simple fetch, it doesn't make any changes to the server. Post allows the user to send data for processing and to modify the data on the server.

5: whether the request is idempotent

A get request is idempotent, it just wants to get something, it doesn't change the content on the server. It can be executed multiple times without any bad side effects. While post is not idempotent, the committed data in the post body may be used for irreversible transactions. So be careful about using the doPost() feature from this perspective.

If method= "POST" is not indicated in the form, it defaults to an HTTPGET request. The default is to invoke a get request.

  In the early stages of learning about the JavaWeb, you will encounter the doGet and doPost methods in httpservlets.

(1) doGet method: mainly deals with Get requests in Http
(2) doPost method: mainly handles Post requests in Http

So what's the difference between a Get request and a Post request

(1) get has only one stream, the parameters are attached to the url, the number of sizes is strictly limited and can only be strings
For example, http://localhost:8888/javaweb/getServlet? Name = 123
(2) the parameters of post are passed through another stream, not through the url, so it can be large, or it can pass binary data, such as file upload.

When to use the doGet and doPost methods:

1. The servlet submitted to by the form, whether the method of the form is get or post   
2. Through links < A  Href... > Access to the servlet, doGet   
3. Directly enter the servlet address in the ie address bar,doGet


Related articles: