参考:http://lym6520.iteye.com/blog/871092
1.Ext增加如下代码
Ext.Ajax.defaultHeaders = { 'Request-By': 'Ext' // 标识ajax请求 }; Ext.Ajax.on('requestcomplete',function(conn,response,options){ var json = Ext.decode(response.responseText); if(typeof json == 'object' && !json.success && json.timeout){ alert('<fmt:message key="msgBox.loginerror.timeout"/>'); window.location.href = 'login.jsp'; } });
2.Filter代码:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { List<String> list = new ArrayList<String>(); list.add(loginUrl); list.add("/login"); HttpServletRequest httpServletRequest = (HttpServletRequest) request; HttpServletResponse servletResponse = (HttpServletResponse) response; String path = httpServletRequest.getServletPath(); User user = (User) httpServletRequest.getSession().getAttribute("user"); if (!list.contains(path) && user == null) { String ext = httpServletRequest.getHeader("Request-By"); if (ext != null && ext.equalsIgnoreCase("Ext")) { servletResponse.addHeader("sessionstatus", "timeout"); PrintWriter out = servletResponse.getWriter(); out.print("{success:false,timeout:true}"); out.flush(); out.close(); } else { servletResponse.sendRedirect(httpServletRequest.getContextPath() + loginUrl); } } chain.doFilter(request, response); }
3.也可以直接判断是否为Ajax提交
String head = httpRequest.getHeader("x-requested-with"); if (head != null && !(head.equalsIgnoreCase("XMLHttpRequest"))) { //Ajax提交 }
相关推荐
通过这种方式,EXTJS 4.1 MVC应用能够智能地处理动态加载控制器,同时优雅地处理Session过期的情况,确保用户有一个顺畅的使用体验。动态加载控制器和Session管理是构建大型、可扩展Web应用的基础,它们有助于提高...
在EXTJS中,你可能遇到的问题是,session过期后,某些功能可能不再正常工作,尤其是对于那些在session过期前未被触发的功能。 为了解决这个问题,后端需要做以下判断: 1. 检查请求头中的`accept`字段,通常Ajax...
在本文中,我们将探讨如何在Session过期后自动将用户重定向到登录页面。 首先,开发者可能尝试使用Session监听器(HttpSessionListener)来检测Session的过期。监听器是Java Servlet规范的一部分,允许我们注册监听...
在Web开发中,Session是服务器用来跟踪用户状态的一种机制,特别是在多用户同时访问的应用中,如EXT...这种机制确保了用户的会话安全,防止了未经授权的访问,并且能够优雅地处理Session过期的情况,提高用户体验。
然而,当用户在一定时间内没有与服务器交互,Session可能会因为过期而失效,这被称为Session超时。在AJAX(Asynchronous JavaScript and XML)请求中,如果Session超时,通常需要有合适的机制来处理这种情况,以免...
默认情况下,Session在1800秒无活动后会自动销毁,但可以通过`session.setMaxInactiveInterval(int interval)`设置不同的过期时间。调用`session.invalidate()`会立即结束Session。需要注意的是,关闭浏览器并不直接...
4. **Session安全性**:保护Session安全包括使用HTTPS、避免Session劫持和固定ID,以及定期清理过期Session。 5. **数据库事务**:事务确保数据一致性,由一系列操作组成,要么全部成功,要么全部回滚。在.NET中,...
设置UniGUI的超时时间可以通过调整服务器端的配置来实现,以防止长时间未操作导致的会话过期。 #### 45. UniGUI如何实现登陆页面 实现UniGUI的登陆页面通常包括: - **设计界面**:设计登录表单的UI。 - **验证...