We often use servlets and JSPs, often using request.getparameter () to get data.
Request.getparameter ()
Request.getinputstream ()
Request.getreader ()
These three methods are the data submitted from the request object, but are used differently.
To select different methods based on the encoding of the <form> form submission data.
One of the key properties of form forms in HTML enctype:
1. enctype=application/x-www-form-urlencoded
This encoding is the default encoding method.
The result of the coding is usually field1=value2&field2=value2& forms, such as NAME=AAAA&PASSWORD=BBBB.
Commonly used forms are encoded in this way, and the servlet API provides support for decoding this encoding, and only calls the GetParameter () method in the ServletRequest class to get the fields and data in the user form.
Insufficient:
This encoding (application/x-www-form-urlencoded) is simple, but it does not seem to be enough to transmit large chunks of binary data.
2. For data that transmits large chunks of binary numbers, the browser uses another encoding, the "Multipart/form-data" encoding Method:
The browser can easily send the data and files from the form together. This encoding defines the string that good one is not likely to appear in the data as a delimiter, and then uses it to separate the segments of the data, and for each segment corresponds to an input area in the HTML page form, including a Content-disposition property, Describes the data section of the information, if the content of this data segment is a file, there will be a Content-type attribute, and then the data itself, if the data submitted in this way will be used Request.getinputstream () or Request.getreader () to get the data submitted, the data submitted by Request.getparameter () is not available.
3. It is important to note that:
The three methods of Request.getparameter (), Request.getinputstream (), Request.getreader () are conflicting because the stream can only be read once .
Like what:
When the form form content is enctype=application/x-www-form-urlencoded encoded, the parameters are first obtained by calling the Request.getparameter () method. The call to Request.getinputstream () or Request.getreader () has not been able to get the content in the stream because the system may have read the data submitted in the form in a stream at the time of calling Request.getparameter (). Vice versa.
When the form form content is enctype=multipart/form-data encoded, even if the first call to Request.getparameter () does not get the data, then Request.getparameter () is called. Method does not conflict with Request.getinputstream () or Request.getreader (), even if the Request.getparameter () is already called Method can also get the data in the form by calling Request.getinputstream () or Request.getreader (), while Request.getinputstream () and Request.getreader () Cannot be mixed in the same response, and if mixed use throws an exception.
In the HTTP request, there is the header and the body of the points, read the header using request.getheader ("...");
The read body uses Request.getreader (), but Getreader gets the BufferedReader and needs to convert it to a string, the following is the method of conversion.
String str = "";
InputStream in = Request.getinputstream ();
InputStreamReader reader = new InputStreamReader (in);
BufferedReader bd = new BufferedReader (reader);
while ((Inputline = Bd.readline ()) = null) {
str + = Inputline;
}
GetParameter () getInputStream () and getreader () differences