The problem of Chinese garbled code is really a very difficult problem, especially after being transmitted from the front desk to the back desk, I don’t know where the problem lies. Now we share 3 ways to solve the problem of garbled Chinese in front and back offices in javaWEB.
Method 1:
Tomcat's own encoding is in the format of ISO-8859-1 and is incompatible with Chinese encoding. So we should pay attention when receiving from the background.
Use the same format to receive (ISO-8859-1), and then convert it with a parsable encoding (utf-8). In this way, we can get a Chinese-compatible format. After this processing, send it to the front desk. Note: You need to set it when sending to the foreground
resp.setContentType("text/html;charset=utf-8");//Set the character encoding of the page to solve the problem of garbled Chinese text on the interface.
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Because tomcat comes with the encoding ISO-8859-1 format
//One of the methods to solve garbled code <span style="white-space:pre"> </span>String name=req.getParameter("username"); <span style="white-space:pre"> </span >String pwd=req.getParameter("pwd"); <span style="white-space:pre"> </span>byte[] b=name.getBytes("ISO-8859-1");//Use Tomcat format (iso-8859-1) is used to read. <span style="white-space:pre"> </span>String str=new String(b,"utf-8");//Use utf-8 to connect string <span style="white-space:pre "> </span>resp.setContentType("text/html;charset=utf-8");//Set the character encoding of the page <span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>PrintWriter pw =resp.getWriter(); <span style="white-space:pre"> </span>String str1="<html><body ><font size='5px' color='red'>username:"+name+"pwd:"+pwd+"</font></body></html>"; <span style="white-space:pre "> </span>pw.print(str1); PrintWriter pw =resp.getWriter(); String str1="<html><body><font size='5px' color='red'>username:"+name + "pwd:"+pwd+"</font></body></html>"; pw.print(str1);
Method 2:
Because the method is complicated, a simple setting is used. Just a simple sentence
req.setCharacterEncoding("utf-8");//It must be written in the first place because this method is used to read the data, otherwise the data will be incorrect.
This way, there is no need to set up as cumbersome as before
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Because tomcat comes with the encoding is ISO-8859-1 format//Solve garbled two "method" A more cumbersome》req.setCharacterEncoding("utf-8") ;//It must be written in the first place because this method is used to read the data, otherwise the data will be incorrect. //Set this way to read. This way, Chinese can be read, but attention is needed. The form must be sent by <span style="color:#ff0000;"> method='post'</span> resp.setContentType("text/html;charset=utf-8");//Set the passed The encoding displayed on the page String name=req.getParameter("username"); String pwd=req.getParameter("pwd"); PrintWriter pw =resp.getWriter(); String str1="<htm l><body><font size ='5px' color='red'>username:"+name+"pwd:"+pwd+"</font></body></html>"; pw.print(str1);
Method 3:
This is modified based on the two Dharmas. Although we can modify the encoding format to read, considering that the user will definitely not modify it, we need to adopt a more general method to let the user modify the configuration file. That is, the web.xml file needs to modify the contents in web.xml, that is, the character encoding is received from xml. Requires configuration parameters in the xml file.
The code is as follows:
<servlet> <servlet-name>Encodeing</servlet-name> <servlet-class>cn.hncu.com.encode.Encodeing</servlet-class> <init-param> <param-name>charset</para m- name> <param-value>utf-8</param-value>//The contents here can be filled in by the user (must be in encoding format) </init-param> </servlet>
We know that the exchange between the foreground and the background must be carried out through web.xml configuration. We need to obtain the parameters of the settings of web.xml
public void init(ServletConfig config) throws ServletException { charset=config.getInitParameter("charset");//Get the initialization parameters. Of course, charset needs to be set as a global variable. The subsequent service function needs to be set req.setCharacterEncoding(charset); }
req.setCharacterEncoding(charset); resp.setContentType("text/html;charset=utf-8"); String name=req.getParameter("username"); String pw d=req.getParameter("pwd"); PrintWriter pw =resp.getWriter(); String str1="<html><body><font size='5px' color='red'>username:"+name+"pwd:"+pwd+"</font></body> </html>"; pw.print(str1);
Resolved renderings:
Before resolving:
After the resolution:
Front Desk Code:
<body> <form action="login" method="post">//Login is configured in web.xml, and the username of the data can be read in the background: <input type="text" name="username" /><br/> Password: <input type="password" name="pwd"/><br/> <input type="submit" value="Login"/> </form> </body>
The above are three ways to solve the problem of garbled Chinese in front and back offices in javaWEB, especially the problem of garbled Chinese in the Chinese after being transmitted from the front desk to the backend. I hope it will be helpful to everyone's learning.