<!-- 过滤器拦截指定后缀-->
<!-- //localhost:8080/sanzu/test/****都会被拦截 -->
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>com.jk.login.filter.MyFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>contentType</param-name>
<param-value>text/html;charset=UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<!-- 要拦截的哪一级目录 -->
<url-pattern>/test/*</url-pattern>
</filter-mapping>
<!-- 配置session超时时间,单位分钟 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
web-info/web.xml配置,并配置相应的interceptor类
相应的过滤器类
package com.jk.login.filter;
import java.io.IOException;
import java.io.PrintWriter;
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.HttpServletResponse;
import org.apache.log4j.Logger;
public class MyFilter implements Filter{
private static final Logger logger = Logger.getLogger(MyFilter.class);
public void destroy() {
System.out.println("销毁过滤器方法");
logger.info("销毁过滤器方法");
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request=(HttpServletRequest) req;
String servletPath = request.getServletPath();
logger.info("请求路径过滤信息路径为:" + servletPath);
//访问login.jsp时,才放过,并且login.jsp的后续操作,继续执行,不拦截
if (servletPath.equals("/login.jsp") ) {
chain.doFilter(req, res);
return;
}else {
String contextPath=request.getContextPath();
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
//否则拦截,跳转指定的页面
pw.print("<script>window.top.location.href='"+contextPath + "/aa.jsp'"+";</script>");
}
}
public void init(FilterConfig arg0) throws ServletException {
System.out.println("初始化过滤器的方法");
logger.info("初始化过滤器的方法");
}
}