Garbled characters often appear in java web programming. Now let’s explain in detail how to set it up to avoid garbled characters
1 Web page encoding
When writing a web page, you need to specify the encoding format of the web page. Use to specify. At this time, when the browser reads or sends a request, it will save or send the data in the specified encoding format. Here it is in utf-8 form.
For example, code snippet:
<form action="/Pro1/bb" method="post"> 用户名: <input type="text" name="username" ><br> 性别: 男<input type="radio" name="gender" value="男"> 女<input type="radio" name="gender" value="女"><br> 喜欢的颜色:<br> 红<input type="checkbox" name="color" value="红"> 绿<input type="checkbox" name="color" value="绿"> 蓝<input type="checkbox" name="color" value="蓝"> <br>来自的国家 <select name="country"> <option value="中国">中国</option> <option value="美国">美国</option> <option value="日本">日本</option> </select> <br> <input type="submit" value="提交"> <input type="reset" value="重置"> </form>
2 The backend reads the request data
In order to obtain the requested data in the java web servlet, the binary data sent needs to be decoded according to the corresponding code table to obtain the corresponding Humans can read strings. In this example, the post method is used, so when processing the post request, the encoding format needs to be set before obtaining the Chinese request parameters, otherwise garbled characters will appear. 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, 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("<HTML>"); out.println(" <HEAD><TITLE>测试servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print("<h1>以下是您的输入</h1>"); out.print("<p>"); out.print("您的用户名:"+username+"<br>"); out.print("您的性别:"+gender+"<br>"); out.print("您喜欢的颜色:"); for(String cr:colors){ out.print(cr+" "); } out.print("<br>"); out.print("您的国家:"+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. The request parameters of the get method are placed after the url starting with a question mark, and '&' connects 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 decodes gender. 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 garbled Chinese characters on web pages and servers can be perfectly solved. Remember one thing, garbled characters are caused by different coding tables used for encoding and decoding. Use The same coding table will solve the problem.