Solve the Problem of garbled code passing through jsp Parameters

Source: Internet
Author: User

Solve the Problem of garbled code passing through jsp Parameters
Computer was born in the United States. English is his mother tongue, and other languages except English are foreign languages for him. Like us

It is not as easy to use as your mother tongue, and sometimes there are some spelling mistakes.
 
The root cause of garbled characters is that different encoding schemes are used for encoding and decoding. For example, the GBK encoding file, the UTF-8 to decode the results are certainly all

It's Mars. To solve this problem, the central idea is to use a unified coding solution.
 
Parameters between jsp pages are transmitted in the following ways: 1. submit a form. 2. directly use the URL followed by parameters (Hyperlink ).

3. If two jsp pages are in two different windows and the two windows are parent-child, jsp in the subwindow can also use javascript and

DOM (window. opener. XXX. value) to obtain the value of the jsp input element in the parent window. The following describes the garbled characters in the first two methods.

Analysis.
 
1. form submission implements parameter transfer between pages
Before introducing the content of parameters passed in a form, you should first learn some preparation knowledge. The form submission method and the processing of Chinese characters in the request message.
 
Form submission method:
The form submission method is usually post or get. The difference between the two is that the post method places the data content in the requested data

There is no length limit on the body part. The get method directly follows the URL of the request header and has a length limit. Below is the same

Page.
Requesttest. jsp code
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">

 
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Insert title here </title>
</Head>
<Body>
<% -- Submit a form in post mode -- %>
<Form action = "http: // localhost: 8888/EncodingTest/requestresult. jsp" method = "post">
UserName: <input type = "text" name = "username"/>
Password: <input type = "password" name = "password"/>
<Input type = "submit" value = "Submit">
</Form>
</Body>
</Html>
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html

PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"> <title> Insert title here </title>

</Head> <body> <% -- submit a form in post mode -- %> <form

Action = "http: // localhost: 8888/EncodingTestb/requestresult. jsp" method = "post"> UserName: <input

Type = "text" name = "username"/> Password: <input type = "password" name = "password"/> <input type = "submit"

Value = "Submit"> </form> </body> In the username input box on the above request page, the three Chinese characters "World Cup" are entered. In the password input box, enter "123" and press

The Submit button submits the request. The intercepted request packets are as follows:
Request message code in Post Mode
POST/EncodingTest/requestresult. jsp HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash,

Application/vnd. ms-excel, application/vnd. ms-powerpoint, application/msword ,*/*
Referer: http: // localhost: 8080/TomcatJndiTest/requesttest. jsp
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CBA; aff-kingsoft-CBA;

. Net clr 2.0.50727)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost: 8888
Content-Length: 49
Connection: Keep-Alive
Cache-Control: no-cache
 
Username = % E4 % B8 % 96% E7 % 95% 8C % E6 % 9D % AF & password = 123
POST/EncodingTest/requestresult. jsp HTTP/1.1 Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg,

Application/x-shockwave-flash, application/vnd. ms-excel, application/vnd. ms-powerpoint,

Application/msword, */* Referer: http: // localhost: 8080/atatjnditest/requesttest. jsp Accept-Language:

Zh-cn User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-

CBA;. NET CLR 2.0.50727) Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip,

Deflate Host: localhost: 8888 Content-Length: 49 Connection: Keep-Alive Cache-Control: no-cache

Username = % E4 % B8 % 96% E7 % 95% 8C % E6 % 9D % AF & password = 123
The above message content shows that the post request message has a dedicated data department .,
The following request message for get submission on the same request page:
Get request message code
GET/EncodingTest/requestresult. jsp? Username = % E4 % B8 % 96% E7 % 95% 8C % E6 % 9D % AF & password = 123 HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash,

Application/vnd. ms-excel, application/vnd. ms-powerpoint, application/msword ,*/*
Referer: http: // localhost: 8080/TomcatJndiTest/requesttest. jsp
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CBA; aff-kingsoft-CBA;

. Net clr 2.0.50727)
Accept-Encoding: gzip, deflate
Host: localhost: 8888
Connection: Keep-Alive
GET/EncodingTest/requestresult. jsp? Username = % E4 % B8 % 96% E7 % 95% 8C % E6 % 9D % AF & password = 123 HTTP/1.1 Accept:

Image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/vnd. ms-

Excel, application/vnd. ms-powerpoint, application/msword, */* Referer:

