Parsing _java of Chinese garbled problems in Java Web requests and responses

Source: Internet
Author: User
Tags java web

Note: All the text information stored in the computer is a certain coding table (0,1,0,1) to save the characters we know (Chinese character or English character), from the character to the computer storage binary process is encoded, from the reading of binary to text process called decoding. Character encoding has a number of different encoding tables, so if the encoding format and decoding format is not the same Code table will appear garbled. To avoid garbled characters, you need to use the same Code table for saving and reading.

In Java Web Programming will often appear garbled, now detailed explain how to set up to avoid garbled

1 Web page encoding

When writing a Web page, you need to specify the encoding format of the Web page, using the <meta http-equiv= "Content-type" content= text/html; Charset=utf-8 "> to specify. When the browser reads or sends the request, the data is saved or sent in the specified encoding format. This is in the form of utf-8.

For example, code snippets:

<form action= "/pro1/bb" method= "POST" > Username
  :
  <input type= "text" name= "username" ><br>
  
  Gender:
  male <input type= "Radio" name= "Gender" value= "male" > Female <input type= "Radio" name= "Gender" value= "female" ><br >
  
  favorite Colors:<br>
  red <input type= "checkbox" name= "Color" value= "red" > Green <input type= "checkbox" Name= "Color" value= "green" >
  blue <input type= "checkbox" name= "Color" value= "Blue" > 
   
  <br> from Country 
  <select Name= "Country" >
   <option value= "China" > China </option>
   <option value= "USA" > USA </option >
   <option value= "Japan" > Japan </option>
  </select>
  
  <br>
  <input type= " Submit "value=" >
  <input type= "reset" value= "reset" >
  
  
 </form>

2 Back-end read request data

To get the requested data in the Java Web servlet, you need to decode the sent binary data according to the corresponding code table to get the corresponding human can read the string. This example uses the Post method, so in processing a POST request, you need to set the encoding format before getting the request parameters in Chinese, otherwise there will be garbled. Because the server defaults to decoding using the ISO-8859-1 encoding table.

Of course, if you want to output Chinese characters in the output, you also need to use a uniform character encoding, here is Utf-8, the following code

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> 

Note: The request.setcharacterencoding ("Utf-8") here is valid only for the content of the requesting entity. The POST request parameter is stored in the request entity, and the request parameter of the Get method is placed at the end of the URL with a question mark and a ' & ' connects multiple parameters. So you want to get the parameters of the Getting method, you need to use manual decoding, or use filter.

Manual decoding method, for the sake of simplicity only to decode the gender, the actual use of each parameter needs to be decoded: String gender = new String (Req.getparameter ("Gender"). GetBytes ("Iso-8859-1 ")," Utf-8 ");

At this point can be a perfect solution to the Web page and the server side of the phenomenon of Chinese characters garbled, remember A, there are garbled because of encoding and decoding the use of different coding table reasons, to use the same coding table, you can solve the problem.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.