解决用户退出登录不会自动跳转到登录页面的问题

刚开始的footer.jsp是这样:window.setInterval(checkSessionUser(), 1000); checkSessionUser()定义放到common.js通过header.jsp包含进来,结果死活只在页面加载的时候执行一次,然后就没有然后了............后来才想到是页面加载的问题,so,把整个函数都放到footer.jsp里面来,这样就可以了每隔1秒执行一次了。


var flag = true;
window.setInterval(function(){
	var url = window.location.href;
	if(url.indexOf("Login_login.action") == -1) {
		$.ajax({
			url : "${ctx}/Login_checkSessionUser.action",
			type : "POST",
			data : null,
			dataType : "json",
			async : true,
			error : function() {
				if(flag) {
					flag = false;
					alert('服务器失去连接!');
				}
			},
			success : function(result) {
				if(!result.success) {
					window.location.href = ctx + "/Login_login.action";
					$("#message").text(result.errorMessage);
				}
			}
		});
	}
}, 1000);


服务端的checkSessionUser函数只是单纯的检查下服务端的session是否还有效而已,后来发现放到footer.jsp还是不太好,因为会有很多页面包含footer.jsp,虽然不向数据库发送请求,但是这样服务器的压力还是挺大的,1秒一次,所以最后把window.setInterval这段代码搬到了主页面的最后来。

你可能感兴趣的:(解决用户退出登录不会自动跳转到登录页面的问题)