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 ...
随机推荐
- /root/.bashrc与/etc/profile的异同
要搞清bashrc与profile的区别,首先要弄明白什么是交互式shell和非交互式shell,什么是loginshell 和non-loginshell. 交互式模式就是shell等待你的输入,并 ...
- C++变量的“总分性”(Mereology)
Stroustrup 在自传中说自己在哲学上深受 Kierkegaard (吉爾凱高爾)的影响,而讨厌黑格尔.所以看 Stroustrup 的书,很少感受到抽象理论的重要性.这也影响了C++的文化:许 ...
- POJ 1155 树形背包(DP) TELE
题目链接: POJ 1155 TELE 分析: 用dp[i][j]表示在结点i下最j个用户公司的收益, 做为背包处理. dp[cnt][i+j] = max( dp[cnt][i+j ...
- CentOS 6.5下安装MySql 5.7
不管您按下面的方法安装成功否,请留个言,把您遇到的问题写上共勉! 包下载http://url.cn/WrNg5S 环境: 1).软硬件:E6420双核CPU,8G内存,1T硬盘 2).虚拟机下 Cen ...
- windows计划任务执行SQLserver脚本
2016年3月1号,北京出差,documentbrowser系统改善上线. 其中有一个数据库表需要每天进行同步,原计划使用SQLServer的作业来执行又方便又快捷,但是客户的数据库是05的expre ...
- 使用grunt压缩css是能否设置background-size不压缩进去呢?否则ie8不能识别
.index-bg{ background:url(img/index-bg-t.5344b19d.jpg) center center/cover no-repeat } 比如上面这样ie8不能识别 ...
- C语言的算法--------二分法查找
int find(int n,int a[],int l){int low=0;int high=l-1;int middle=0;while(low<high){middle=(low+hig ...
- HDU-2054 A==B?
#include<stdio.h>#include<string.h>char n[100000], m[100000];int main(){ int i, j, len_n ...
- codeforces567E. President and Roads
题目大意:总统要回家,会经过一些街道,每条街道都是单向的并且拥有权值.现在,为了让总统更好的回家,要对每一条街道进行操作:1)如果该街道一定在最短路上,则输出“YES”.2)如果该街道修理过后,该边所 ...
- 玩玩SPARK
没有SCALA的东东,玩不起哈. ./spark-shell 从文件生成一个DRIVER? val logFile = sc.textFile("hdfs://192.168.14.51:9 ...