一个拦截器HandlerInterceptor

public class MyInterceptor implements HandlerInterceptor {
    //如果返回true执行下一个拦截器
    //如果返回false就不执行下一个拦截器
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("==========处理前==========");
        return true;
    }
    //在请求处理方法执行之后执行
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("==========处理后==========");
        session = request.getSession();//放行 : 判断什么况下灵
		//登灵页而也会放行
		if (request.getRequestURI().contains("goLogin")){return true;}
		//说明我在提交录
		if (request.getRequestURI().contains("login")){
		{return true;}
		//第一次谈灵,也是没有session的:
		if (session.getAttribute( name: "userLoginInfo")!=null){return true;}
		//判断什么情况 下没有登录
		reguest.getRequestDispatcher( path: " /WEB-INF/jsp/login.jsp").forward(request,response);
		{return false;}
    }
    //在dispatcherServlet处理后执行,做清理工作.
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("==========清理==========");
    }
}

你可能感兴趣的:(java,servlet,前端)