一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Session過期后自動跳轉(zhuǎn)到登錄頁面的實(shí)例代碼

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

2020-05-10 15:35零度的魚 JAVA教程

這篇文章主要介紹了Session過期后自動跳轉(zhuǎn)到登錄頁面實(shí)例代碼,非常不錯具有參考借鑒價(jià)值,需要的朋友可以參考下

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

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

?
1
2
3
4
<!--添加Session監(jiān)聽器-->
<listener>
<listener-class> 監(jiān)聽器路徑 </listener-class>
</listener>

2.編寫java類。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
}
}

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

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

1、web.xml中添加過濾器配置

?
1
2
3
4
5
6
7
8
<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類,實(shí)現(xiàn)Filter接口。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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請求超時(shí)設(shè)置超時(shí)狀態(tài),頁面請求超時(shí)則返回提示并重定向
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超時(shí)

對于jquery

?
1
2
3
4
5
6
7
8
9
10
11
12
<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("請求超時(shí)請重新登陸 !");
}
}
});
</script>

對于extjs的ajax請求

?
1
2
3
4
5
6
7
8
9
10
11
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("請求超時(shí)請重新登陸 !");
}
}
}

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

?
1
2
3
4
$.ajax({
url:"test.html",
global:false//不觸發(fā)全局ajax事件
})

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜国产福利视频一区 | 草榴色导航 | 美妇在男人胯下哀求 | 青草福利在线 | 男女真实无遮挡xx00动态图软件 | 夫妇野外交换激情 | 日韩国产欧美成人一区二区影院 | 欧美灰丝袜丝交nylons | 超级乱淫伦小说全集np | 无限资源在线观看8 | 国外欧美一区另类中文字幕 | 国内精品麻豆 | 99热国产在线 | 国产视频91在线 | 亚洲国内精品 | 午夜国产精品影院在线观看 | 久久这里只有精品视频9 | 久热在线这里只有精品7 | 日韩中文字幕在线不卡 | 国产中文在线视频 | 四虎影视永久免费视频观看 | 久久精品观看 | 波多野结衣无码 | 543精品视频 | 亚瑟天堂久久一区二区影院 | 国产日韩精品一区二区三区 | 国产一区二区三区在线看 | 国产va欧美va在线观看 | 护士xxxx | 国产精品网页 | 毛片网在线观看 | 亚洲四虎永久在线播放 | 四虎在线精品免费高清在线 | 日本最新伦中文字幕 | 欧美日韩三区 | 亚洲国产综合久久久无码色伦 | 成年人福利 | aaa黄色| 人成网站在线观看 | 韩国帅男同gay网站 韩国三级在线播放 | 日韩精品在线视频观看 |