Chinese garbled processing between JSP and Servlet

Source: Internet
Author: User

JSP and Servlet Chinese garbled processing I learned about Chinese garbled characters in JSP and Servlet a few days ago. I wrote a blog and updated it today. It should be able to solve the daily garbled problem. 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 occur during form submission: When submitting forms, some Chinese characters are often submitted, which naturally prevents Chinese characters from being garbled. For forms, there are two submission methods: get and post submission methods. 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. Each method has a different solution. garbled characters occur because the data transmitted to the server after the get request is appended to the URL address, 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. The client's get request solves the garbled problem for different request methods. For the client's get request, if the server does not need garbled code for processing, to solve this problem is a little more complex, you need to use the String type of constructor, one of the constructor is to use the specified encoding method to decode, generally using the "UTF-8" approach. You only need to reconstruct the request parameters into a string on the server side. String stuname = request. getParameter ("stuname"); String str = new String (stuname. after getBytes ("ISO-8859-1"), "UTF-8") is constructed, the client enters Chinese characters and str becomes Chinese when the get request is in 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. the post request of the client is simple for the post request of the client, because the request data is transmitted to the server as part of the Request body, you only need to modify the encoding in the request. As long as the request data is set to "UTF-8" at the beginning of the server, enter the following statement: request. setCharacterEncoding ("UTF-8"); so that the user's Chinese data on the server is no longer garbled. 2. garbled characters occur during hyperlinks (IE6 is not supported for browsers of earlier versions). In Web development, many Chinese parameters are transmitted through hyperlinks, this also causes garbled Characters During display. For a hyperlink, it actually sends a request to the server, and the request it sends is a get request, therefore, for hyperlink garbled characters, the method for handling garbled characters is the same as that for form get requests. String stuname = MyUtil. getNewString (request. getParameter ("stuname"); 3. garbled characters occur during redirection (IE6 is unavailable in earlier browsers). Sometimes garbled characters 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 text is the same as above. 4. When the browser version is low, leading to garbled Internet access, sometimes some information submitted is displayed in the address bar with the words "% 2C % C6 % CC % C6, in fact, this is a solution to prevent garbled characters. If your browser is IE6 or earlier, in this case, garbled characters occur in the second and third cases (especially when Chinese characters are odd numbers ), therefore, we must adopt another practical method: The URLEncoder class and URLDcoder class are provided in the java.net package, the two classes provide two static methods, encode and decode, respectively for encoding and decoding. 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. Encode: URLEncoder. encode (stuname, "UTF-8") passed to the server: <a href = "/1.jsp? Stuname <% = stuname %> "> pass </a> to decode: URLDecoder. decode (stuname, "UTF-8"); so that you can get the passed Chinese parameters, I found that many websites use this method to solve the parameters. 5. Returning garbled characters displayed by the browser in Servlet programming, some information is often returned to the browser through the response object for our client, and the Chinese characters we display on the server side, but the response to the client browser is garbled, mainly because the PrintWriter object returned by the getWriter () method of the response object uses the "ISO-8859-1" character set encoding by default for Unicode string to byte array conversion, because the root of the ISO8859-1 character set does not contain Chinese characters, so Java in the conversion will be invalid character encoding output to the client, so there is garbled, 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 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. 6. Modify Tomcat encoding in case of garbled code caused by the above get request, there is also a solution. We often use Tomcat as the container for running Servlet and JSP, tomcat internal default encoding is ISO-8859-1, so for the get request method, the transmitted data (URI) will be appended to the accessed resources, its encoding is Tomcat default, if you modify the URI encoding, there will be no garbled characters for all get request methods, including the redirection and hyperlinks mentioned above, in the Tomcat configuration file server. in xml, locate the location where Tomcat's port is modified, add the URIEncoding attribute inside it, set to the same value as the encoding set in your project, all here are UTF-8. As follows: <Connector port = "8080" protocol = "HTTP/1.1" connectionTimeout = "20000" redirectPort = "8443" URIEncoding = "UTF-8"/> when writing Servlet and JSP, to avoid garbled characters, the most important thing is to adopt consistent encoding. If the encoding is consistent, no garbled characters will occur.

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.