过滤器类
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.MediaType;
import com.alibaba.fastjson.JSON;
public class SJFilter implements Filter {
private final Log logger = LogFactory.getLog(this.getClass());
private static Map<String, SjjlInterceptionobject> allowUriMap = new HashMap<String, SjjlInterceptionobject>();
public SJFilter() {
}
public void destroy() {
}
// 正式内容从这里开始(需要做什么写什么,这里是顺便记录一下获取路径的方法)
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
// 首先获取请求路径
HttpServletRequest request2 = (HttpServletRequest) request;
String uri = request2.getRequestURI();
……
// do Something后 allowUriMap里面是需要拦截的路径
//判断该请求是否是拦截对象
if (allowUriMap.containsKey(uri)) {
//是的话do Something
……
}
//执行该方法,回归到该执行的请求
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
xml文件中的配置
<filter>
<filter-name>SJFilter</filter-name>
<filter-class>com.test.SJFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SJFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>