判断session是否存在的过滤器
在web.xml中有如下配置:
<filter><filter-name>myFilter</filter-name><filter-class>com.test.MyFilter</filter-class></filter><filter-mapping><filter-name>myFilter</filter-name><url-pattern>*.jsp</url-pattern></filter-mapping>
public class MyFilterimplements Filter {protected FilterConfig filterConfig;public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {HttpServletRequest hreq = (HttpServletRequest) req;HttpServletResponse hres = (HttpServletResponse) res;String requestUrl = hreq.getRequestURI();if (requestUrl.indexOf("index.jsp") > -1) {chain.doFilter(req, res);} else {if (null == hreq.getSession().getAttribute("session_user"))hres.sendRedirect(hreq.getContextPath()+ "/"+"index.jsp");elsechain.doFilter(req, res);}}public void init(FilterConfig filterConfig) throws ServletException {this.filterConfig = filterConfig;}public void destroy() {this.filterConfig = null;}public void setFilterConfig(FilterConfig filterConfig) {this.filterConfig = filterConfig;}}