Java web requests and responses contain garbled characters in Chinese.

Source: Internet
Author: User

Java web requests and responses contain garbled characters in Chinese.

Note: All text information stored in the computer is stored in a certain encoding table (,) to save the characters we know (Chinese or English characters ), the binary process stored by characters to the computer is encoding, and the process of reading binary data to text is called decoding. Character encoding has many different encoding tables. Therefore, if the encoding format and decoding format are not the same, garbled characters may occur. To avoid garbled characters, use the same code table for saving and reading.

 

Garbled characters often occur in java web programming. Now I will explain in detail how to set them to avoid garbled characters.

 

1 webpage code

When writing a webpage, you must specify the webpage encoding format, which is specified using <meta http-equiv = "content-type" content = "text/html; charset = UTF-8">. When the browser reads or sends a request, it saves or sends data in the specified encoding format. In UTF-8 format.

Example code snippet:

<Form action = "/Pro1/bb" method = "post"> username: <input type = "text" name = "username"> <br> gender: male <input type = "radio" name = "gender" value = "male"> & nbsp; female <input type = "radio" name = "gender" value = "female"> <br> favorite colors: <br> Red <input type = "checkbox" name = "color" value = "red"> & nbsp; green <input type = "checkbox" name = "color" value = "green"> & nbsp; blue <input type = "checkbox" name = "color" value = "blue"> <br> countries from <select name = "country"> <option value = "China"> China </option> <option value = "us"> us </option> <option value = "Japan"> Japan </option> </select> <br> <input type = "submit" value = "submit"> & nbsp; <input type = "reset" value = "reset"> </form>

 

2. the backend reads the request data.

To obtain the requested data in a java web servlet, You need to decode the sent binary data according to the corresponding code table to obtain the corresponding human readable string. In this example, the post method is used. Therefore, in processing post requests, you must set the encoding format before obtaining Request Parameters with Chinese characters. Otherwise, garbled characters may occur. Because the server uses the iso-8859-1 encoding table for decoding by default.

Of course, if you want to output Chinese characters in the output, you also need to use a unified character encoding, Which is UTF-8. The Code is as follows:

Public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html; charset = UTF-8"); PrintWriter out = response. getWriter (); String username = request. getParameter ("username"); String gender = request. getParameter ("gender"); String [] colors = request. getParameterValues ("color "); String country = request. getParameter ("country"); out. println ("<! Doctype html> "); out. println ("<HTML>"); out. println ("<HEAD> <TITLE> test servlet </TITLE> </HEAD>"); out. println ("<BODY>"); out. print ("

Note: The request. setCharacterEncoding ("UTF-8") Here is only valid for the content of the request entity. THE post request parameters are stored in the Request Entity. The get request parameters are placed behind the url and start with question marks. '&' connects multiple parameters. To obtain the parameters of the get method, you must use manual decoding or filter.

Manual decoding method. For the sake of simplicity, only the gender is decoded. In actual use, each parameter needs to be decoded: String gender = new String (req. getParameter ("gender "). getBytes ("iso-8859-1"), "UTF-8 ");

At this time, we can perfectly solve the problem of garbled Chinese characters on the webpage and server. Remember that all garbled characters are caused by the use of different encoding tables for encoding and decoding, you can solve the problem by using the same encoding table.

 

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.