Http: // localhost: 8080/TomcatJndiTest/requesttest. jsp Accept-Language: zh-cn User-Agent: Mozilla/4.0

(Compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CBA; aff-kingsoft-CBA;. NET CLR 2.0.50727)

Accept-Encoding: gzip, deflate Host: localhost: 8888 Connection: Keep-Alive
The above message content shows that the get request message does not have a special data department, and the data is directly following the url.
 
Processing of Chinese characters in the Request Message:
From the preceding two types of messages, we can see that the three Chinese characters "World Cup" entered on the page have been replaced with "% E4 % B8 % 96% E7 % 95% 8C % E6 % 9D % AF ".

A string that is then sent to the server. There may be two problems: Question 1: What is this string? Question 2: Why?

Sample replacement?
 
This string is the "UTF-8" encoding "E4B896E7958CE69DAF" corresponding to the three Chinese characters "World Cup" append a "%" suffix before each byte

. As for why to do this conversion, my understanding is: because the request message will be encoded in the "ISO-8859-1" encoding method, through the Network Flow

To the server. "ISO-8859-1" only supports numbers, English letters and some special characters, so such characters as Chinese characters "ISO-8859-1"

I don't know. So it is necessary to give these "ISO-8859-1" does not support the character a "plastic" operation. In this way, the information on the page can be correctly transmitted

To the server.
 
At this time there may be another question: in the above example, why choose "UTF-8" encoding, Other encoding scheme can? The answer is yes.

. There is such a code in the header of the jsp page code "<% @ page language =" java "contentType =" text/html; charset = UTF-8"

PageEncoding = "UTF-8" %> "The charset value is the character set used by the browser to perform an" integer "Operation on the request message before the browser submits the request message.

It is also the character set used by the browser to interpret the server's response page.
 
After learning about the above content, I began to analyze the garbled characters of parameters passed in the form method.
For example, after clicking the "Submit" button, the browser sends the request message after the "shaping" operation to the Servlet content on the WEB server.

After receiving the request message, the container parses the request message and uses the message to generate an HttpServletRequest object. Then

