session过期后自动跳转到登陆页
1.在项目的web.xml文件中添加如下代码:
<!--添加Session监听器-->
<listener>
<listener-class> 监听器路径 </listener-class>
</listener>
2.编写java类。
public class SessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent arg0) {
// session创建时执行
SimpleDateFormat simpleFormat = new SimpleDateFormat("mm-ss-ms");
String nowtimes = simpleFormat.format(new Date());
User u=null;
//System.out.println("执行。。 当前时间:"+nowtimes+"_"+u);
HttpSession ses= arg0.getSession();
String id=ses.getId()+"_"+ses.getCreationTime();
}
public void sessionDestroyed(HttpSessionEvent arg0) {
// session失效时执行
SimpleDateFormat simpleFormat = new SimpleDateFormat("mm-ss-ms");
String nowtimes = simpleFormat.format(new Date());
//System.out.println("session失效了。。 结束时间: "+nowtimes);
}
}
配置完成后等session失效后成功进入sessionDestroyed方法,准备进行页面跳转操作,突然发现怎么写跳转,愣住了,继续上网请教大神,发现这个监听是做一些后台统计处理的,无法实现页面跳转的功能。
只能放弃这方法了,开始使用过滤器实现
1、web.xml中添加过滤器配置
<filter>
<filter-name>sessionFilter</filter-name>
<filter-class>com.orchestrall.web.helper.session.SessionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sessionFilter</filter-name>
<url-pattern>/actions/*</url-pattern>
</filter-mapping>
2、新建SessionFilter类,实现Filter接口。
public class SessionFilterimplements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
HttpSession session = httpRequest.getSession();
// 登陆url
String loginUrl = httpRequest.getContextPath() + "/admin/login.jsp";
String url = httpRequest.getRequestURI();
String path = url.substring(url.lastIndexOf("/"));
// 超时处理,ajax请求超时设置超时状态,页面请求超时则返回提示并重定向
if (path.indexOf(".action") != -1
&& session.getAttribute("LOGIN_SUCCESS") == null) {
// 判断是否为ajax请求
if (httpRequest.getHeader("x-requested-with") != null
&& httpRequest.getHeader("x-requested-with")
.equalsIgnoreCase("XMLHttpRequest")) {
httpResponse.addHeader("sessionstatus", "timeOut");
httpResponse.addHeader("loginPath", loginUrl);
chain.doFilter(request, response);// 不可少,否则请求会出错
} else {
String str = "<script language='javascript'>alert('会话过期,请重新登录');"
+ "window.top.location.href='"
+ loginUrl
+ "';</script>";
response.setContentType("text/html;charset=UTF-8");// 解决中文乱码
try {
PrintWriter writer = response.getWriter();
writer.write(str);
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
chain.doFilter(request, response);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
3、客户端JS,用于ajax请求session超时
对于jquery
<script type="text/javascript">
$(document).ajaxComplete(function(event, xhr, settings) {
if(xhr.getResponseHeader("sessionstatus")=="timeOut"){
if(xhr.getResponseHeader("loginPath")){
alert("会话过期,请重新登陆!");
window.location.replace(xhr.getResponseHeader("loginPath"));
}else{
alert("请求超时请重新登陆 !");
}
}
});
</script>
对于extjs的ajax请求
Ext.Ajax.on('requestcomplete',checkUserSessionStatus, this);
function checkUserSessionStatus(conn,response,options){
if(response.getResponseHeader("sessionstatus") == 'timeout'){
if(response.getResponseHeader("loginPath")){
alert("会话过期,请重新登陆!");
window.top.location.href = response.getResponseHeader("loginPath");
}else{
alert("请求超时请重新登陆 !");
}
}
}
如果使某个ajax请求不受全局方法的影响,那么可以在使用$.ajax()方法时,将参数中的global设置为false,jquery代码如下:
$.ajax({
url:"test.html",
global:false//不触发全局ajax事件
})
session过期后自动跳转到登陆页的更多相关文章
- 当session过期后自动跳转到登陆页而且会跳出iframe框架
写项目时在重定向后一直存在一个问题就是重定向后登陆页面会出现在跳出的子框架里.
- Session过期后自动跳转到登录页面的实例代码
1.在项目的web.xml文件中添加如下代码: ? 1 2 3 4 <!--添加Session监听器--> <listener> <listener-class> ...
- Session过期后自动跳转到登录页面
最近研究如果用原生的Filter来判别session存在否或者过期否.来跳转到的页面实例,下载来展示代码. 因为顾虑器是每次请求能会进入的,所以可以设置了,进行拦截判断 1.配置web.xml < ...
- 解决ajax 遇到session失效后自动跳转的问题
在项目中,经常会遇到session失效后,点击任何链接无反应的情况!这样给客户的体验就不是很好,以为是系统出了故障!所以在项目中我们会处理session失效后的跳转问题(一般给用户提示,并跳转后登录页 ...
- 利用.Net自带的票据完成BaseController的未登陆自动跳转到登陆页功能
一:定义票据中要记录的字段类 /// <summary> /// 用户存在于浏览器端的身份票据(非持久) /// 非持久 FormsAuthenticationTicket 的isPers ...
- JS 控制页面超时后自动跳转到登陆页面
<span style="font-size: small;"><script language="javascript"> var m ...
- MVC 访问IFrame页面Session过期后跳转到登录页面
Web端开发时,用户登录后往往会通过Session来保存用户信息,Session存放在服务器,当用户长时间不操作的时候,我们会希望服务器保存的Session过期,这个时候,因为Session中的用户信 ...
- JS n秒后自动跳转实例
<p><a href="<?php echo base_url();?>usercenter/index" id="message" ...
- 关于使用struts2时子窗体页面跳转后在父窗体打开的问题以及Session过期后的页面跳转问题
问题1:传统的系统界面,iframe了三个页面,上,左,右,用户点击注销的按钮在上面得top.jsp里面,方法:<a href="../adminAction/admin_logout ...
随机推荐
- 防止sql注入 参数化解决方案
StringBuilder strSql=new StringBuilder(); strSql.Append("insert into T_SysLog("); strSql.A ...
- 局部线性嵌入(LLE)原理总结
局部线性嵌入(Locally Linear Embedding,以下简称LLE)也是非常重要的降维方法.和传统的PCA,LDA等关注样本方差的降维方法相比,LLE关注于降维时保持样本局部的线性特征,由 ...
- STL中的set容器
#include <iostream> #include <set> using namespace std; int main() { set<int> s; s ...
- Configuration ReportNG with TestNG
下载 Reporter.jar,velocity-dep-1.4.jar 和 Guice.jar: 配置项目属性:Properties ->TestNG ->Disable Default ...
- spring 入门笔记(一)
最近学习spring 通过笔记形式加深自己对spring的理解,也希望能跟各位入门者分享和讨论. 一.下载spring 下载spring也费了不少功夫,目前还没从spring官网找到下载入口,我从下面 ...
- scrapy1.1入门用例简介
今天将scrapy安装成功,测试了下,倒腾了好长时间,才倒腾成功,特此分享. 其实最好的老师就是scrapy的帮助文档,只要把文档看懂,照着做,也就啥都会儿了! 帮助文档下载见http://downl ...
- python:UnboundLocalError: local variable 'xxx' referenced before assignment
近来一直都在学习python语言,偶然在伯乐在线看到2017年京东C/C++的面试题.就打算用python+ST3 IDE顺便敲下面试题代码. 原题 C语言: #include <stdio.h ...
- 驱动读写进程内存R3,R0通信
stdafx.h 头文件代码 #ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later. #defin ...
- Linux bash重拾 2016-8-16
bash支持的引号: "" 双引号:弱引用,可以实现变量替换(变量会替换为值) [root@localhost ssh-6Xolw4LLAMwe]# echo "dire ...
- Android Service之LOCATION_SERVICE
Android提供了GPS功能 LocationManager obj = (LocationManager)getSystemService(Context.LOCATION_SERVICE) Pe ...