How to solve JSP parameter transmission garbled

Source: Internet
Author: User

Computer was born in the United States. English is his mother tongue, and other languages except English are foreign languages for him. Like us, He doesn't use a foreign language as well as his mother tongue no matter how much he has mastered. He often has some spelling mistakes.

The root cause of garbled characters is that different encoding schemes are used for encoding and decoding. For example, the GBK-encoded file, the result of decoding with a UTF-8 is certainly a 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 problem in the first two methods.

 

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 request's data body without the length limit, and the get method directly follows the URL in the request header, there is a length limit. The following is a request message for the same page.

Requesttest. jsp code

 
 
  1. <% @ Page language = "java" contentType = "text/html; charset = UTF-8"
  2. PageEncoding = "UTF-8" %>
  3. <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/lo
  4. Ose. dtd ">
  5. <Html>
  6. <Head>
  7. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
  8. <Title> Insert title here </title>
  9. </Head>
  10. <Body>
  11. <% -- Submit a form in post mode -- %>
  12. <Form action = "http: // localhost: 8888/EncodingTest/requestresult. jsp" method = "post">
  13. UserName: <input type = "text" name = "username"/>
  14. Password: <input type = "password" name = "password"/>
  15. <Input type = "submit" value = "Submit">
  16. </Form>
  17. </Body>
  18. </Html>
 
 
  1. <% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! D
  2. OCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loos
  3. E. dtd ">
  4. Itle> Insert title here </title>
  5. E = "username"/> Password: <input type = "password" name = "password"/> <input type = "submit" va
  6. Lue = "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 to Submit the request. The intercepted request packets are as follows:

Request message code in Post Mode

 
 
  1. POST /EncodingTest/requestresult.jsp HTTP/1.1   
  2. Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, applicati
  3. on/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*    
  4. Referer: http://localhost:8080/TomcatJndiTest/requesttest.jsp    
  5. Accept-Language: zh-cn    
  6. User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-ci
  7. ba; .NET CLR 2.0.50727)    
  8. Content-Type: application/x-www-form-urlencoded    
  9. Accept-Encoding: gzip, deflate    
  10. Host: localhost:8888   
  11. Content-Length: 49   
  12. Connection: Keep-Alive    
  13. Cache-Control: no-cache    
  14.    
  15. username=%E4%B8%96%E7%95%8C%E6%9D%AF&password=123   
  16. POST /EncodingTest/requestresult.jsp HTTP/1.1 Accept: image/gif, image/jpeg, image/pjpeg, image/pjp
  17. eg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, applicati
  18. on/msword, */* Referer: http://localhost:8080/TomcatJndiTest/requesttest.jsp Accept-Language: zh-cn Us
  19. er-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-ciba; .N
  20. ET CLR 2.0.50727) Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate H
  21. ost: localhost:8888 Content-Length: 49 Connection: Keep-Alive Cache-Control: no-cache username=%E
  22. 4%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

 
 
  1. GET /EncodingTest/requestresult.jsp?username=%E4%B8%96%E7%95%8C%E6%9D%AF&password=123 H
  2. TTP/1.1   
  3. Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, applica
  4. tion/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*    
  5. Referer: http://localhost:8080/TomcatJndiTest/requesttest.jsp    
  6. Accept-Language: zh-cn    
  7. User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-cib
  8. a; .NET CLR 2.0.50727)    
  9. Accept-Encoding: gzip, deflate    
  10. Host: localhost:8888   
  11. Connection: Keep-Alive   
  12. GET /EncodingTest/requestresult.jsp?username=%E4%B8%96%E7%95%8C%E6%9D%AF&passwo
  13. rd=123 HTTP/1.1 Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockw
  14. ave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */* Refer
  15. er: http://localhost:8080/TomcatJndiTest/requesttest.jsp Accept-Language: zh-cn User-Agent: Mozi
  16. lla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; aff-kingsoft-ciba; .NET CLR 2.0.50
  17. 727) 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 one such as "% E4 % B8 % 96% E7 % 95% 8C % E6 % 9D % AF ". string, then it is sent to the server. There may be two problems: Question 1: What is this string? Question 2: Why is this replacement necessary?

 

This string is the "UTF-8" encoding "E4B896E7958CE69DAF" corresponding to the "World Cup", which is formed by appending a "%" before each byte. As to why we need to do this conversion, my understanding is: because the request message will be encoded in the "ISO-8859-1" encoding method, transmitted to the server through the way of network flow. "ISO-8859-1" only supports numbers, English letters and some special characters, so such as Chinese characters such as "ISO-8859-1" is not recognized. 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 value of charset is the character set used by the browser to perform an "integer" Operation on the request message before submitting 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 container on the WEB server. After receiving the request message, the container, the request message will be parsed and an HttpServletRequest object will be generated with the information of this packet, and then the HttpServletRequest object will be sent to the jsp or Servlet requested on this page (in the above example, "requestresult. jsp "). In the requested jsp or Servlet ("requestresult. jsp" in the above example), use the getParameter ("") method of the HttpServletRequest object to obtain parameters from the previous page. By default, this method uses "ISO-8859-1" to decode, so the value of parameters for English or numbers can be obtained correctly, but for Chinese characters such as the character is not available, these Chinese characters have been "orthopedic" and cannot be recognized. If you want to recognize them again, you have to find the surgeon for the procedure, and then perform a "Restore" operation. The following solutions can be used in different situations.

 

 

 

 

