Servlet/JSP Container development NOTE 4-Request Parameter

Source: Internet
Author: User

Source of Request Parameters

1. query string of the request URL, such as/localhost/abc/test? Abc = ddd & bbb = ccc, the string following the question mark
2. For a POST request, if Content-Type = "application/x-www-form-urlencoded", the parameter comes from the request Content (in the same format as the query string)

For the first case, the request parameters can be read in any case using the getParameter series method using ISO-8859-1 Encoding

In the second case, it is a bit complicated. It involves the following methods of ServletRequest:

GetParameter Generation Method
GetCharacterEncoding
GetInputStream
GetReader

GetCharacterEncoding

Generally, the request characterEncoding comes from the charset parameter of Content-Type. If the charset parameter is not set, it is null.

GetInputStream and getReader (correct the error in Note 1)

These two methods are used to read the request content and cannot be used at the same time. If one method is used and another method is attempted, IllegalStateException is thrown.
The getReader method returns the BufferedReader object that uses getCharacterEncoding to encode the string. If it is null, The ISO-8859-1 is used.

GetParameter Generation Method

When the parameter comes from the request content, because the stream is used to read the request content

1. If the getInputStream and getReader methods are used before using the getParameter series methods, the request parameters cannot be obtained using the getParameter series methods.
2. When the getParameter series method is used before the stream is used, the stream should have reached the end, and the request parameters can be obtained.
3. Use getCharacterEncoding to encode the parameter. If it is null, use the ISO-8859-1
4. The getInputStream or getReader method is not directly used to obtain the stream for parsing parameters, because this will affect the status and cause the getInputStream or getReader method to throw IllegalStateException

Related Article

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.