少林

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

package com.platform.common.web.servlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;


public class SetCharEncodingFilter implements Filter {

    protected FilterConfig filterConfig = null;
    protected String encoding = "UTF-8";
    public void destroy() {
        filterConfig = null;
        encoding = null;
    }
 
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
            FilterChain filterChain) throws IOException, ServletException {
       /* if(encoding != null && !"".equals(encoding))
            servletRequest.setCharacterEncoding(encoding);*/
        
        servletRequest.setCharacterEncoding("utf-8");
        //servletResponse.setContentType("text/html;charset=utf-8");
        filterChain.doFilter(servletRequest, servletResponse);
    }
 
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
    }

}

 

web.xml:

<filter>
      <filter-name>Set Character Encoding</filter-name> 
      <filter-class>com.platform.common.web.servlet.SetCharEncodingFilter</filter-class> 
     </filter> 
     <filter-mapping> 
         <filter-name>Set Character Encoding</filter-name>
         <url-pattern>/*</url-pattern> 
     </filter-mapping>

 

posted on 2016-02-22 17:36  Stanley_yu  阅读(1547)  评论(0编辑  收藏  举报