过滤器从url开始过滤,即便程序入口是html页面也能完成跳转
核心代码
package com.cyboil.utils;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class SessFilter
implements Filter
{
public void init(FilterConfig filterConfig)
throws ServletException
{
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
System.out.println("过滤器启动!!");
HttpServletRequest servletRequest = (HttpServletRequest)request;
HttpServletResponse servletResponse = (HttpServletResponse)response;
HttpSession session = servletRequest.getSession();
String path = servletRequest.getRequestURI();
System.out.println("path==================" + path);
String empId = (String)session.getAttribute("userName");
System.out.println("empId=================" + empId);
if (path.indexOf("AnalogLogin") > -1) {
chain.doFilter(servletRequest, servletResponse);
return;
}
if (empId == null)
{
servletResponse.sendRedirect("http://localhost:46623/Login.aspx");
}
else
chain.doFilter(request, response);
}
public void destroy()
{
}
}
web.xml配置