关闭浏览器时清除session

前一段时间做了一个小网站,本来想用fiter过滤后台登录,可是,当你给一个flag时,它实际还是存储在session中,这样的话,在我把url直接跨过框架指向一个新增或者其它后台页面时,只要session中登录过保存的值,直接关闭后,还是没有清除,因此,有人下次不用登录就可以指到这个地址。下面我介绍一下自己用的解决方案。

一、新建一个.js文件,将以下代码保存:

function window.onUnload()

{

var newWindow;

if((window.screenLeft>=10000 && window.screenTop>=10000)||event.altKey)

{ newWindow=window.open('destorys.jsp','网页名称','width=0,height=0,top=4000,left=4000');//新窗口将在视区之外打开 newWindow.opener=null; sleep(5000); newWindow.close();//新窗口关闭 }

}

function sleep(milisecond)

{ var currentDate,beginDate=new Date(); var beginHour,beginMinute,beginSecond,beginMs; var hourGaps,minuteGaps,secondGaps,msGaps,gaps; beginHour=beginDate.getHours(); beginMinute=beginDate.getMinutes(); beginSecond=beginDate.getSeconds(); beginMs=beginDate.getMilliseconds(); do {   currentDate=new Date();   hourGaps=currentDate.getHours() - beginHour;   minuteGaps=currentDate.getMinutes() - beginMinute;   secondGaps=currentDate.getSeconds() - beginSecond;   msGaps=currentDate.getMilliseconds() - beginMs;   if(hourGaps<0) hourGaps+=24; //考虑进时进分进秒的特殊情况   gaps=hourGaps*3600+ minuteGaps*60+ secondGaps;   gaps=gaps*1000+msGaps; }while(gaps<milisecond); }

其中红色部分为你指向清除session的JSp页面。

如下:

<%@ page contentType="text/html; charset=GBK" %> <%@ page language="java" import="java.lang.*"%> <jsp:useBean id="login" scope="page" class="com.util.Login"/> <% session.removeAttribute("username"); session.removeAttribute("userid"); session.removeAttribute("power"); session.removeAttribute("flag"); %>

这样,在每个后台页面引用一个这个JS,就可以实现了。

原文地址:https://www.cnblogs.com/guanghuiqq/p/2740709.html