大家好,又见面了,我是你们的朋友全栈君。
首先创建一个FilterServlet类 下面代码
package com.group.users.util;
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;
import com.group.users.entity.Users;
public class FilterServlet implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession();
Users user = (Users) session.getAttribute(“users”);
if(user!=null){
chain.doFilter(request, response);
}else{
request.getRequestDispatcher(“/index.jsp”).forward(request, response);
}
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
下面wed代码
FilterServlet
com.group.users.util.FilterServlet
FilterServlet
/admin/*
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/136566.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...