Use of HttpServletRequest objects

Source: Internet
Author: User

The browser encapsulates all the request information into the HttpServletRequest object.

The browser accesses the resource directly, and the default submission method is the Get method.

Entity content: Only post-submitted parameters are placed in the entity content.

1@WebServlet ("/requestdemo1")2  Public classRequestDemo1extendsHttpServlet {3     /**4 * These action servers have been done:5 * 1) The Tomcat server receives the request data sent by the browser and encapsulates it to the HttpServletRequest object6 * 2) The Tomcat server calls the Doget method and passes the request object into the servlet7      * @authorMaodoer8      *9      */Ten     protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { One     /** A * 3) Remove the request data from the requests object.  -      */ -         /** the * 3.1 Request lines -          */ -System.out.println ("Request method:" +request.getmethod ());//Request Method -System.out.println ("URI:" +request.getrequesturi ());//Requesting Resources +System.out.println ("URL:" +Request.getrequesturl ());  -SYSTEM.OUT.PRINTLN ("HTTP protocol version:" +request.getprotocol ());//HTTP protocol version +         /** A * 3.2 Request Header at          */ -String Host=request.getheader ("host");//take the value of the head according to the header name - System.out.println (host); -Enumeration<string> enums = Request.getheadernames ();//gets all the names in the request request header -          while(Enums.hasmoreelements ()) { -String Headname =enums.nextelement (); inString Headervalue =Request.getheader (headname); -System.out.println (headname+ "::" +headervalue); to         } +     } -      the     //in order to receive post-submitted requests, the post-submission request must be requested using the Dopost method * @Override $     protected voidDoPost (httpservletrequest request, HttpServletResponse resp)throwsservletexception, IOException {Panax Notoginseng         /** - * 3.3 Entity content the          */ +InputStream in = Request.getinputstream ();//Get entity Content A         byte[] buf=New byte[1024]; the         intLen=0; +          while((Len=in.read (BUF))!=-1){ -String str =NewString (buf,0, Len); $ System.out.println (str); $         } -     } -}

HttpServletRequest Object

The HttpServletRequest object is used to obtain the requested data. The Core API:

Request Line:

Request.getmethod ();//How to get Submitted

Request.getrequesturi (); Request.getrequesturl ();//Get Resources

Request.getprotocol ();//Request HTTP protocol version

Request Header:

Request.getheader ("name");//Get request value by name

Request.getheadernames ();//Gets all the names in the request header

Entity content:

Request.getinputstream ();//Get Data for entity content

Use of HttpServletRequest objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.