servlet, doGet () and doPost () usage

Transfer: https: //blog.csdn.net/qq_38963960/article/details/79468182

1, servlet in doGet () and doPost () usage 

In general we are less than doGet method doGet method when the form is submitted will display the contents of submitted url behind, so insecure. And doGet method can only submit 256 characters (1024 bytes), and doPost no limit as delivery vehicles get way data is URL (submission can form, can be arbitrary URL link), and POST HTTP header is key to (can only be submitted in form mode). Usually we use are doPost method, you just let these two methods in the servlet call each other on the line, for example, wrote in the doGet method

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doPost(request,response);
    }

Then write business logic directly in the doPost method. servlet doGet method calls directly will encounter to call doPost because their parameters are the same. And doGet approach to Chinese problem is very difficult to write filter and the like.    

More from Baidu know right @ Boa

In the Internet to collect some information, the difference between the two methods are summarized as follows:
GET and post are two ways to http protocols, in addition to head, delete, etc. 
These two methods are essentially different, get only one stream, the additional parameters after the url, the number of sizes are strictly limited and only a string. Parameters are passed by an additional post flow, not through url, can be large, you may pass binary data, such as file uploads. 
In servlet development to doGet () and doPost () get and post treatment methods, respectively. 
First request judgment is get or post, if you get a call doGet (), if a post is called doPost (). This method will be executed. 
1.doGet
GET calls used to get server information, and as a response back to the client. When via a Web browser or through HTML, JSP Servlet direct access to the URL, usually with GET call. GET call is being passed in the URL display SERVLET of data, which may cause some problems in terms of security systems, such as user login, in the form of user name and password to be sent to the server side, if you use the Get call, the browser will displays the user name and password in the URL's.
Example:
JSP page code:

    <form action="/doGet_servlet" method="get">
    ………
    <input type="text" >
    ………
    </form>

servlet code:

    public class doGet_servlet extends HttpServlet {
      public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
          request.setCaracterEncoding(“gb2312”);//汉字转码
          PrintWriter out = response.getWriter();
          out.println("The Parameter are :"+request.getParameter("name1"));
      }
    }

After this form is submitted, the parameters will be automatically added to the browser address bar, to bring security issues.

2.doPost
It is used to transfer data to the client to the server, there will be side effects. But the advantage is you can hide any data to the server. Post suitable for sending large amounts of data.
Example:
JSP page code:

    <form action="/doPostt_servlet" method="post">
    ………
    <textarea cols="50" rows="10"></textarea>
    ………
    </form>

servlet code:

    public class doPostt_servlet extends HttpServlet {
      public void doPost(HttpServletRequest request,HttpServletResponse esponse) throws IOException,ServletException {
          request.setCaracterEncoding(“gb2312”);//汉字转码
          PrintWriter out = response.getWriter();
          out.println("The Parameter are :"+request.getParameter("name2"));
      }
    }

3. The method can be written in the doGet () method is called to perform in doPost () method, so, whether you submit a post or get method can be performed
such as:
JSP page code:

    <form action="/servlet" method="post">
    ………
    <input type="text" >
    ………
    </form>

servlet code:

    public class servlet extends HttpServlet {
      public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
          request.setCaracterEncoding(“gb2312”);//汉字转码
          PrintWriter out = response.getWriter();
          out.println("The Parameter are :"+request.getParameter("name1"));
      }
     
      public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
          this.goGet (Request, Response); // call doGet () method 
      } 
    }

Further, HttpServlet embodiment there is a client request doPut, doDelete, doTrace, doHead, doOptions, but uses less.

 

 

 

Distinction 2, servlet in doGet () and doPost () of

1, generating mode

get four ways: 1) directly entering the URL in the URL address bar. 2) hyperlinks web page. 3) form of the method is get. 4) form of the method is empty, the default is to get committed.

post only know that there is a: form in the method attribute to post.

2, the data transfer scheme

get way: form data stored behind the URL address. HTTP submit all get no message body.

post way: the form data stored in the message body by way of the HTTP protocol entity to the server.

3, server data acquisition mode

GET method: using request.QueryString server to get the value of the variable.

POST method: using request.Form server to retrieve data.

