Note: All text information stored in a computer saves the characters we know (Chinese or English characters) in a certain encoding table (0, 1, 0, 1). The binary process from characters to computer storage is encoding, and the process from reading binary to text is called decoding. There are many different encoding tables for character encoding, so if the encoding format and decoding format are not the same code table, garbled code will appear. To avoid garbled code, you need to use the same code table when saving and reading.
There are often garbled codes in java web programming. Now let me explain in detail how to set them to avoid garbled codes.
1 Web page encoding
When writing a web page, you need to specify the encoding format of the web page and use <meta http-equiv="content-type" content="text/html; charset=UTF-8"> to specify it. At this time, when the browser reads or sends the request, the data will be saved or sent in the specified encoding format. Here it is in the form of utf-8.
For 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">Female<input type="radio" name="gender" value="female"><br> Favorite color: <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>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"> <input type="reset" value="reset"> </form>
2 Backend read request data
In the servlet of the java web, in order to obtain the requested data, the sent binary data needs to be decoded according to the corresponding code table to obtain the corresponding human-readable string. In this example, the post method is used, so when processing the post request, you need to set the encoding format before obtaining the Chinese request parameters, otherwise garbled code will occur. Because the server uses the iso-8859-1 encoding table by default for decoding.
Of course, if you want to output Chinese characters in the output, you also need to use unified character encoding. Here 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("<HEAD><TITLE>Test servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print("<h1>The following is your input</h1>"); out.print("<p>"); out.print("Your username:"+username+"<br>"); out.print("Your gender:"+gender+"<br>"); out.print("Your favorite color:"); 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"); here is valid only for the content of the requesting entity. The post request parameters are stored in the request entity, and the request parameters of the get method are placed after the url and start with a question mark, and '&' connect multiple parameters. So if you want to get the parameters of the get method, you need to use manual decoding, or use filter.
Manual decoding method, for the sake of simplicity, only gender decodes, and in actual use, each parameter needs to be decoded: String gender = new String(req.getParameter("gender").getBytes("iso-8859-1"),"utf-8");
At this point, the phenomenon of Chinese characters garbled on the web and server side can be perfectly solved. Remember one thing: garbled on the one hand is because the encoding and decoding use different encoding tables. You must use the same encoding table to solve the problem.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.