环境:JDK1.7 TOMCAT7 spring3.2

 1.过滤器代码

    1.1基于spring过滤器

import java.io.IOException;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.filter.OncePerRequestFilter;public class SessionFilter extends OncePerRequestFilter {    @Override    protected void doFilterInternal(HttpServletRequest request,            HttpServletResponse response, FilterChain filterChain)            throws ServletException, IOException {            ParameterRequestWrapper requestWrapper = new ParameterRequestWrapper(request);            filterChain.doFilter(requestWrapper, response);    }}

   1.2基于javax.servlet.Filter

Java代码  

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;import javax.servlet.http.HttpServletRequest;/** * Servlet Filter implementation class CommonFilter */public class CommonFilter implements Filter {    /**     * Default constructor.      */    public CommonFilter() {        // TODO Auto-generated constructor stub    }    /**     * @see Filter#destroy()     */    public void destroy() {        // TODO Auto-generated method stub    }    /**     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)     */    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {        // TODO Auto-generated method stub        // place your code here                HttpServletRequest hr=(HttpServletRequest) request;                  String url = hr.getServletPath().trim();                if (url.equals("xxx")){//不需要过滤的url,这里可以使用一个配置文件配置这些url,项目启动时读入内存一个map中,然后在这里进行判断            //我定义的是urlFilterMap,然后在这里urlFilterMap.containsValue(url)进行判断                        chain.doFilter(hr, response);                }else{                   ParameterRequestWrapper requestWrapper = new ParameterRequestWrapper((HttpServletRequest)request);           chain.doFilter(requestWrapper, response);                }    }    /**     * @see Filter#init(FilterConfig)     */    public void init(FilterConfig fConfig) throws ServletException {        // TODO Auto-generated method stub    }}

 2.过滤器包装类 Java代码  

import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;public class ParameterRequestWrapper extends HttpServletRequestWrapper {        private Map
 params = new HashMap
();    @SuppressWarnings("unchecked")    public ParameterRequestWrapper(HttpServletRequest request) {        // 将request交给父类,以便于调用对应方法的时候,将其输出,其实父亲类的实现方式和第一种new的方式类似        super(request);        //将参数表,赋予给当前的Map以便于持有request中的参数        this.params.putAll(request.getParameterMap());        this.modifyParameterValues();    }    //重载一个构造方法    public ParameterRequestWrapper(HttpServletRequest request , Map
 extendParams) {        this(request);        addAllParameters(extendParams);//这里将扩展参数写入参数表    }        public void modifyParameterValues(){//将parameter的值去除空格后重写回去        Set
 set =params.keySet();        Iterator
 it=set.iterator();        while(it.hasNext()){           String key= (String) it.next();           String[] values = params.get(key);           values[0] = values[0].trim();           params.put(key, values);         }    }        @Override    public String getParameter(String name) {//重写getParameter,代表参数从当前类中的map获取        String[]values = params.get(name);        if(values == null || values.length == 0) {            return null;        }        return values[0];    }        public String[] getParameterValues(String name) {//同上         return params.get(name);    }   public void addAllParameters(Map
otherParams) {//增加多个参数        for(Map.Entry
entry : otherParams.entrySet()) {            addParameter(entry.getKey() , entry.getValue());        }    }    public void addParameter(String name , Object value) {//增加参数        if(value != null) {            if(value instanceof String[]) {                params.put(name , (String[])value);            }else if(value instanceof String) {                params.put(name , new String[] {(String)value});            }else {                params.put(name , new String[] {String.valueOf(value)});            }        }    }}

3.web.xml配置

3.1 基于spring 配置

    
sessionFilter
    
com.cnpc.oms.common.service.SessionFilter
    
sessionFilter
    
/*

 3.2基于javax.serlvet.Filter

 
    
commonFilter
    
com.cnpc.oms.common.service.CommonFilter
    
commonFilter
    
/*

参考:http://zhangzhaoaaa.iteye.com/blog/1853787