Tomcat下中文乱码问题的解决思路

如今将常见的乱码问题分为JSP页面显示中文乱码、表单提交乱码两类。 javascript

     1)JSP页面中显示中文乱码 html

     在JSP文件中使用page命令指定响应结果的MIME类型,如<%@ page language="java" contentType="text/html;charset=gb2312" %> java

     2)表单提交乱码    web

     表单提交时(post和Get方法),使用request.getParameter方法获得乱码,这是由于tomcat处理提交的参数时默认的是iso-8859-1,表单提交get和post处理乱码问题不一样,下面分别说明。
    (1)POST处理
    对post提交的表单经过编写一个过滤器的方法来解决,过滤器在用户提交的数据被处理以前被调用,能够在这里改变参数的编码方式,过滤器的代码以下: tomcat

Java代码    收藏代码
  1. package example.util;  
  2.       
  3.     import java.io.IOException;  
  4.       
  5.     import javax.servlet.Filter;  
  6.     import javax.servlet.FilterChain;  
  7.     import javax.servlet.FilterConfig;  
  8.     import javax.servlet.ServletException;  
  9.     import javax.servlet.ServletRequest;  
  10.     import javax.servlet.ServletResponse;  
  11.       
  12.     public class SetCharacterEncodingFilter implements Filter {  
  13.       
  14.        protected String encoding = null;  
  15.       
  16.        protected FilterConfig filterConfig = null;  
  17.       
  18.        protected boolean ignore = true;  
  19.       
  20.     
  21.      public void destroy() {  
  22.       
  23.       this.encoding = null;  
  24.       this.filterConfig = null;  
  25.       
  26.      }  
  27.       
  28.      public void doFilter(ServletRequest request, ServletResponse response,  
  29.       <strong><span style="color: #ff0000;"> FilterChain chain) throws IOException, ServletException {  
  30.       
  31.           if (ignore || (request.getCharacterEncoding() == null)) {  
  32.        String encoding = selectEncoding(request);  
  33.        if (encoding != null) {  
  34.         request.setCharacterEncoding(encoding);  
  35.        }  
  36.       }</span>  
  37. </strong>      
  38.       // Pass control on to the next filter  
  39.       chain.doFilter(request, response);  
  40.       
  41.      }  
  42.     public void init(FilterConfig filterConfig) throws ServletException {  
  43.       
  44.       this.filterConfig = filterConfig;  
  45.       this.encoding = filterConfig.getInitParameter("encoding");  
  46.       String value = filterConfig.getInitParameter("ignore");  
  47.       if (value == null) {  
  48.        this.ignore = true;  
  49.       } else if (value.equalsIgnoreCase("true")) {  
  50.        this.ignore = true;  
  51.       } else if (value.equalsIgnoreCase("yes")) {  
  52.        this.ignore = true;  
  53.       } else {  
  54.        this.ignore = false;  
  55.       }  
  56.       
  57.      }  
  58.       
  59.      protected String selectEncoding(ServletRequest request) {  
  60.       
  61.       return (this.encoding);  
  62.       
  63.      }  
  64.       
  65.     }  

 

    文中红色的代码即为处理乱码的代码。
      web.xml文件加入过滤器 app

 

Xml代码    收藏代码
  1. <filter>  
  2.     <filter-name>Encoding</filter-name>  
  3.     <filter-class>  
  4.             example.util.SetCharacterEncodingFilter  
  5.      </filter-class>  
  6.     <init-param>  
  7.    <param-name>encoding</param-name>  
  8.    <param-value>gbk</param-value>  
  9.    <!--gbk或者gb2312或者utf-8-->  
  10.   </init-param>  
  11.   <init-param>  
  12.    <param-name>ignore</param-name>  
  13.    <param-value>true</param-value>  
  14.   </init-param>  
  15.  </filter>  
Xml代码    收藏代码
  1. <filter-mapping>  
  2.   <filter-name>Encoding</filter-name>  
  3.   <servlet-name>/*</servlet-name>  
  4.  </filter-mapping>  

 

(2) Get方法的处理
 tomcat对post和get的处理方法不同,因此过滤器不能解决get的乱码问题,它须要在其余地方设置。
 打开<tomcat_home>\conf目录下server.xml文件,找到对8080端口进行服务的Connector组件的设置部分,给这个组件添加一个属性:URIEncoding="GBK"。修改后的Connector设置为:
   post

Java代码    收藏代码
  1. <Connector port="8080" maxHttpHeaderSize="8192"  
  2.                maxThreads="150" minSpareThreads="25" maxSpareThreads="75"  
  3.                enableLookups="false" redirectPort="8443" acceptCount="100"  
  4.                connectionTimeout="20000" disableUploadTimeout="true" <span style="color: #ff0000;">URIEncoding="GBK"</span> />  


  * 注意修改后从新启动tomcat才能起做用。 this

相关文章
相关标签/搜索