Spring MVC controller and servlet Chinese scrambling problem

Keywords: encoding Tomcat Spring xml

http://tydldd.iteye.com/blog/2071869

Spring MVC controller and servlet Chinese scrambling problem

 

1. The first method solves Chinese scrambling by tomcat configuration and spring coding filter

1. Modify tomcat configuration file server.xml

 

  1. Modify conf/server.xml file under tomcat
  2. Find the following code:
  3. <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />  
  4. This code specifies Tomcat's port number to listen for HTTP requests and so on.   
  5. You can add an attribute here: URIEncoding, which sets the attribute value to UTF-8, so that Tomcat (default ISO-8859-1 encoding) can process get requests with UTF-8 encoding.   
  6. Upon completion of the modification:____________
  7. <Connector port="8080"  protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />  
2. spring Coding Filter, Configuring Coding Filter in web.xml

 

  1. <filter>  
  2.     <filter-name>encodingFilter</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>encodingFilter</filter-name>  
  15.     <url-pattern>/*</url-pattern>  
  16. </filter-mapping>  
  17. <filter-mapping>  
  18.     <filter-name>encodingFilter</filter-name>  
  19.     <url-pattern>*.jsp</url-pattern>  
  20. </filter-mapping>  

 

Second, the second method. Using the above configuration can basically solve the problem. But if you can't use a code filter without using spring mvc, you can request and respond separately.

 

Scrambling of request and response for servlet or spring MVC

1. request uses url to transfer Chinese parameters (such as http get) scrambling problem.

  1. //1. If type is Chinese, there may be Chinese scrambling problem. Because tomcat uses iso-8859-1 to parse Chinese, this will inevitably lead to scrambling.  
  2. String type = request.getParameter(Params.TYPE);  
  3.   
  4. //2. Use utf8 to parse type: use utf-8 to encode iso-88590-1 encoding strings  
  5. res = new String(type.getBytes("iso-8859-1"),"UTF-8");  

 

2. response Output Chinese String Scrambling

The first method is:

  1. //The getWriter() method sets the output encoding to iso-8859-1, so that the output utf8 encoding string must be scrambled.  
  2. PrintWriter pw = response.getWriter();  
  3. //1,              
  4. //response.setCharacterEncoding("UTF-8");  
  5. //2,  
  6. response.setContentType("text/html; charset=utf-8");  
  7.             pw.write(resStr);  
  8.             pw.flush();  
  9.             pw.close();  
  10.   
  11. setContentType and setCharacterEncoding Setting in two methods characterEncoding The method has the same effect on the server and does not need to be called repeatedly. When output text content, use response.setContentType("text/html; charset=utf-8");It seems more convenient.  

The second method:

  1. PrintWriter out =  new PrintWriter(new OutputStreamWriter(new FileOutputStream(), "UTF-8"));  

 

 

Conclusion:

1. To output Chinese in the servlet, if using the PrintWriter mode, you need to call setContentType or setCharacterEncoding before calling getPrintWriter(); the Servlet OutputStream mode is not limited.

 

2.setContentType and setCharacterEncoding have the same effect on the server and do not need to be called repeatedly. It seems more convenient to use response.setContentType("text/html; charset=utf-8") when outputting text content.

 

3.PrintWriter itself does not deal with the responsibility of coding, but it should be regarded as a decorator better: it is designed for more convenient output, providing print, println, printf and other convenient methods. To set the encoding, you can set it on its underlying Writer: (Here, the Output Stream Writer is the underlying Writer), refer to:

 

  1. new PrintWriter(new OutputStreamWriter(new FileOutputStream("yourfilepath"), "UTF-8"));   

Posted by tidou on Thu, 18 Apr 2019 12:00:33 -0700