public class SignonFilter implements Filter
{
String LOGIN_PAGE="login.jsp";
protected FilterConfig filterConfig;
//过滤处理的方法
public void doFilter(final ServletRequest req,final ServletResponse res,FilterChain chain)throws IOException,ServletException
{
HttpServletRequest hreq = (HttpServletRequest)req;
HttpServletResponse hres = (HttpServletResponse)res;
HttpSession session = hreq.getSession();
String isLogin="";
try
{
isLogin=(String)session.getAttribute("isLogin");
System.out.print(isLogin);
if(isLogin!=null&&isLogin.equals("true"))
{
System.out.println("在SignonFilter中验证通过");
//验证成功,继续处理
chain.doFilter(req,res);
}
else
{
//验证不成功,让用户登录。
hres.sendRedirect("login.jsp");
System.out.println("被SignonFilter拦截一个未认证的请求");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setFilterConfig(final FilterConfig filterConfig)
{
this.filterConfig=filterConfig;
}
//销毁过滤器
public void destroy()
{
this.filterConfig=null;
}
/**
*初始化过滤器,和一般的Servlet一样,它也可以获得初始参数。
*/
public void init(FilterConfig config) throws ServletException {
this.filterConfig = config;
}
}