Chinese garbled processing of JSP and Servlet

Source: Internet
Author: User

Today, I learned about Chinese garbled characters in JSP and Servlet. Now I want to make the following summary to help you. I also just learned, so I hope to understand the shortcomings.
1. garbled characters appear during form submission:
When submitting a form, you often submit some Chinese characters, which naturally prevents Chinese garbled characters. For a form, there are two submission methods: get and post. Therefore, there are get requests and post requests. In the past, I thought that the methods of get requests and post requests had the same garbled solution, but today I know that the methods of the two requests have different solutions. The reason is that they are most fundamental. When a get request is sent, the data it passes to the server is appended to the URL address. When a post request is sent, the data transmitted to the server is transmitted to the server as part of the Request body. This leads to different processing methods for Garbled text.
1. get requests from the client
For different request methods, the problem of solving Garbled text is also different. For client get requests, if the server side does not need Garbled text for processing, it is a little complicated to solve this problem, it needs to use the String Type constructor, one of the constructor is to use the specified encoding method to decode, generally use the "UTF-8" method. You only need to reconstruct the request parameters into a string on the server side. As follows:
String stuname = request. getParameter ("stuname ");
String str = new String (stuname. getBytes ("ISO-8859-1"), "UTF-8 ")
After the construction, the client enters Chinese characters and the str becomes Chinese when the get request is made during the form. If there are many request parameters, it is best to encapsulate them into a tool class:
Public class MyUtil
{
 
Public static String getNewString (String str) throws UnsupportedEncodingException
{
Return new String (str. getBytes ("ISO-8859-1"), "UTF-8 ");
}
}
String stuname = MyUtil. getNewString (request. getParameter ("stuname "));
2. Client post request
For the client post request, the problem of garbled processing is relatively simple, as long as the server at the beginning of the request data set to "UTF-8" on the line, enter the following statement:
Request. setCharacterEncoding ("UTF-8 ");
In this way, the Chinese data obtained on the server is no longer garbled.
2. garbled characters appear during hyperlinks (IE6 is unavailable for browsers of earlier versions)
In Web development, Chinese parameters are transmitted through hyperlinks many times, which may cause garbled Characters During display. For hyperlinks, it actually sends a request to the server, and the request is a get request, so for the garbled URL, the method for processing Garbled text is the same as that for form get requests.
String stuname = MyUtil. getNewString (request. getParameter ("stuname "));
3. garbled characters occur during redirection (IE6 is not supported in earlier browsers)
Sometimes garbled characters may occur when the sendRedirect () method of response is written for redirection. The redirection actually sends a request to the server, so the solution to garbled code is the same as above.
4. garbled characters caused by low browser versions
When surfing the Internet, some information submitted is displayed in the address bar "% 2C % C6 % CC % C6". In fact, this is a solution to prevent gibberish, if your browser is IE6 or earlier, garbled characters (especially when Chinese characters are odd) will occur in the second and third cases ), therefore, we must adopt another practical approach:
The URLEncoder class and URLDcoder class are provided in the java.net package. These two classes provide two static methods: encode and decode for encoding and decoding respectively. After the Chinese parameters to be passed are encoded, the Chinese parameters can be displayed after being passed to the server and decoded by the server.
Encoded: URLEncoder. encode (stuname, "UTF-8 ")
Pass to server: <a href = "/1.jsp? Stuname <% = stuname %> "> pass </a>
Decoding: URLDecoder. decode (stuname, "UTF-8 ");
In this way, we can get the passed Chinese parameters. I found that many websites use this method to solve the parameters.
5. Return the garbled code www.2cto.com displayed in the browser.
In Servlet programming, it is often necessary to return some information to the browser through the response object and give it to our client. However, we display Chinese characters on the server side, but the response to the client browser is garbled, this is mainly because the PrintWriter object returned by the getWriter () method of the response object uses the "ISO-8859-1" character set encoding to convert Unicode strings to byte arrays by default, because the ISO8859-1 Character Set root does not contain Chinese characters, therefore, Java will output invalid character codes to the client during conversion, so garbled characters appear, therefore, the ServletResponse interface defines setCharacterEncoding, setContentType, and other methods to specify the character set encoding used by the PrintWriter object returned by the getWriter method. Therefore, in the Servlet program, set the value of these methods before calling the getWriter method. To prevent garbled characters, we often write the following two statements together:
Response. setContentType ("text/html; charset = UTF-8 ");
Response. setCharacterEncoding ("UTF-8 ");
Write these two sentences as long as the Servlet file contains the response information to the client. It is best to write the second sentence, because it has a high priority, and its setting result will overwrite the character delimiter set by setContentType and other methods.
When writing Web documents, try to use UTF-8 encoding to avoid garbled characters in some cases.

Author: long2010yu2010

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.