tomcat下中文乱码

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

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

     2)表单提交乱码   

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

Java代码:

  1. import java.io.IOException;  
  2. #       
  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. #       
  10. #     public class SetCharacterEncodingFilter implements Filter {  
  11. #       
  12. #        protected String encoding = null;  
  13. #       
  14. #        protected FilterConfig filterConfig = null;  
  15. #       
  16. #        protected boolean ignore = true;  
  17. #       
  18. #     
  19. #      public void destroy() {  
  20. #       
  21. #       this.encoding = null;  
  22. #       this.filterConfig = null;  
  23. #       
  24. #      }  
  25. #       
  26. #      public void doFilter(ServletRequest request, ServletResponse response,  
  27. #       <strong><span style="color: rgb(255, 0, 0);"> FilterChain chain) throws IOException, ServletException {  
  28. #       
  29. #           if (ignore || (request.getCharacterEncoding() == null)) {  
  30. #        String encoding = selectEncoding(request);  
  31. #        if (encoding != null) {  
  32. #         request.setCharacterEncoding(encoding);  
  33. #        }  
  34. #       }</span>  
  35. # </strong>      
  36. #       // Pass control on to the next filter  
  37. #       chain.doFilter(request, response);  
  38. #       
  39. #      }  
  40. #     public void init(FilterConfig filterConfig) throws ServletException {  
  41. #       
  42. #       this.filterConfig = filterConfig;  
  43. #       this.encoding = filterConfig.getInitParameter("encoding");  
  44. #       String value = filterConfig.getInitParameter("ignore");  
  45. #       if (value == null) {  
  46. #        this.ignore = true;  
  47. #       } else if (value.equalsIgnoreCase("true")) {  
  48. #        this.ignore = true;  
  49. #       } else if (value.equalsIgnoreCase("yes")) {  
  50. #        this.ignore = true;  
  51. #       } else {  
  52. #        this.ignore = false;  
  53. #       }  
  54. #       
  55. #      }  
  56. #       
  57. #      protected String selectEncoding(ServletRequest request) {  
  58. #       
  59. #       return (this.encoding);  
  60. #       
  61. #      }  
  62. #       
  63. #     }  
  文中红色的代码即为处理乱码的代码。
      web.xml文件加入过滤器

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


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

  1.    1. <Connector port="8080" maxHttpHeaderSize="8192"  
  2.    2.                maxThreads="150" minSpareThreads="25" maxSpareThreads="75"  
  3.    3.                enableLookups="false" redirectPort="8443" acceptCount="100"  
  4.    4.                connectionTimeout="20000" disableUploadTimeout="true" <span style="color: rgb(255, 0, 0);">URIEncoding="GBK"</span> />  


  * 注意修改后重新启动tomcat才能起作用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值