在web.xml中有如下配置:
凡是访问以jsp为后缀的页面都会经过过滤器com.test.MyFilter,代码如下:
<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>
凡是访问以jsp为后缀的页面都会经过过滤器com.test.MyFilter,代码如下:
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");
else
chain.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;
}
}