zoukankan      html  css  js  c++  java
  • 采用Filter的方法解决Servlet的编码问题

    这样比你自己在Servlet代码中硬编码request.setCharacterEncoding, response.setCharacterEncoding方便多了

    总之,如果你添加了这个filter,配置好了web.xml,那么如果还出现乱码问题,你就去检查你的JSP和HTML代码中的encoding选项吧(charset, pageEncoding, meta.content之类的),看看是否和你在web.xml中配置的filter的encoding相匹配

    CharacterEncodingFilter.java

     1 public class CharacterEncodingFilter implements Filter {
     2     protected String encoding = null; 
     3     protected FilterConfig filterConfig = null; 
     4     protected boolean enable = false; 
     5     public void destroy() { 
     6         this.encoding = null; 
     7         this.filterConfig = null; 
     8     } 
     9     public void doFilter(ServletRequest request, ServletResponse response, 
    10                            FilterChain chain) throws IOException, ServletException { 
    11         if (this.enable) {
    12             String encoding = selectEncoding(request); 
    13             if (encoding != null && !encoding.equals("")) { 
    14                 System.out.println(this + ": ignore = true " + encoding);
    15                 request.setCharacterEncoding(encoding); //Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader().
    16                 response.setCharacterEncoding(encoding);
    17             }
    18         }
    19         // Pass control on to the next filter 
    20         chain.doFilter(request, response); 
    21      } 
    22      public void init(FilterConfig filterConfig) throws ServletException { 
    23          this.filterConfig = filterConfig; 
    24          this.encoding = filterConfig.getInitParameter("encoding");
    25          // 
    26          String enable = filterConfig.getInitParameter("enable");
    27          if (enable.equalsIgnoreCase("true")) {
    28             this.enable = true;
    29          } else {
    30             this.enable = false;
    31          }
    32       } 
    33       protected String selectEncoding(ServletRequest request) { 
    34           return (this.encoding); 
    35       } 
    36 }

    web.xml

    <filter> 
             <filter-name>charset-encoding</filter-name> 
             <filter-class>cn.mldn.lxh.encoding.filter.CharacterEncodingFilter</filter-class> 
             <init-param> 
                 <param-name>encoding</param-name> 
                 <param-value>UTF-8</param-value> 
             </init-param> 
             <init-param> 
                 <param-name>enable</param-name> 
                 <param-value>true</param-value> 
             </init-param> 
    </filter>

    <filter-mapping> <filter-name>charset-encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
  • 相关阅读:
    C++程序设计入门--前言
    C++ string_view 的坑
    从OGRE,GAMEPLAY3D,COCOS2D-X看开源
    抽烟解闷的程序员
    一个团队应该是什么样
    准备开始接手公司的项目
    两位印象深刻的同事
    一段故事结束,一段生活开始
    starling性能优化总结(毫无疑问还是转载)
    知道端口号如何查看应用位置
  • 原文地址:https://www.cnblogs.com/qrlozte/p/3177270.html
Copyright © 2011-2022 走看看