package fkpi.ilter;
import
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginFilter extends HttpFilter{
@Override
protected void doFilter(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) {
if(req.getSession().getAttribute("userName")==null){//拿取到sesson判断,如果空了
try {
resp.sendRedirect(req.getContextPath()+"/index.jsp");//跳转到首页
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
chain.doFilter(req, resp);//如果不空,说明已经登录了,就直接放行
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}