Solution 1 code

 
 
  1. <%String str = new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8"); %>    
  2. Username:<%=str %>   
  3. <%String str = new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8"); %> Usern
  4. ame:<%=str %>  

Since request. getParameter ("username") by default, the returned string is extracted with "ISO-8859-1", then the first unidentifiable string and then "ISO-8859-1" to scatter, 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 1 is a more omnipotent method, both post and get are applicable, but it can be seen that its disadvantage is: For each parameter that may contain Chinese characters, it 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

 
 
  1. <%request.setCharacterEncoding("UTF-8"); %>   
  2. <%request.setCharacterEncoding("UTF-8"); %>  

Solution 2: The first request used at the beginning of the page or in the page. before the getParameter ("") method, add the code above, which is used to overwrite the default "ISO-8859-1" set in the request object with the response set passed in as a parameter. Request. the getParameter ("") method will be decoded with the new delimiter set, because "UTF-8" supports Chinese, therefore, the three Chinese characters passed as parameters for the "World Cup" 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 additional 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. In this case, you will find that the same method is still garbled. So if your request is submitted in get mode, you should choose solution 1!

 

As you can see from the above description, solution 2 needs to add <% request. setCharacterEncoding ("UTF-8"); %> This code, this is not very tired, so we thought of using the filter to help us do this thing, It is refreshing, simple.

Encodingfilter code

 
 
  1. Public class EncodingFilter implements Filter {
  2. Private String charset;
  3. @ Override
  4. Public void destroy (){
  5. // TODO Auto-generated method stub
  6. }
  7. @ Override
  8. Public void doFilter (ServletRequest request, ServletResponse response,
  9. FilterChain chain) throws IOException, ServletException {
  10. // Use the charset obtained by the init method to overwrite the charset of the intercepted request object
  11. Request. setCharacterEncoding (this. charset );
  12. // Hand over the request to the next filter, if any.
  13. Chain. doFilter (request, response );
  14. }
  15. @ Override
  16. Public void init (FilterConfig config) throws ServletException {
  17. // Obtain the character set from the filter configuration information in web. xml
  18. This. charset = config. getInitParameter ("charset ");
  19. }
  20. }
  21. Public class EncodingFilter implements Filter {private String charset; @ Override public void destr
  22. Oy () {// TODO Auto-generated method stub} @ Override public void doFilter (ServletRequest req
  23. Uest, ServletResponse response, FilterChain chain) throws IOException, ServletException {// use init
  24. The obtained charset overwrites the charset request. setCharacterEncoding (this. charset) of the intercepted request object; //
  25. The request is handed over to the next filter, if there are still cases. Chain. doFilter (request, response) ;}@ Override pub
  26. Lic void init (FilterConfig config) throws ServletException {// obtain the word from the filter configuration information in web. xml
  27. Character Set this. charset = config. getInitParameter ("charset ");}}

To apply the filter, add the following configuration information to the web. xml file.

Web. xml Code

 
 
  1. <filter>    
  2.    <filter-name>EncodingFilter</filter-name>    
  3.    <filter-class>cn.eric.encodingtest.filter.EncodingFilter</filter-class>    
  4.    <init-param>    
  5.        <param-name>charset</param-name>    
  6.        <param-value>UTF-8</param-value>    
  7.    </init-param>    
  8. </filter>    
  9. <filter-mapping>    
  10.    <filter-name>EncodingFilter</filter-name>    
  11.    <url-pattern>/*</url-pattern>    
  12. </filter-mapping>   
  13. <filter> <filter-name>EncodingFilter</filter-name> <filter-class>cn.eric.encodingtest.filter.Encodi
  14. ngFilter</filter-class> <init-param> <param-name>charset</param-name> <param-value>UT
  15. F-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filt
  16. er-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 be in Chinese. As shown below:

 
 
  1. <A href = "./jstlresult. jsp? Content = World Cup "> Go South Africa

Unlike form submission, http: // localhost: 8080/atatjnditest/jstlresult. jsp is displayed in the address bar of the browser after you click this hyperlink? Content = World Cup instead of 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:

 
 
  1. <A href = "./jstlresult. jsp? Content = <% = java.net. URLEncoder. encode ("World Cup", "UTF-8") %> "> Go South Africa

In this way, you can use

 
 
  1. <%String str = new String(request.getParameter("content").getBytes("ISO-8859-1"),"utf-8"); %> 

To obtain 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: first, use java.net. URLEncoder. encode ("paramValue", "charset") to process the Chinese characters in the Link url. Refer to 2 for the following procedure.

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.