获取异常 - 在提交响应后无法调用sendRedirect()

时间:2014-01-09 19:54:27

标签: java spring-mvc login

我正在为登录帮助编写拦截器。

public class LoginInterceptor extends HandlerInterceptorAdapter {

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

    HttpSession session = request.getSession();
    System.out.println("GreetingInterceptor: REQUEST Intercepted for URI: " + request.getRequestURI());
    System.out.println(session.getAttribute("userName"));

    /*if(request.getRequestURI().equalsIgnoreCase("/studentenrollmentsystem/login")){
        return true;
    }
    if(request.getRequestURI().equalsIgnoreCase("/studentenrollmentsystem/loginaction")){
        return true;
    }*/

    if(session.getAttribute("userName") != null){
        if(request.getRequestURI().equalsIgnoreCase("/studentenrollmentsystem/login")){
            response.sendRedirect("loginaction");
            return true;
        }
        else{
            return true;
        }
        /*if(request.getRequestURI().equalsIgnoreCase("/studentenrollmentsystem/loginaction")){
            return true;
        }
        return true;*/
    }
    else{
        if(request.getRequestURI().equalsIgnoreCase("/studentenrollmentsystem/login")
                || request.getRequestURI().equalsIgnoreCase("/studentenrollmentsystem/loginaction")){
            return true;
        }
        else{
            response.sendRedirect("login");
            return false;
        }
    }

}

}

当我第一次去/登录时工作正常,但是在登录后我回到/ login它给了我异常“在提交响应后无法调用sendRedirect()”。你能帮忙吗?还有一种更好的方法来维护登录。

0 个答案:

没有答案