Send the HttpServletRequest object to the jsp or Servlet requested on this page ("requestresult. jsp" in the above example "). Please

In the jsp or Servlet ("requestresult. jsp" in the preceding example), use the getParameter ("") method of the HttpServletRequest object to obtain

Parameters sent from the previous page. By default, this method uses the "ISO-8859-1" to decode, so the parameter value for English or numbers can naturally be positive

Indeed, Chinese characters such as Chinese characters cannot be obtained, because these Chinese characters have been "orthopedic" and cannot be recognized. If you want

If they recognize it, they have to find the surgeon who performs the operation and then perform a "Restore" operation. The following solutions can be used for different scenarios:

Status.
 
Solution 1 code
<% String str = new String (request. getParameter ("username"). getBytes ("ISO-8859-1"), "UTF-8"); %>
Username: <% = str %>
<% String str = new String (request. getParameter ("username"). getBytes ("ISO-8859-1"), "UTF-8"); %>

Username: <% = str %>
Since request. getParameter ("username") returns a string that is parsed using "ISO-8859-1" by default

Unidentifiable strings are then hashed by "ISO-8859-1", that is: request. getParameter ("username"). getBytes ("ISO-8859-1 ")

. Finally, use the character set consistent with the charset of your page to reorganize this String: new String (request. getParameter

("Username"). getBytes ("ISO-8859-1"), "UTF-8 "). In this way, you can see its true nature.
 
Solution One is a more omnipotent method, both post and get are applicable, but it can be seen that its disadvantage is: for every possible occurrence of Chinese Characters

Parameters must be displayed for such processing. One or two items are okay. If there are many items, you should consider whether you can use the following solution.

Solution 2 code
<% Request. setCharacterEncoding ("UTF-8"); %>
<% Request. setCharacterEncoding ("UTF-8"); %>
Solution 2: add the preceding generation before the first request. getParameter ("") method or the first request. getParameter ("") method on the page.

It is used as a parameter to overwrite the default "ISO-8859-1" set in the request object. This way

Request. getParameter ("") method will be decoded with the new delimiter set, because "UTF-8" supports Chinese, so as a parameter passed over the "World Cup"

Three Chinese characters can be correctly received. However, the request. setCharacterEncoding ("") method is described as follows in the API documentation:
 
Overrides the name of the character encoding used in the body of this request. This method must

Be called prior to reading request parameters or reading input using getReader (). Otherwise, it has no

TB.
 
Therefore, solution 2 only applies to post requests because the parameters are in the request body area. The request submitted in the get method is invalid.

At this time, you will find the same practice, but the display is still garbled. Therefore, if your request is submitted in get mode, you can choose a solution.

1!
 
As you can see from the above description, solution 2 needs to add <% request. setCharacterEncoding ("UTF-8"); %> This

Code Section, is this also very tired, so we thought of using filters to help us do this thing, It is refreshing, much simpler.
Encodingfilter code
Public class EncodingFilter implements Filter {

Private String charset;
@ Override
Public void destroy (){
// TODO Auto-generated method stub
}
 
@ Override
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Use the charset obtained by the init method to overwrite the charset of the intercepted request object
Request. setCharacterEncoding (this. charset );
// Hand over the request to the next filter, if any.
Chain. doFilter (request, response );
}
 
@ Override
Public void init (FilterConfig config) throws ServletException {
// Obtain the character set from the filter configuration information in web. xml
This. charset = config. getInitParameter ("charset ");
}
}
Public class EncodingFilter implements Filter {private String charset; @ Override public void destroy ()

{// TODO Auto-generated method stub} @ Override public void doFilter (ServletRequest request,

ServletResponse response, FilterChain chain) throws IOException, ServletException {// obtained using the init Method

Charset overwrites charset request. setCharacterEncoding (this. charset) of the intercepted request object. // transfers the request

Next, the filter. Chain. doFilter (request, response) ;}@ Override public void init

(FilterConfig config) throws ServletException {// obtain the character set from the filter configuration information in web. xml

This. charset = config. getInitParameter ("charset ");}}
To apply the filter, add the following configuration information to the web. xml file.
Web. xml Code
<Filter>
<Filter-name> EncodingFilter </filter-name>
<Filter-class> cn. eric. encodingtest. filter. EncodingFilter </filter-class>
<Init-param>
<Param-name> charset </param-name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
<Filter-mapping>
<Filter-name> EncodingFilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>
<Filter> <filter-name> EncodingFilter </filter-name> <filter-

Class> cn. eric. encodingtest. filter. EncodingFilter </filter-class> <init-param> <param-

Name> charset </param-name> <param-value> UTF-8 </param-value> </init-param> </filter> <filter-mapping>

<Filter-name> EncodingFilter </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>
 
2. directly use the URL followed by parameters (Hyperlink ).
In some cases, a hyperlink may be used to upload the parameter to the next page, and the value of this parameter may contain Chinese characters.

Status. As shown below:
<A href = "./jstlresult. jsp? Content = World Cup "> Go South Africa
Unlike form submission, When you click this hyperlink, what you see in the browser's address bar is

Http: // localhost: 8080/TomcatJndiTest/jstlresult. jsp? Content = World Cup, not

Http: // localhost: 8080/TomcatJndiTest/jstlresult. jsp? Content = % E4 % B8 % 96% E7 % 95% 8C % E6 % 9D % AF
Here, the browser does not help us with this transformation, so we have to do it ourselves. The procedure is as follows:
<A href = "./jstlresult. jsp? Content = <% = java.net. URLEncoder. encode ("World Cup", "UTF-8") %> "> Go South

Africa
In this way, you can use <% String str = new String (request. getParameter ("content"). getBytes

("ISO-8859-1"), "UTF-8"); %> method to get the value of this parameter correctly.
 
Summary:
1. post submission method: Use a filter to set the character encoding in the request object before arriving at the page to the same encoding as that on your page.
2. get submission method: <% String str = new String (request. getParameter ("content"). getBytes ("ISO-8859

-1 ")," UTF-8 "); %> such a string restructuring method.
3. hyperlink: Use java.net. URLEncoder. encode ("paramValue", "charset") to specify the Chinese characters in the url.

For details, refer to the following method 2.

Author: tbwshc

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.