關閉瀏覽器時清除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,就可以實現了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM