Recent projects using jquery Easyui Development, session timeout, no login direct access to the page need to return to the login page special processing.
Description: Test Setup failure time is 1 minutes, login success after 1 minutes to click Action Penalty event, perform ① connection to com.lactec.framework.ac.filter.AuthFilter this class
Determines which action action to perform. To determine if the session is empty, the timeout is returned, I use the AJAX request: If it is the AJAX request response header will have, X-requested-with;
Settings: Rep.setheader ("Sessionstatus", "timeout"),//Setting session status in response header
Rep.getwriter (). Print ("Login timed out!") "); Print a return value, no this line, in the tabs page can not jump out (the navigation bar to jump out), the specific reason is unclear
Front section through xmlhttprequest.getresponseheader ("Sessionstatus"),//through the XMLHttpRequest to obtain the response head, give a hint.
For the project on-line, will involve multiple servers, so jump to the server IP to jump to the problem, solution: Window.location.host
Code section: ①web. XML partial Configuration Code
<!--filter permission authentication only for AMF requests
<filter>
<filter-name>authFilter</filter-name>
<filter-class>com.lactec.framework.ac.filter.AuthFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>authFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!--session Expiration time is 30 minutes--
<session-config>
<session-timeout>30</session-timeout>
</session-config>
②java Code
public class AuthFilter extends Baseaction implements Filter {
public void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, servletexception {
Login to judge if it is/crm_web/loginaction.do login jump
HttpServletRequest req = (httpservletrequest) request;
HttpServletResponse rep = (HttpServletResponse) response;
System.out.println (Req.getrequesturi ());
String uri = Req.getrequesturi ();//Get Login action
if (Uri.indexof ("Loginaction") >=0) {
Chain.dofilter (request, response);//Jump page
}else{
Get login User's session--Basic permission check, user not logged in, blocked or session timeout please login again
if (Utils.isnull (Req.getsession ()) | | Utils.isnull (Req.getsession (). getattribute ("user")) {
If it is the AJAX request response header will have, X-requested-with;
if (Req.getheader ("X-requested-with")! = null && req.getheader ("X-requested-with"). Equalsignorecase (" XMLHttpRequest "))
{
Rep.setheader ("Sessionstatus", "timeout");//setting session status in the response header
Rep.getwriter (). Print ("Login timed out!") "); Print a return value, no this line, in the tabs page can not jump out (the navigation bar to jump out), the specific reason is unclear
}
Return
}else {
Chain.dofilter (request, response);//Jump page
}
}
}
}
③JS Code Section
$.ajaxsetup ({
Error:function (XMLHttpRequest, Textstatus, Errorthrown) {
if (xmlhttprequest.status==403) {
$.messager.alert (' My message ', ' You do not have permission to access this resource or do this! ', ' success ');
return false;
}
},
Complete:function (xmlhttprequest,textstatus) {
var sessionstatus=xmlhttprequest.getresponseheader ("Sessionstatus"); Get the response head through XMLHttpRequest, Sessionstatus,
if (sessionstatus== ' timeout ') {
If the timeout is processed, specify the page to jump to
var top = Gettopwinow (); Gets the top-level window object of the current page
$.messager.alert (' My message ', ' Login timeout-please login again! ', ' info ');
Top.location.href = "http:/" +window.location.host+ "/crm_web/login_new.html"; Jump to landing page
}
}
});
/**
* Get top-level windows in any nested level window in the page
* @return The top-level window object of the current page
*/
function Gettopwinow () {
var p = window;
while (P! = p.parent) {
p = p.parent;
}
return p;
}
If you have any questions, you can reply to me directly .....
JQuery Easyui Session Timeout returns to the login page