Good memory is not as good as the method and example of bad pen 9-HttpServletRequest, httpservletrequest

Source: Internet
Author: User

Good memory is not as good as the method and example of bad pen 9-HttpServletRequest, httpservletrequest

The HttpServletRequest object represents the client's request. When the client accesses the server through the HTTP protocol, all information in the HTTP request header is encapsulated in this object, through the method provided by this object, obtain all the information requested by the client.

Common Methods for HttpServletRequest

1. HttpServletRequest obtains client information

The getRequestURL method returns the complete URL when the client sends a request.

The getRequestURI method returns the resource name in the request line.

The getQueryString method returns the parameters in the request line.

The getPathInfo method returns the additional path information in the request URL. The additional path information is the content in the request URL following the Servlet Path and before the query parameter. It starts.

The getRemoteAddr method returns the IP address of the client sending the request.

The getRemoteHost method returns the complete host name of the client sending the request.

The getRemotePort method returns the network port number used by the client.

The getLocalAddr method returns the IP address of the WEB server.

The getLocalName method returns the Host Name of the WEB server.

 

2. HttpServletRequest obtains the header file (header) of the client request)

GetHeader (string name) method: String
GetHeaders (String name) method: Enumeration

GetHeaderNames () method

 

3. HttpServletRequest obtains the request parameters (data) submitted by the client)

GetParameter (String) method (commonly used)

GetParameterValues (String name) method (commonly used)

GetParameterNames () method (not commonly used)

GetParameterMap () method (often used for compiling frameworks)

4. source code used by HttpServletRequest

Package com. servlet;

 

Import java. io. IOException;

Import java. io. PrintWriter;

Import java. util. Enumeration;

 

Import javax. servlet. ServletException;

Import javax. servlet. http. HttpServlet;

Importjavax. servlet. http. HttpServletRequest;

Importjavax. servlet. http. HttpServletResponse;

/**

* Obtain client request information through the request object

* @ Author fan fangming

*/

Public class Request extends HttpServlet {

 

Privatestatic final long serialVersionUID =-5934755793836610405l;

 

Publicvoid doGet (HttpServletRequest request, HttpServletResponse response)

Throws ServletException, IOException {

// 1. Obtain client information

String requestUrl = request. getRequestURL (). toString (); // obtain the request URL

String requestUri = request. getRequestURI (); // obtain the requested resource

String queryString = request. getQueryString (); // obtain the parameters included in the request URL.

String remoteAddr = request. getRemoteAddr (); // obtain the visitor's IP address.

String remoteHost = request. getRemoteHost ();

Int remotePort = request. getRemotePort ();

String remoteUser = request. getRemoteUser ();

String method = request. getMethod (); // method used to obtain the request URL

String pathInfo = request. getPathInfo ();

String localAddr = request. getLocalAddr (); // obtain the IP address of the WEB server

String localName = request. getLocalName (); // obtain the WEB server host name

Response. setCharacterEncoding ("UTF-8"); // set to output characters in UTF-8 encoding to the client browser

// Control the browser by setting the response header to the UTF-8 encoding display data, the browser shows garbled Solution

Response. setHeader ("content-type", "text/html; charset = UTF-8 ");

 

PrintWriter out = response. getWriter ();

Out. write ("obtain the client information as follows :");

Out. write ("

Out. write ("request URL:" + requestUrl );

Out. write ("<br/> ");

Out. write ("requested resource:" + requestUri );

Out. write ("<br/> ");

Out. write ("parameter included in the request URL:" + queryString );

Out. write ("<br/> ");

Out. write ("Visitor's IP Address:" + remoteAddr );

Out. write ("<br/> ");

Out. write ("Host Name of the visitor:" + remoteHost );

Out. write ("<br/> ");

Out. write ("the port number used:" + remotePort );

Out. write ("<br/> ");

Out. write ("remoteUser:" + remoteUser );

Out. write ("<br/> ");

Out. write ("method used by the request:" + method );

Out. write ("<br/> ");

Out. write ("pathInfo:" + pathInfo );

Out. write ("<br/> ");

Out. write ("localAddr:" + localAddr );

Out. write ("<br/> ");

Out. write ("localName:" + localName );

Out. write ("<br/> ");

// 2. Obtain the header file of the client request (header)

Enumeration <String> reqHeadInfos = request. getHeaderNames (); // obtain all request headers

Out. write ("Get the header file of the client request (header ):");

Out. write ("

While (reqHeadInfos. hasMoreElements ()){

String headName = (String) reqHeadInfos. nextElement ();

String headValue = request. getHeader (headName); // obtain the value of the corresponding request header Based on the name of the request Header

Out. write (headName + ":" + headValue );

Out. write ("<br/> ");

}

Out. write ("<br/> ");

Out. write ("value of the client Accept-Encoding Request Header obtained :");

Out. write ("

String value = request. getHeader ("Accept-Encoding"); // obtain the value corresponding to the Accept-Encoding request header.

Out. write (value );

Enumeration <String> e = request. getHeaders ("Accept-Encoding ");

While (e. hasMoreElements ()){

String string = (String) e. nextElement ();

Out. write (string );

}

// 3. Obtain the request parameters (data) submitted by the client)

Out. write ("<br/> ");

String paramA = request. getParameter ("paramA ");

String [] paramB = request. getParameterValues ("paramB ");

Out. write ("obtain the request parameters (data) submitted by the client ):");

Out. write ("

Out. write ("parameter paramA:" + paramA );

Out. write ("<br/> ");

Out. write ("parameter paramB:" + paramB );

Out. write ("<br/> ");

}

 

Public void doPost (HttpServletRequest request, HttpServletResponseresponse)

Throws ServletException, IOException {

DoGet (request, response );

}

 

}

5. Running result

Enter in the browser

Http: // localhost: 8080/webStudy/request? ParamA = a & paramB = 1% 202% 203

The result is as follows:

Obtain the client information as follows:

Request URL: http: // localhost: 8080/webStudy/request

Requested Resource:/webStudy/request

Parameters included in the request URL: paramA = a bytes mB = 1% 202% 203

Visitor's IP Address: 0: 0: 0: 0: 0: 0: 0: 1

Visitor's host name: 0: 0: 0: 0: 0: 0: 0: 1

Port used: 50344

RemoteUser: null

Request Method: GET

PathInfo: null

LocalAddr: 0.0.0.0

LocalName: 0.0.0.0 get the header file of the client request (header ):

Host: localhost: 8080

Connection: keep-alive

Accept: text/html, application/xhtml + xml, application/xml; q = 0.9, image/webp, */*; q = 0.8

User-agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.20.safari/537.36

Accept-encoding: gzip, deflate, sdch

Accept-language: zh-CN, zh; q = 0.8

Cookie: JSESSIONID = 83372ECC1D536CC3586DB8B27CD5013F

 

The value of the client Accept-Encoding Request Header obtained:

Gzip, deflate, and sdch obtain the request parameters (data) submitted by the client ):

Parameter paramA:

Parameter paramB: [Ljava. lang. String; @ 96ad7c

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.