java - Spring MVC HandlerInterceptor : Redirect Failed -
i have created simple webmvc application - handler interceptor configured. interceptors responsibility simple - should check if valid session exists in httprequest - , if true, redirect registration page.
the issue encounter on redirect - browser throwing message:
the page isn't redirecting properly
firefox has detected server redirecting request address in way never complete.
the interceptors code follows:
public class logininterceptor extends handlerinterceptoradapter{ // used checking session management user. @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception { system.out.println(" <interceptor> - pre handle"); return true; } @override public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws exception { system.out.println(" <interceptor> - post handle"); httpsession session = request.getsession(); user user = (user) session.getattribute("user"); system.out.println(" <interceptor> - session id --> " + session.getid()); system.out.println(" <interceptor> - session create time --> " + session.getcreationtime()); system.out.println(" <interceptor> - session last accessed time --> " + session.getlastaccessedtime()); try { system.out.println(" <interceptor> - user info --> " + user.tostring()); }catch(exception e){ e.tostring(); } try{ if(session != null){ response.sendredirect("register"); return; } }catch(exception e){ e.tostring(); } } public void aftercompletetion(){ system.out.println(" <interceptor> - after completion"); }
}
i have attempted append full request context path redirect - root context - both of fail. cannot see failing on - appreciated.
whatever interceptor have written come picture requests.
so when redirect /register
time reach interceptor find session
not null , again redirect..and same thing continues...infinite redirection same request.
@override public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws exception { httpsession session = request.getsession(); try{ if(session != null){ //infinite response.sendredirect("register"); return; } }catch(exception e){ e.tostring(); } }
so make sure intercept 1 request configuration
or code
if((request.getrequesturi().equals(contextpath+"/someuri") && session != null){ response.sendredirect("register"); }
then check particular request , redirect register.it wont fell in infinite loop.
Comments
Post a Comment