国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Session過期后自動(dòng)跳轉(zhuǎn)到登錄頁面的實(shí)例代碼

高洛峰
發(fā)布: 2017-01-07 09:36:15
原創(chuàng)
2374人瀏覽過

最近做了一個(gè)項(xiàng)目其中有需求,要實(shí)現(xiàn)自動(dòng)登錄功能,通過查閱相關(guān)資料,打算用session監(jiān)聽來做,下面給大家列出了配置監(jiān)聽器的方法:

1.在項(xiàng)目的web.xml文件中添加如下代碼:

<!--添加Session監(jiān)聽器-->
<listener>
<listener-class> 監(jiān)聽器路徑 </listener-class>
</listener>
登錄后復(fù)制

? ?

2.編寫java類。

public class SessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent arg0) {
// session創(chuàng)建時(shí)執(zhí)行
SimpleDateFormat simpleFormat = new SimpleDateFormat("mm-ss-ms");
String nowtimes = simpleFormat.format(new Date());
User u=null;
//System.out.println("執(zhí)行。。 當(dāng)前時(shí)間:"+nowtimes+"_"+u);
HttpSession ses= arg0.getSession();
String id=ses.getId()+"_"+ses.getCreationTime();
}
public void sessionDestroyed(HttpSessionEvent arg0) {
// session失效時(shí)執(zhí)行
SimpleDateFormat simpleFormat = new SimpleDateFormat("mm-ss-ms");
String nowtimes = simpleFormat.format(new Date());
//System.out.println("session失效了。。 結(jié)束時(shí)間: "+nowtimes);
}
}
登錄后復(fù)制

? ?

配置完成后等session失效后成功進(jìn)入sessionDestroyed方法,準(zhǔn)備進(jìn)行頁面跳轉(zhuǎn)操作,突然發(fā)現(xiàn)怎么寫跳轉(zhuǎn),愣住了,繼續(xù)上網(wǎng)請(qǐng)教大神,發(fā)現(xiàn)這個(gè)監(jiān)聽是做一些后臺(tái)統(tǒng)計(jì)處理的,無法實(shí)現(xiàn)頁面跳轉(zhuǎn)的功能。

只能放棄這方法了,開始使用過濾器實(shí)現(xiàn)

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>
登錄后復(fù)制

? ?

2、新建SessionFilter類,實(shí)現(xiàn)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("/"));
// 超時(shí)處理,ajax請(qǐng)求超時(shí)設(shè)置超時(shí)狀態(tài),頁面請(qǐng)求超時(shí)則返回提示并重定向
if (path.indexOf(".action") != -1
&& session.getAttribute("LOGIN_SUCCESS") == null) {
// 判斷是否為ajax請(qǐng)求
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);// 不可少,否則請(qǐng)求會(huì)出錯(cuò)
} else {
String str = "<script language='javascript'>alert('會(huì)話過期,請(qǐng)重新登錄');"
+ "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
}
}
登錄后復(fù)制

? ?

3、客戶端JS,用于ajax請(qǐng)求session超時(shí)

對(duì)于jquery

<script type="text/javascript">
$(document).ajaxComplete(function(event, xhr, settings) {
if(xhr.getResponseHeader("sessionstatus")=="timeOut"){
if(xhr.getResponseHeader("loginPath")){
alert("會(huì)話過期,請(qǐng)重新登陸!");
window.location.replace(xhr.getResponseHeader("loginPath"));
}else{
alert("請(qǐng)求超時(shí)請(qǐng)重新登陸 !");
}
}
});
</script>
登錄后復(fù)制

? ?

對(duì)于extjs的ajax請(qǐng)求

Ext.Ajax.on('requestcomplete',checkUserSessionStatus, this);
function checkUserSessionStatus(conn,response,options){
if(response.getResponseHeader("sessionstatus") == 'timeout'){
if(response.getResponseHeader("loginPath")){
alert("會(huì)話過期,請(qǐng)重新登陸!");
window.top.location.href = response.getResponseHeader("loginPath");
}else{
alert("請(qǐng)求超時(shí)請(qǐng)重新登陸 !");
}
}
}
登錄后復(fù)制

? ?

如果使某個(gè)ajax請(qǐng)求不受全局方法的影響,那么可以在使用$.ajax()方法時(shí),將參數(shù)中的global設(shè)置為false,jquery代碼如下:

$.ajax({
url:"test.html",
global:false//不觸發(fā)全局ajax事件
})
登錄后復(fù)制

? ?

以上所述是小編給大家介紹的Session過期后自動(dòng)跳轉(zhuǎn)到登錄頁面的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家想了解更多內(nèi)容敬請(qǐng)關(guān)注PHP中文網(wǎng)!

更多Session過期后自動(dòng)跳轉(zhuǎn)到登錄頁面的實(shí)例代碼相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

最佳 Windows 性能的頂級(jí)免費(fèi)優(yōu)化軟件
最佳 Windows 性能的頂級(jí)免費(fèi)優(yōu)化軟件

每個(gè)人都需要一臺(tái)速度更快、更穩(wěn)定的 PC。隨著時(shí)間的推移,垃圾文件、舊注冊(cè)表數(shù)據(jù)和不必要的后臺(tái)進(jìn)程會(huì)占用資源并降低性能。幸運(yùn)的是,許多工具可以讓 Windows 保持平穩(wěn)運(yùn)行。

下載
來源:php中文網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn
最新問題
開源免費(fèi)商場(chǎng)系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)