如何解决Tomcat下中文乱码问题?

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

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

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

2)表单提交乱码

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

Java代码 复制代码

  1. package example.util;      


  2. import java.io.IOException;      


  3. import javax.servlet.Filter;      

  4. import javax.servlet.FilterChain;      

  5. import javax.servlet.FilterConfig;      

  6. import javax.servlet.ServletException;      

  7. import javax.servlet.ServletRequest;      

  8. import javax.servlet.ServletResponse;      


  9. publicclass SetCharacterEncodingFilter implements Filter {      


  10. protected String encoding = null;  


  11. protected FilterConfig filterConfig = null;  


  12. protectedboolean ignore = true;  



  13. publicvoid destroy() {      


  14. this.encoding = null;  

  15. this.filterConfig = null;  


  16.     }      


  17. publicvoid doFilter(ServletRequest request, ServletResponse response,      

  18.      <STRONG><SPAN style="COLOR: #ff0000"> FilterChain chain) throws IOException, ServletException {      


  19. if (ignore || (request.getCharacterEncoding() == null)) {  

  20.       String encoding = selectEncoding(request);      

  21. if (encoding != null) {  

  22.        request.setCharacterEncoding(encoding);      

  23.       }      

  24.      }</SPAN>      

  25. </STRONG>          

  26. // Pass control on to the next filter    

  27.      chain.doFilter(request, response);      


  28.     }      

  29. publicvoid init(FilterConfig filterConfig) throws ServletException {      


  30. this.filterConfig = filterConfig;      

  31. this.encoding = filterConfig.getInitParameter("encoding");  

  32.      String value = filterConfig.getInitParameter("ignore");  

  33. if (value == null) {  

  34. this.ignore = true;  

  35.      } elseif (value.equalsIgnoreCase("true")) {  

  36. this.ignore = true;  

  37.      } elseif (value.equalsIgnoreCase("yes")) {  

  38. this.ignore = true;  

  39.      } else {  

  40. this.ignore = false;  

  41.      }      


  42.     }      


  43. protected String selectEncoding(ServletRequest request) {      


  44. return (this.encoding);      


  45.     }      


  46.    }  

package example.util;


   import java.io.IOException;


   import javax.servlet.Filter;

   import javax.servlet.FilterChain;

   importjavax.servlet.FilterConfig;

   importjavax.servlet.ServletException;

   importjavax.servlet.ServletRequest;

   import javax.servlet.ServletResponse;


   public classSetCharacterEncodingFilter implements Filter {


      protected Stringencoding = null;


      protectedFilterConfig filterConfig = null;


      protected booleanignore = true;



    public void destroy() {


     this.encoding = null;

     this.filterConfig =null;


    }


    public voiddoFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {


         if(ignore || (request.getCharacterEncoding() == null)) {

      Stringencoding = selectEncoding(request);

      if(encoding != null) {

       request.setCharacterEncoding(encoding);

      }

     }


     // Pass control on tothe next filter

     chain.doFilter(request,response);


    }

   public void init(FilterConfigfilterConfig) throws ServletException {


     this.filterConfig =filterConfig;

     this.encoding =filterConfig.getInitParameter("encoding");

     String value = filterConfig.getInitParameter("ignore");

     if (value == null) {

      this.ignore = true;

     } else if(value.equalsIgnoreCase("true")) {

      this.ignore = true;

     } else if(value.equalsIgnoreCase("yes")) {

      this.ignore = true;

     } else {

      this.ignore = false;

     }


    }


    protected StringselectEncoding(ServletRequest request) {


     return(this.encoding);


    }


   }


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


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>

<filter>

   <filter-name>Encoding</filter-name>

   <filter-class>

           example.util.SetCharacterEncodingFilter

    </filter-class>

   <init-param>

 <param-name>encoding</param-name>

  <param-value>gbk</param-value>

  <!--gbk或者gb2312或者utf-8-->

 </init-param>

 <init-param>

  <param-name>ignore</param-name>

  <param-value>true</param-value>

 </init-param>

</filter>

Xml代码 复制代码

  1. <filter-mapping>

  2. <filter-name>Encoding</filter-name>

  3. <servlet-name>/*</servlet-name>

  4. </filter-mapping>

<filter-mapping>

 <filter-name>Encoding</filter-name>

 <servlet-name>/*</servlet-name>

</filter-mapping>


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

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> />  

<Connector port="8080"maxHttpHeaderSize="8192"

             maxThreads="150" minSpareThreads="25"maxSpareThreads="75"

             enableLookups="false" redirectPort="8443"acceptCount="100"

             connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />


 *
注意修改后重新启动tomcat才能起作用。

1.使用GBK编码的解决方案
这个最简单 遇到设置编码的地方就是用GBK数据库gbk 然后在使用个过滤器过滤编码为gbk一切搞定。
效果为添加数据无乱码 读出无乱码 数据库管理工具无乱码 到处sql结构和数据无乱码

2.使用UTF-8编码解决方案
所有编码都设置为UTF-8
数据库编码utf8
设置过滤器编码utf8
数据库连接?characterEncoding=utf8
然后在数据库管理工具或mysql命令行 运行 SETcharacter_set_results = gbk;
效果为添加数据无乱码 读出无乱码 数据库管理工具无乱码 到处sql结构和数据时存在乱码

3.页面使用UTF8 数据库使用latin1的解决方案
jap java tomcat 设置为UTF-8
过滤器 utf8
数据库连接?characterEncoding=latin1
数据库其他latin1
然后在数据库管理工具或mysql命令行 运行 SETcharacter_set_results = gbk;
效果为添加数据无乱码 读出无乱码 数据库管理工具无乱码 到处sql结构和数据时存在乱码



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章