public class LoginFilter implements Filter{
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//将request强转成htt...
HttpServletRequest req = (HttpServletRequest) request;
//获取session
HttpSession ss = req.getSession();
//从session中获取user
if(ss.getAttribute("user")==null){
System.err.println("你还没有登录");
req.getSession().setAttribute("msg", "请你先登录");
//重定向到登录
HttpServletResponse resp = (HttpServletResponse) response;
resp.sendRedirect(req.getContextPath()+"/index.jsp");
}else{
//放行
chain.doFilter(request, response);
}
}
public void destroy() {
}
}
配置到web.xml中且对jsps/*进行过虑:
<filter>
<filter-name>login</filter-name>
<filter-class>cn.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>login</filter-name>
<url-pattern>/jsps/*</url-pattern>
<url-pattern>/views/*</url-pattern>
</filter-mapping>