java web request and response in Chinese garbled code problem resolution

  • 2020-05-10 18:06:37
  • OfStack

In the computer saved 1 cut text information is 1 fixed encoding table (0,1,0,1) to save the characters we know (Chinese or English characters), from the character to the computer stored binary process is encoding, from the reading of binary to the text process is called decoding. And character encoding has a variety of different encoding table, so, if the encoding format and decoding format is not the same as 1 code table will appear messy code. To avoid garbled code, use the same code table when saving and reading.

In java web programming, garble code often appears, now explain in detail how to set 1, to avoid garble code

1 webpage coding

When writing a web page, you need to specify the encoding format of the web page to use < meta http-equiv="content-type" content="text/html; charset=UTF-8" > To specify. When the browser reads or sends the request, it saves or sends the data in the specified encoding format. Here it is in the form of utf-8.

For example, code snippets:


  <form action="/Pro1/bb" method="post">
   User name: 
  <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> Country of origin  
  <select name="country">
   <option value=" China "> China </option>
   <option value=" The United States "> The United States </option>
   <option value=" Japan "> Japan </option>
  </select>
  
  <br>
  <input type="submit" value=" submit "> 
  <input type="reset" value=" reset ">
  
  
 </form>

The back end reads the request data

In java web, to get the requested data, you need to decode the sent binary data according to the corresponding code table to get the corresponding human-readable string. In this example, the post method is used, so in processing the post request, the encoding format should be set before obtaining the request parameters with Chinese characters, otherwise garbling will occur. Because by default the server USES the iso-8859-1 encoding table for decoding.

Of course, if you want to output Chinese characters in the output, you also need to use the same 1 character encoding, utf-8, as shown below


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("<h1> Here is your input </h1>");
  
  out.print("<p>");
  out.print(" Your username :"+username+"<br>");
  out.print(" Your gender :"+gender+"<br>");
  out.print(" The color you like :");
  for(String cr:colors){
   out.print(cr+" ");
  }
  out.print("<br>");
  
  out.print(" Your country: "+country+"<br>");
  
  out.print("</p>");
  
  out.println(" </BODY>");
  out.println("</HTML>");
 }

Note: request. setCharacterEncoding(" utf-8 "); Only valid for the content of the requesting entity. The post request parameters are stored in the request entity, and the get method's request parameters are placed at the end of url starting with a question mark, '&' to connect multiple parameters. So to get the parameters of the get method, you need to use either manual decoding or filter.

For the sake of simplicity, only the gender is decoded. In practice, 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 server side of the Chinese characters garbled, remember 1, garbled code is due to the encoding and decoding using different encoding table, to use the same encoding table, you can solve the problem.


Related articles: