Taiwan before and after the garbage problem

Taiwan before and after the garbage problem themselves due also read related blog introduced solution, but slightly different after their own local test garbled happen, so in conclusion to this, along with their environment

Development environment idea2019.3, tomcat8, windows7

Using the chrome browser for testing

1. foreground to the background transfer Chinese characters introduced:

Reception codes jsp file

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 
 3 <form action="requestPost.do" method="post">
 4     用户名:<input type="text" name="userName"/>
 5     <input type="submit" value="post方式提交表单">
 6 </form>
 7 
 8 <form action="requestGet.do" method="get">
 9     用户名:<input type="text" name="userName"/>
10     <input type="submit" value="get方式提交表单">
11 </form>

Servlet code behind

. 1  protected  void the doGet (the HttpServletRequest Request, the HttpServletResponse Response) throws ServletException, IOException {
 2  
. 3          // Get servletPath 
. 4          String servletPath = request.getServletPath ();
 . 5          // removed in the path name extension .do 
. 6          String methodName = servletPath. the substring (. 1, servletPath.length () -. 3 );
 . 7          the try {
 . 8              // using reflected methodName obtain a corresponding method 
. 9              method, method = getClass () getDeclaredMethod (methodName, the HttpServletRequest.. class , the HttpServletResponse. class );
10             //利用反射调用相应的方法
11             method.invoke(this, request, response);
12         } catch (Exception e) {
13             e.printStackTrace();
14             response.sendRedirect("error.jsp");
15         }
16     }
17 
18 
19     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
20         // TODO Auto-generated method stub
21         doGet(request, response);
22     }

 

 1 private void requestGet(HttpServletRequest request, HttpServletResponse response){
 2         System.out.println("requestGet");
 3 
 4         String userName = request.getParameter("userName");
 5         System.out.println("直接获取用户名: "+userName);
 6         try {
 7             request.setCharacterEncoding("UTF-8");
 8             userName = request.getParameter("userName");
 9             System.out.println("设置utf-8编码: "+userName);
10 
11             userName = new String(userName.getBytes("ISO8859-1"), "utf-8");
12             System.out.println("先ISO8859-1接收数据,后转UTF-8编码: "+ userName);
13 
14 //            response.setHeader("content-type", "text/html; charset=UTF-8");
15 //            response.getWriter().write(userName);
16         } catch (IOException e) {
17             e.printStackTrace();
18         }
19 
20     }
21   

result:

requestGet
Direct access Username: red
Provided utf-8 encoded: red
ISO8859-1 first received data after transfection UTF-8 encoding: ??
 1  private void requestPost(HttpServletRequest request, HttpServletResponse response){
 2         System.out.println("requestPost");
 3         String userName = request.getParameter("userName");
 4         System.out.println("直接获取用户名: "+userName);
 5         try {
 6             request.setCharacterEncoding("UTF-8");
 7             userName = request.getParameter("userName");
 8             System.out.println("设置utf-8编码: "+userName);
 9 
10             userName = new String(userName.getBytes("ISO8859-1"), "utf-8");
11             System.out.println("先ISO8859-1接收数据,后转UTF-8编码: "+ userName);
12 
13 //            response.setHeader("content-type", "text/html; charset=UTF-8");
14 //            response.getWriter().write(userName);
15         } catch (IOException e) {
16             e.printStackTrace();
17         }
18 
19     }

result:

requestPost
Direct access Username:? ° ????
Provided utf-8 encoded:? ° ????
ISO8859-1 first received data after transfection UTF-8 encoding: Bob

As can be seen from the above results, for the front desk get request method, the background is not required for any of its operations, Chinese characters will not be garbled situation;

Instead, the request for post way, the background will first need to use

ISO8859-1 received data after transfection UTF-8 encoding

To ensure the Chinese are not garbled.

In addition, if you set the character encoding in web.xml, then either post or get all without any treatment, the following settings are preventing front-end input data to the back garbled phenomenon.

 1   <filter>
 2         <filter-name>encoding-filter</filter-name>
 3         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 4         <init-param>
 5             <param-name>encoding</param-name>
 6             <param-value>UTF-8</param-value>
 7         </init-param>
 8         <init-param>
 9             <param-name>forceEncoding</param-name>
10             <param-value>true</param-value>
11         </init-param>
12     </filter>
13     <filter-mapping>
14         <filter-name>encoding-filter</filter-name>
15         <url-pattern>*.do</url-pattern>
16     </filter-mapping>

 

2. Background to the front desk output garbled phenomenon with Chinese characters:

Front Code:

1 <a href="query.do">Query</a><br/>
2 <a href="delete.do">Delete</a><br/>
3 <a href="update.do">update</a><br/>

Code behind servlet:

. 1  Private  void Update (the HttpServletRequest Request, the HttpServletResponse Response) {
 2          System.out.println ( "Update" );
 . 3          the try {
 . 4              / * 
. 5              * when the digital output, the digital need to convert into a string form and then converted to byte array output
 6              * Therefore, the output of the server when it is desired to see what can be any browser, the server should string
 7              is output in the form of *
 . 8              * * / 
. 9  
10              response.setHeader ( "Content-type "," text / HTML; charset = UTF-. 8 " );
 . 11              // response.getOutputStream () Write (. 1);. // front display no. 1 
12 is             response.getOutputStream () Write ((+. 1 "") the getBytes (. "UTF-. 8." ));
 13 is          } the catch (IOException E) {
 14              e.printStackTrace ();
 15          }
 16      }
 . 17  
18 is      Private  void Delete ( Request the HttpServletRequest, the HttpServletResponse Response) {
 . 19          System.out.println ( "Delete" );
 20 is          the try {
 21 is              / * 
22 is               * outputStream output using Chinese Note:
 23               * at the server, which code data output table, then they would control client opens a corresponding code table,
 24              * For example response.getOutputStream () write ( "delete" .getBytes ( "UTF-8" ));. // use
 25               * flow outputStream client browser output Chinese, utf-8 encoding for output
 26               * this we must control the client browser to utf-8 encoding to open, otherwise there will be Chinese garbled front display
 27               * can control the browser's behavior by setting the response headers solve the garbage problem:
 28               * response.setHeader ( "Content-of the type "," text / HTML; charset = UTF-. 8 ");
 29               * provided by the browser in response to the first control display data to the encoding utf-8
 30               * * / 
31 is              response.setHeader (" Content-type "," text / HTML; charset = UTF-. 8 " );
32              / * getBytes () is a process to convert the characters into an array of bytes, the process will look code table, if it is
 33              Chinese operating system environments, the default is to find GB2312 code table, converts the character into a byte array the process is to
34              converts the number corresponding to the Chinese code table GB2312, and
 35              such as: "in the" digital in GB2312 code table corresponds to the 98
 36              getBytes () without parameters, then the operating system will be selected locale code conversion table, if the operating system is the Chinese,
 37              then use the GB2312 code table
 38 is              * / 
39              response.getOutputStream () Write ( "delete" .getBytes ( "UTF-. 8." ));
 40          } the catch (IOException E) {
 41 is              e.printStackTrace ();
 42 is          }
 43 is      }
 44 is  
45      Private  void Query (the HttpServletRequest Request, the HttpServletResponse Response) {
 46 is          System.out.println ( "Query");
 47          the try {
 48              / * 
49              * same as the outputStream
 50              * set up in response to control of the browser to display a specified character code
 51 is              * response.setHeader ( "Content-type", "text / HTML; charset = UTF 8 ");
 52              * its equivalent wording:
 53 is              * response.getWriter () Write (." <Meta HTTP-equiv = 'Content-type' Content = 'text / HTML'; charset = 'UTF-8' / > ");
 54 is               * * / 
55              response.setHeader (" Content-type "," text / HTML; charset = UTF-. 8 " );
 56 is               . response.getWriter () Write (" query " );
57         } catch(IOException e) {
 58              e.printStackTrace ();
59          }
 60      }

Back to the front desk when output Chinese, whether used outputStream or PrintWriter, just need to add a

response.setHeader("content-type", "text/html; charset=UTF-8");

Will be able to solve the garbage problem, but also the way described here if the front desk when the digital output, you need to convert it to a string and then output, otherwise it will not display correctly.

 

The main reference herein aloof Wolf blog content, but the situation personally on their computers tested and which is a bit different, specific reason for the difference is not clear.



Guess you like

Origin www.cnblogs.com/alice-cj/p/11616552.html