解决JSP提交表单保存到Mysql中文乱码的问题

JSP网页即使写成

<%@ page contentType= “text/html; charset=UTF-8 ” language= “java ” import= “java.sql.* ” errorPage= “error.jsp ” %>

Mysql里存取的数据库的charset也设成了UTF-8,可是通过JSP网页存取中文还是显示乱码 。解决的办法就是加一个过滤器:

 


  1. package cn.cate.utils;          
  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.     protected FilterConfig filterConfig = null;           
  16.     protected boolean ignore = true;           
  17.           
  18.     @Override          
  19.     public void destroy() {          
  20.         this.encoding = null;           
  21.         this.filterConfig = null;           
  22.     }          
  23.           
  24.     @Override          
  25.     public void doFilter(ServletRequest request, ServletResponse response,          
  26.             FilterChain chain) throws IOException, ServletException {          
  27.                   
  28.         if (ignore || (request.getCharacterEncoding() == null)) {           
  29.             String encoding = selectEncoding(request);           
  30.             if (encoding != null) {           
  31.                 request.setCharacterEncoding(encoding);          
  32.             }           
  33.         }           
  34.         chain.doFilter(request, response);           
  35.     }          
  36.           
  37.     @Override          
  38.     public void init(FilterConfig filterConfig) throws ServletException {          
  39.         this.filterConfig = filterConfig;           
  40.         this.encoding = filterConfig.getInitParameter("encoding");           
  41.         String value = filterConfig.getInitParameter("ignore");           
  42.         if (value == null) {           
  43.           this.ignore = true;           
  44.         }           
  45.         else if (value.equalsIgnoreCase("true")) {           
  46.           this.ignore = true;           
  47.         }           
  48.         else if (value.equalsIgnoreCase("yes")) {           
  49.           this.ignore = true;           
  50.         }           
  51.         else {           
  52.           this.ignore = false;           
  53.         }           
  54.     }          
  55.           
  56.     protected String selectEncoding(ServletRequest request) {           
  57.         return (this.encoding);           
  58.     }           
  59. }      

然后在web.xml作如下配置:

 


  1. XML/HTML 代码复制内容到剪贴板  
  2.       
  3. <filter>           
  4.   <filter-name>Set Character Encoding</filter-name>           
  5.   <filter-class>cn.cate.utils.SetCharacterEncodingFilter</filter-class>           
  6.   <init-param>           
  7.     <param-name>encoding</param-name>           
  8.     <param-value>UTF-8</param-value>           
  9.   </init-param>           
  10. </filter>           
  11. <filter-mapping>           
  12.   <filter-name>Set Character Encoding</filter-name>           
  13.   <url-pattern>/*</url-pattern>           
  14. </filter-mapping>       

 

     本文转自06peng 51CTO博客,原文链接:http://blog.51cto.com/06peng/962462,如需转载请自行联系原作者






上一篇:Android系列之UI组件----Menu菜单


下一篇:我的测试历程---用例设计思路(安装/卸载)