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