1. Get the object content in the Request Message getinputstream and getreader Methods
1. getinputstream MethodIt is a byte input stream object and returns an object that represents the object content. Type: javax. servlet. servletinputstream
2. getreader MethodIs a bufferedreader object. 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.
Note: Before calling the getaskmethod, you can call the setcharacterencoding method to specify the character set encoding used by the returned bufferedreader object.
3. The instance isFile Upload----------- Example of reading object content
Step 1: Write a form in the HTML document
<Form method ="Post"Action ="Testinput"Name ="Form1"Enctype ="Multipart/form-Data">
Enter the first parameter: <input type ="Text"Name ="Param1"/> <Br/>
<! -- Upload a file -->
<Input type ="File"Name ="F1"/>
// Submit
<Input type ="Submit"Name ="OK"Value ="Submit"/>
</Form>
Note: enctype ="Multipart/form-Data"Yes.
Step 2:
First:
// Obtain the character input stream object
Bufferedreader BR = request. getreader ();
Filewriter fw =NewFilewriter ("D:/ouc.txt ");
// Put the read characters in the array to save
Char[] Arr =New char[1024];
// Read and display
While(Br. Read (ARR )! =-1 ){
The character array to be read is output or input from the console to a specified file.
System.Out. Println (NewString (ARR, 0, arr. Length ));
// FW. Write (Arr);
}
FW. Close ();
Second:
// Obtain the character input stream object
Bufferedreader BR = request. getreader ();
// Obtain its object through the context object
Servletcontext context =This. Getservletcontext ();
// Obtain the real path of the specified object
String filename = context. getrealpath ("/out.txt ");
// The output stream writes the read content to the file.
Filewriter fw =NewFilewriter (filename );
// Put the read characters in the array to save
Char[] Arr =New char[1024];
// Read and display
While(Br. Read (ARR )! =-1 ){
The character array to be read is output or input from the console to a specified file.
System.Out. Println (NewString (ARR, 0, arr. Length ));
// FW. Write (Arr);
}
FW. Close ();
2. Chinese reading in Request Parameters
Learning reason: Solve the garbled Problem
1. Understand the URL encoding of Chinese Characters
The URL encoding of a string in Java is actually URL encoding of its character set encoding (non-Unicode code) data, while the string in Java is encoded in the Unicode Character Set, therefore, when performing URL encoding on a string in Java, you must specify which non-UNICODE character set encoding is used for URL encoding.
The browser encodes a character in the request parameter by URL.
2. browser parsing steps:
(Entered from the text box) ------ the UTF-8 character set is e4b8ad ----- à the server will perform URL encoding (% E4 % B8 % AD) ---- à After decoding it into a byte array, decodes the code according to certain characters and sends the compiled characters to the web server.
3. Solutions for garbled characters:
Chinese Question about the getparameter Method
(1) When getparameter and other methods read parameter information, URL Decoding is required.
(2) For parameters following 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 setcharauanycterencoding 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.
Note: Each parameter name and parameter value separated by each parameter name and parameter value in the request message by the web server will also perform URL Decoding for certain character set encoding.
The character encoding method is to set the object content.
4. There are several reasons for garbled characters
(1) character encoding is set in HTML documents,
(2) set the encoding character set in the request.
Request. setcharacterencoding ("UTF-8 ");
(3) set the encoding character set in the request,
Response. setcontenttype ("text/html; charset = UTF-8 ");
(4) When the submission method is different, for example, if the get method is used for submission, garbled code may be output in the console window. You can modify it in service:
<Connector executor = "tomcatthreadpool"
Port = "8080" protocol = "HTTP/1.1"
Connectiontimeout = "20000"
Redirectport = "8443" usebodyencodingforurl = "true"/>