The difference between doGet method and doPost method in servlet

1. Generation method
There are four ways to get: 1) Enter the URL directly in the URL address bar. 2) Hyperlinks in web pages. 3) The method in the form is get. 4) When the method in the form is empty, the default is get submit.
Post only knows that there is one: the method attribute in the form is post.
2. Data transmission method
get method: The form data is stored behind the URL address. There is no message body in HTTP when all get methods are submitted.
Post method: The form data is stored in the message body of the HTTP protocol and sent to the server in the form of an entity.
3. How the server obtains data
GET method: The server uses request.QueryString to obtain the value of the variable.
POST method: The server uses request.Form to obtain data.
4. The amount of data transmitted
GET method: The length of the data is limited, generally no more than 2kb. Because the parameter is passed and it is in the address bar, the amount of data is limited.
POST method: suitable for large-scale data transmission. Because it is transmitted in a physical way.
5. Security
GET method: poor security. Because the data is directly displayed in the address bar, the browser has buffering and can record user information. So security is low.
POST method: high security. Because the HTTP post mechanism is used when submitting data in the post method, the fields and values ​​in the form are placed in the HTTP HEADER and sent to the URL pointed to by ACTION, which is invisible to the user.
6. When the user refreshes
GET method: there will be no prompts,
POST method: A prompt box will pop up, asking the user whether to resubmit

1. get is to obtain data from the server, and post is to transmit data to the server.
2. get is to add the parameter data queue to the URL pointed to by the ACTION attribute of the submitted form, and the value corresponds to each field in the form, which can be seen in the URL. Post is through the HTTP post mechanism, each field in the form and its content are placed in the HTML HEADER and sent to the URL address pointed to by the ACTION attribute. The user cannot see this process.
3. For the get method, the server uses Request.QueryString to obtain the value of the variable, and for the post method, the server uses Request.Form to obtain the submitted data.
4. The amount of data transmitted by get is small and cannot be larger than 2KB. The amount of data transmitted by post is relatively large, and is generally defaulted to be unlimited. But in theory, the maximum amount is 80KB in IIS4 and 100KB in IIS5.
5. The security of get is very low, and the security of post is high. But the execution efficiency is better than the Post method. Suggestions: 1. The security of the get method is worse than that of the post method. If it contains confidential information, it is recommended to use the post data submission method;
2. When doing data query, it is recommended to use the Get method; when adding, modifying or deleting data, it is recommended to use the Post method;



 Servlet's doGet/doPost is implemented in javax.servlet.http.HttpServlet

          doGet: handle GET requests 
          doPost: handle POST requests 
      When making a client request, call the service method and pass a request and response object. The servlet first determines whether the request is a GET operation or a POST operation. Then it calls one of the following methods: doGet or doPost. The doGet method is called if the request is a GET, and the doPost method is called if the request is a POST. Both doGet and doPost accept requests (HttpServletRequest) and responses (HttpServletResponse).

      get has only one stream, the parameters are appended to the url, and the address line displays the information to be transmitted. The size and number are strictly limited and can only be strings, and the size is limited to 1024KB. The parameters of the post are passed through another stream, not through the url, so it can be very large, or it can pass binary data, such as file uploads.

     The parameters submitted by get through the URL will be displayed in the address bar, which may cause problems in the security of the system; the parameters submitted by post will not be displayed in the address bar. In this way, post can improve the security performance of get and avoid data leakage.

     当form框里面的method为get时,执行doGet方法,使用get提交就必须在服务器端用doGet()方法接收;当form框里面的method为post时,执行doPost方法,使用post提交就必须在服务器端用doPost()方法接收。

     在request请求里面,编码转换;get方法得到的内容每一个都要进行编码转换,而post方法则只要设置request.setCharacterEncoding("UTF-8")就可以,不要再从request得到的每个数据进行编码转换了。


 

1、安全

GET调用在URL里显示正传送给SERVLET的数据,这在系统的安全方面可能带来问题,例如用户名和密码等

POST就可以在一定程度上解决此类问题

2、服务器接收方式

服务器随机接受GET方法的数据,一旦断电等原因,服务器也不知道信息是否发送完毕

而POST方法,服务器先接受数据信息的长度,然后再接受数据

3、form运行方式

当form框里面的method为get时,执行doGet方法
当form框里面的method为post时,执行doPost方法

4、容量限制

GET方法后面的信息量字节大小不要超过1.3K,而Post则没有限制

###########最后说明的是:

你可以用service()来实现,它包含了doget和dopost ;service方法是接口中的方法,servlet容器把所有请求发送到该方法,该方法默认行为是转发http请求到doXXX方法中,如果你重载了该方法,默认操作被覆盖,不再进行转发操作!

service()是在javax.servlet.Servlet接口中定义的,   在   javax.servlet.GenericServlet   
    中实现了这个接口,   而   doGet/doPost   则是在   javax.servlet.http.HttpServlet   中实现的,  javax.servlet.http.HttpServlet   是   javax.servlet.GenericServlet   的子类.   
    
  所有可以这样理解,   其实所有的请求均首先由   service()   进行处理,   而在  javax.servlet.http.HttpServlet   的   service()   方法中,   主要做的事情就是判断请求类型是   Get   还是   Post,   然后调用对应的   doGet/doPost   执行.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324462826&siteId=291194637