ext的session超时如何实现

具体的实现步骤:
一、定义一个public class SessionCheckFilter implements Filter,并在doFilter方法中

public void doFilter(ServletRequest arg0, ServletResponse arg1,  
            FilterChain arg2) throws IOException, ServletException {  
        HttpServletRequest request = (HttpServletRequest) arg0;  
        HttpServletResponse response = (HttpServletResponse) arg1;  
  
        String currentURL = request.getRequestURI();  
  
        String targetURL = currentURL.substring(currentURL.indexOf("/", 1),  
                currentURL.length());  
  
        HttpSession session = request.getSession(false);  
  
        if (!"/index.jsp".equals(targetURL)) {  
            if (session == null || session.getAttribute("user") == null) {  
//              System.out.println("request.getContextPath()="  
//                      + request.getContextPath());  
                if (request.getHeader("x-requested-with") != null  
                        && request.getHeader("x-requested-with").equalsIgnoreCase(  
                                "XMLHttpRequest")) {  
                    response.setHeader("sessionstatus", "timeout");  
                }  
        //      response.sendRedirect(request.getContextPath() + "/login.jsp");  
        //      return;  
            }  
        }  
  
        arg2.doFilter(request, response);  
    }

 二、在web.xml中增加

 

<filter>
		<filter-name>AuthFilter</filter-name>
		<filter-class>com.sp.util.SessionCheckFilter</filter-class>
	</filter>
  
	<filter-mapping>
		<filter-name>AuthFilter</filter-name>
		<url-pattern>*.jsp</url-pattern>
	</filter-mapping>

 

三、在main.js中增加

Ext.Ajax.on('requestcomplete',checkUserSessionStatus, this);    
    function checkUserSessionStatus(conn,response,options){    
        var sessionStatus = response.getResponseHeader("sessionstatus");  
        if(typeof(sessionStatus) != "undefined"){    
            Ext.Msg.alert('提示', '会话超时,请重新登录!', function(btn, text){  
               if (btn == 'ok'){  
                    var redirect = '../logout';  
                    window.location = redirect;  
                }  
            });  
         }    
    }; 

 

你可能感兴趣的:(jsp,Ajax,Web,xml,ext)