Summary of HttpServletRequest objects

Source: Internet
Author: User

When the client accesses the server through the HTTP protocol, all information is requested to be encapsulated in the HttpServletRequest object, which can be used to obtain all the information requested, which is commonly described as follows:

The Getrequesturl method returns the full URL of the client when the request is made.
The Getrequesturi method returns the resource name portion of the request row.
The GetQueryString method returns the parameters section in the request line.
The GetPathInfo method returns additional path information in the request URL. The additional path information is the content in the request URL that precedes the servlet's path and before the query parameter, beginning with "/".
The Getremoteaddr method returns the IP address of the client that made the request.
The Getremotehost method returns the full host name of the client that made 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.

GetHeader (String name) method: string
Getheaders (String Name) method: Enumeration
Getheadernames () method

    • GetParameter (String) method (Common)
    • Getparametervalues (String name) method (Common)
    • Getparameternames () method ( not commonly used )
    • Getparametermap () method (often used for writing frames)
    • The SetAttribute (String name,object O) method, which stores the data as a property of the request object into the request object, for example: Request.setattribute ("Data", data);
    • GetAttribute (String name) method, gets the property value of the Name property of the Request object, for example: Request.getattribute ("Data")
    • RemoveAttribute (String name) method to remove the Name property of the Request object, for example: Request.removeattribute ("Data")
    • Getattributenames method, gets all the property names of the request object, returns one, for example:enumeration<string> attrnames = Request.getattributenames () ;

1, GetAttribute () and GetParameter () method differences

GetAttribute represents the property that is set from the request scope, which must be setattribute to set the property before it can be obtained by getattribute, set and acquired as Object type
GetParameter is a parameter that receives parameters, parameters for a page submission, including parameters for form submission, URL rewriting (which is the ID in xxx?id=1), and so on, so there is no way to set the parameters (no setparameter). And the receive parameter returns not an object, but a string type.

The Request.getattribute () method returns the object that exists in the request scope, and the Request.getparameter () method is to get the data submitted by HTTP.
The Request.getparameter () method uses the container's implementation to get incoming data through a similar post,get, which is uploaded from the Web client to the Web server side and represents the HTTP request data. The Request.getparameter () method returns data of type String.

The data passed by the Request.setattribute () and GetAttribute () methods will only exist inside the Web container, shared between Web Components that have a forwarding relationship, Request.setattribute (), and getattribute () Just flows inside the Web container. These two methods enable you to set shared data of type object.

2. HTTP request Content

In an HTTP request, the first line is the request line (Request-line), which describes the type of request, the resource (URL) to access, and the HTTP version used;
This is followed by a multi-line header (headers) message that describes the additional information the server will use;
The header information is followed by a carriage return newline character (/r/n), which is used to indicate the end of the header information.
The above is the necessary content, according to the need to increase the body data after the end of the information (request-body);
The principal data is followed by a carriage return newline character (/r/n), which indicates the end of the principal data.

It is important to note that
The URL portion of the request line (Request-line) must be encoded in application/x-www-form-urlencoded mode.
The encoding of the principal data (Request-body) is specified by the Content-type in the header (headers) information.
The length of the principal data (Request-body) is specified by the Content-length in the header (headers) information.

If you need to attach the principal data in the request, that is, increase the Request-body section, you must send the HTTP request by using Post. HTML Hyperlinks (<a></a>) can only submit HTTP requests in Get mode, and HTML forms (<form></form>) may submit HTTP requests in two ways.
In an HTTP request, Request-line is always encoded in application/x-www-form-urlencoded mode. The Enctype Tag property only works on Request-body. In other words, setting enctype only works if method= "POST". When the Enctype Tag property is set, a line of content-type information is given in the header (headers) information of the HTTP request, and the Request-body section is encoded with the MIME specified by Content-type. These actions are done automatically by the client browser.
When the enctype tag attribute is not specified, the form encodes the request-body in the default application/x-www-form-urlencoded manner. If name or value in the form field contains illegal characters (such as Chinese characters), the client browser automatically encoding it for URL processing.

3, the request garbled problem

To set the Request object encoding to be the same as the encoding type specified in Content-type when the client sends the request, for the garbled problem generated by the POST request. For a GET request or a link (which is essentially a GET request) to pass the Chinese garbled problem, it can be resolved by URL encoding, obtaining parameters after transcoding, or modifying the Tomcat configuration file in Uriencoding.

1, if the submission method is post, want to not garbled, only need to set the Request object encoding on the server side, the client is submitted by which encoding, the server-side Request object is received by the corresponding code, such as the client is submitted by UTF-8 encoding, Then the server-side Request object is received in UTF-8 encoding (request.setcharacterencoding ("UTF-8"))

2, if the submission method is get, set the encoding of the request object is not valid, the request object or the default iso8859-1 encoding to receive data, so to not garbled, only after receiving data and then manually converted, the steps are as follows:

1). Get the data from the client submission, get the garbled string, data= "??? È????? "

String data = Request.getparameter ("paramname");

2). Find the Iso8859-1 Code table and get a byte array of raw data submitted by the client

byte[] Source = data.getbytes ("iso8859-1");

3). Build a string with the specified encoding through a byte array to resolve garbled characters

data = new String (source, "UTF-8");

Constructs a string with the specified encoding through a byte array, where the encoding specified is based on the character encoding used when the client submits the data, and if it is GB2312, it is set to data = new string (Source, " GB2312 "), if it is UTF-8, then set to data = new String (source," UTF-8 ")

Get method to submit Chinese data garbled due to the problem of "Tomcat" encoding settings.
Workaround: In the Server.xml file under the Tomcat installation directory
<connector connection timeout= "20000" port= "8080" uriencoding= "UTF-8" prorocol= "http/1.1" redirectport= "8443"/ >
Uriencoding is the URL encoding of Tomcat, set to Utf-8 can be normal code with Chinese links

Http://www.cnblogs.com/xdp-gacl/p/3798347.html

Summary 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.