1、案例实现如果访问任何资源,如果没有登录统一切换到登录界面,并提示信息请先登录
主要代码展示:
LoginFilter.java
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@WebFilter("/*")
public class LoginFilter implements Filter {
public void init(FilterConfig config) throws ServletException {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest)req;
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
// System.out.println(requestURI);
// System.out.println(requestURL);
if(requestURI.contains("/login.jsp")||requestURI.contains("/loginServlet")
||requestURI.contains("/css/")||requestURI.