Solutions to Chinese garbled characters in front and back of Java Web
Chinese problems have been hard to solve by many people. Especially after being transferred from the front-end to the backend, I don't know where the problem is. Several solutions are provided.
Method 1:
Tomcat comes with encoding is ISO-8859-1 format, is not compatible with Chinese encoding. Therefore, we should pay attention when receiving data from the background.
Use the same format to receive (ISO-8859-1), and then use the parsed encoding (UTF-8) to convert. In this way, we can get a Chinese-compatible format. After such processing, it is sent to the front-end. Note: you also need to set it when sending it to the front-end.
Resp. setContentType (text/html; charset = UTF-8); // sets the character encoding of the page to solve the problem of garbled Chinese characters displayed on the interface.
Protected void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// because tomcat's built-in encoding is in ISO-8859-1 format
// String name = req. getParameter (username); String pwd = req. getParameter (pwd); byte [] B = name. getBytes (ISO-8859-1); // read in tomcat format (iso-8859-1. String str = new String (B, UTF-8); // use UTF-8 to connect to stringresp. setContentType (text/html; charset = UTF-8); // set the character encoding of the page
PrintWriter pw =resp.getWriter();String str1=
Username: + name + pwd: + pwd +; pw. print (str1); PrintWriter pw = resp. getWriter (); String str1 = username: + name + pwd: + pwd +; pw. print (str1 );
Method 2: Because method 1 is cumbersome, simple settings are used. You only need a simple sentence.
Req. setCharacterEncoding (UTF-8); // It must be written in the first place because data is read in this way. Otherwise, an error occurs.
In this way, you do not need to make the settings as cumbersome as you previously have.
Protected void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// because tomcat comes with encoding is a ISO-8859-1 format // solve the garbled two "method a relatively cumbersome" req. setCharacterEncoding (UTF-8); // It must be written in the first place because data is read in this way. Otherwise, an error occurs. // Set this method to read. In this way, Chinese characters can be read, but note that. The form must be sent by method = 'post' resp. setContentType (text/html; charset = UTF-8); // you can specify the encoding String name = req. getParameter (username); String pwd = req. getParameter (pwd); PrintWriter pw = resp. getWriter (); String str1 =Username: + name + pwd: + pwd +; pw. print (str1); Method 3: (method 2 solves most of the garbled issues) This is modified on the basis of method 2. Although we can modify the encoding format for reading, we need to adopt a general method to allow users to modify the configuration file, considering that the user will not modify it. That is, the content in the web. xml file needs to be modified, that is, the character encoding is received from the xml. You must configure parameters in the xml file. The Code is as follows:
Encodeing
Cn.hncu.com. encode. Encodeing
Charset
UTF-8
// The content here can be entered by the user (the encoding format must be used)
We know that the frontend and backend must be configured in web. xml.
We need to obtain the web. xml setting parameters.
Public void init (ServletConfig config) throws ServletException {charset = config. getInitParameter (charset); // obtain the initialization parameter. Of course, charset must be set as a global variable. The following service function needs to set req. setCharacterEncoding (charset );}
req.setCharacterEncoding(charset);resp.setContentType(text/html;charset=utf-8);String name=req.getParameter(username);String pwd=req.getParameter(pwd);PrintWriter pw =resp.getWriter();String str1=
Username: + name + pwd: + pwd +; pw. print (str1); solution:
Front-end code: