第一步,配置过滤。
<filter>
<filter-name>accessControl</filter-name>
<filter-class>com.chgr.util.AccessControlFilter</filter-class>
<init-param>
<param-name>sessionOut</param-name>
<param-value>/pages/session_out.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>accessControl</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
第二步,创建过滤器类
public class AccessControlFilter implements Filter {
protected FilterConfig filterConfig;
private String sessionOut;
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
if(Util.isLogin(request) || (request.getRequestURI() != null && request.getRequestURI().equals("/as400/login.do"))){
arg2.doFilter(request, response);
} else {
response.sendRedirect(sessionOut);
}
}
public void init(FilterConfig arg0) throws ServletException {
this.filterConfig = arg0;
sessionOut= filterConfig.getInitParameter("sessionOut");;
if (sessionOut == null){
throw new ServletException("loginPage init param missing");
}
}
}