SPRINGMVC Interceptor Implementation: When the user accesses the site resources, listen to whether the session expired one, interceptor configuration
<mvc:interceptors> <Mvc:interceptor> <mvc:mappingPath="/**"/> <mvc:exclude-mappingPath= "/user/login"/> <!--do not intercept login requests - <mvc:exclude-mappingPath= "/user/logout"/> <!--do not intercept logoff requests - <mvc:exclude-mappingPath= "*.jsp"/> <mvc:exclude-mappingPath= "*.html"/> <mvc:exclude-mappingPath= "*.js"/> <mvc:exclude-mappingPath= "*.css"/> <Beanclass= "Org.huaxin.interceptor.AccessInterceptor"></Bean> </Mvc:interceptor></mvc:interceptors>
Second, the Interceptor Code
Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object obj)throwsException {System.out.println ("[Accessinterceptor]:p rehandle Execution"); HttpSession Session=request.getsession (); ServletContext Application=Session.getservletcontext (); if(Application.getattribute (Session.getid ()) = =NULL){//not logged inPrintWriter out =Response.getwriter (); StringBuffer SB=NewStringBuffer ("<script type=\" text/javascript\ "charset=\" utf-8\ ">"); Sb.append ("Alert (\" Your account has been squeezed out, or not logged in, or the page has expired, please re-login \ ")"); Sb.append ("Window.location.href= '/user/logout ';"); Sb.append ("</script>"); Out.print (Sb.tostring ()); Out.close (); return false; }Else{//already logged in return true; } }
Three Summary 1. Note that the interceptor used here is handlerinterceptor, and your interceptor needs to implement this interface 2. In your login handler, to save the session to application, it is convenient to SessionID to determine if there is SESSION3.SB . Append ("window.location.href= '/user/logout ';"); This line of code is to say, perform the logout operation, in your/user/logout this handler inside must parse the page to the login page, easy to re-login
SPRINGMVC Interceptor Implementation: When the user accesses the site resources, monitoring session is expired