登陆验证案例
- 需求:
- 访问案例的资源,验证其是否登录
- 如果登陆了则直接放行
- 如果没有登录,则跳转到登录页面,提示"您尚未登录,请先登录"
代码实现:
package com.ailyt.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@WebFilter("/*")
public class LoginFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
String requestURI = request.getRequestURI();
if (requestURI.contains("/login.jsp") || requestURI.contains("/loginServlet")) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
request.setAttribute("login_msg","您尚未登陆");
request.getRequestDispatcher("/login.jsp").forward(servletRequest, servletResponse);
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
标签:HttpServletRequest,登录,验证,request,案例,登陆,import,servletRequest,public
From: https://www.cnblogs.com/ailhy/p/16598181.html