4, the amount of data transferred

GET mode: data length limited amount, generally no more than 2kb. Because the parameters are passed, and in the address bar, so the amount of data is limited.

POST way: for large-scale data transfer. Because entity by way of transfer.

5, security

GET way: poor security. Because the data directly in the address bar, the browser buffer, the user can record information. So security is low.

POST method: safe. Because the data submission mechanism of post HTTP post method is employed, the value field is placed in the form of the delivery HTTP HEADER URL ACTION referred to together, the user is invisible.

6, when the user refreshes

GET way: without warning,

POST method: prompt box will pop up, asking the user whether to resubmit

 

1. get the data is acquired from the server, post data is transmitted to the server.

2. get the parameter is added to data queue ACTION attribute of a form submission URL referred to in the various fields and values ​​within the form-one correspondence can be seen in the URL. post through HTTP post mechanism to various fields with its contents is placed in a form HTML HEADER URL address transmitted with the ACTION attribute refers. Users do not see this process.

3. For get embodiment, a server Request.QueryString variable value acquisition for post embodiment, the server acquires data submitted by Request.Form.

4. get the amount of data transfer is small, not greater than 2KB. Post large amount of data transmission, typically default to unlimited. But in theory, IIS4 the maximum amount of 80KB, IIS5 for 100KB.

5. get security is very low, post high security. But the efficiency of the Post method than good. Recommendation: 1, security get way than the way to badly Post, it contains confidential information, the proposal submission by Post data;

2, doing data query, recommended Get way; but in doing data added, modified or deleted, it is proposed Post manner;

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

          doGet: processing GET requests 
          doPost: POST request is processed 
      when the client issuing the request, and calls the service method of transmitting a request and response objects. Servlet first determines whether the request is GET or POST operation operation. It then calls a method following: doGet or doPost. If the request is GET doGet method is called, if the request is POST calls the doPost method. doGet and doPost accept requests (the HttpServletRequest) and response (HttpServletResponse).

      get only one stream, the parameter is appended to the url, information, the number of address lines display size to be transmitted is strictly limited and only a string, size limit 1024KB. Parameters are passed by an additional post flow, not through url, can be large, you may pass binary data, such as file uploads.

     get through URL parameters submitted will be displayed in the address bar, it may cause problems in terms of security systems; parameters submitted by post will not be displayed in the address bar. This post will be to improve the safety performance get, to avoid data leakage.

     When the form inside the box method to get when executed doGet method, get submitted must use doGet server-side method for receiving (); when the form inside the box a method for the post, perform doPost method using post submission must be in the server end with doPost () method of receiving.

     Inside the request in the request, transcoding; get content obtained by the method should be carried out for each transcoding method as long as the post is provided request.setCharacterEncoding ( "UTF-8") can not request further data obtained from each the transcoding.

1, security

GET call is being passed in the URL display SERVLET of data, which may cause problems in terms of security systems, such as user names and passwords, etc.

POST can solve this problem to some extent,

2, the server receives the mode

They were randomized to receive data server GET method, once the power failure and other reasons, the server does not know whether the information has been sent

And the POST method, a server to accept the data length information, and then receives the data

3, form Run

When the block form of the method which is get, doGet method performed
when the method form inside the box when post, perform the method doPost

4, capacity constraints

The amount of information bytes in size behind the GET method of not more than 1.3K, while the Post is no limit

Finally, it noted that:

You can use the service () implemented, it contains doget and dopost; service method is a method interface, the servlet container to send all requests to the method of the default behavior is forwarded http request to doXXX process, if you weight uploaded this method, the default action is covered, no further forwarding operations!

service () is defined in the interface javax.servlet.Servlet in javax.servlet.GenericServlet   
    implements this interface, the doGet / doPost is implemented in javax.servlet.http.HttpServlet in the javax.servlet.http .HttpServlet is a subclass of javax.servlet.GenericServlet.   
    
  All can be understood, in fact, all requests are first processed by the service (), and in javax.servlet.http.HttpServlet of service () method, the main thing to do is determined request type is Get or Post, and then call the corresponding doGet / doPost performed.

Guess you like

Origin www.cnblogs.com/isme-zjh/p/11820298.html