GET Request Message

Source: Internet
Author: User

 

Obtains the entity content of the request message.

Getinputstream and getreader Methods

The getinputstream and getreader methods are used to obtain the binary byte input stream and character input stream.

1. The getinputstream method is used to return an input stream object representing the object content. Its type is javax. servlet. servletinputstream.

2. The getreader method is used to return a bufferedreader object that represents the object content. The returned bufferedreader object encodes the byte data in the object content into a text string according to the character set specified in the request message.

3. before calling the getreader method, you can call the setcharacterencoding method of servletrequest to specify the character set encoding used by the returned bufferedreader object.

Analyze the request message structure of File Upload

Servletinputstream SIS = request. getinputstream ();

String filepath = getservletcontext (). getrealpath ("/body. Out ");

Fileoutputstream Fos = new fileoutputstream (filepath );

Byte [] Buf = new byte [1, 1024];

Int Len = sis. Read (BUF, 0,1024 );

While (Len! =-1)

{

FOS. Write (BUF, 0, Len );

Len = sis. Read (BUF, 0,1024 );

}

FOS. Close ();

Sis. Close ();

 

Request domain attributes

1. objects stored in the servletrequest object are called request domain attributes. object data can be transmitted between multiple processing modules of the same request through request domain attributes.

2. methods related to request domain attributes:

Ü setattribute Method

Ü getattribute Method

Ü removeattribute Method

Ü getattributenames Method

Typical application of request domain attributes

1. the MVC design mode divides a request response process into three functional modules (generally called layers) for collaborative completion. The three modules are model (model layer), view (view layer), controller (control layer ).

2. The model is a business object that can be used as a JavaBean. The view is used to create a JSP page for the display interface. The controller is usually a servlet program that receives user requests, it creates the corresponding model object and calls the Business Method of the model object according to the request, and finally selects a view to create the webpage document content and send it back to the client.

3. The controller calls requestdispatcher. the forward method forwards the request to the JSP page that is used as the view, and transmits the model object as the request domain attribute. As the view JSP page, the model object is retrieved from the request domain.

Chinese reading of Request Parameters

1. url encoding for a string in Java is actually URL encoding for its character set encoding (non-Unicode code) data, in Java, strings are encoded in the Unicode Character Set. Therefore, when performing URL encoding on strings in Java, you must specify which non-UNICODE character set encoding is used for URL encoding.

2. JDK's java.net package has two classes: urlencoder and urldecoder. The urlencoder class defines an encode static method to complete URL encoding, the urldecoder class defines a decode static method to complete URL Decoding.

(1) Public static string encode (string S, string ENC)
Throws unsupportedencodingexception

(2) Public static string decode (string S, string ENC)
Throws unsupportedencodingexception

1,The browser encodes the Chinese characters entered in the form and then transmits them to the web server.

2,For the content entered in the Form on the page, the browser performs URL Encoding Based on the character set encoding currently used when the page is displayed.

Methods related to character set encoding of Request Parameters

1. getcharacterencoding Method

2. setcharacterencoding Method

Chinese Question about the getparameter Method

1. URL Decoding is required for getparameter and other methods to read parameter information.

2. For the parameters after the URL address in the request line of the HTTP request message, getparameter and other methods used for URL Decoding character set encoding in servlet specification does not clearly specify the Tomcat servletrequest object getparameter and other methods by default using the ISO8859-1 character set encoding for URL Decoding, therefore, the correct Chinese parameter information cannot be returned.

3. For the object content in the "Application/X-WWW-form-urlencoded" encoding format in the post mode, getparameter and other methods use getcharacterencoding () of the servletrequest object () the method returns the character set encoding to perform URL Decoding.

4. the return value of the getcharacterencoding () method is usually null. In this case, methods such as getparameter of the servletrequest object perform URL Decoding on parameters in the object content using the default ISO8859-1 character set encoding, therefore, the correct Chinese parameter information cannot be returned.

5. The servletrequest interface defines a setcharacterencoding method to set the character set encoding name of the object content in the request message. The getparameter method decodes the object Content Based on the character set encoding method.

6. The setcharacterencoding method sets the character set encoding name of the entity content in the request message, it only affects the result of URL Decoding of object content in the "Application/X-WWW-form-urlencoded" encoding format in the post mode, the getparameter method cannot affect the result of URL Decoding of the URL-based parameters in the request line of the HTTP request message.

Principles of character garbled characters and Solutions

1. If the page for submitting the form and the servlet program for processing the form request are both developed by the same person or the same project team, you only need to set the character set encoding passed to the setcharacterencoding method in the servlet program that processes form submission to the character set encoding on the page where the form is located.

2. If the servlet program can receive data submitted by form forms from multiple other sites, the character set encoding may vary for each page of other sites, to let the servlet program know the character set encoding of the Form Content, you can add a hidden field in the form to pass the character set encoding name of the current form content.

3. As long as any request path, such as hyperlink, Form Action attribute setting, request forwarding, and Redirection URL, contains parameters, the parameter must be URL encoded. When URL encoding is performed on parameters, the character set encoding should be consistent with the character set encoding on the current page. You can also use a parameter to specify the character set encoding name of the URL encoding content.

4. All standard browsers and client terminals support UTF-8 encoding, if the web server needs to be compatible with form information transmitted by browsers in various countries and regions, web documents should be in UTF-8 encoding format.

5. Some implicit character encoding errors should be noted when writing applications. Some programs have an encoding conversion error when outputting the data, and the returned string also has a conversion error when reading the input. When the program outputs the problematic string it reads, the displayed result may be a normal Chinese character.

 

 

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.