亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 PHP開(kāi)發(fā) Example code that automatically jumps to the login page after the session expires

Example code that automatically jumps to the login page after the session expires

Jan 07, 2017 am 09:36 AM

I recently worked on a project where there was a need to implement the automatic login function. After consulting relevant information, I planned to use session monitoring to do it. Here is a list of how to configure the listener:

1. Add the following code to the project's web.xml file:

<!--添加Session監(jiān)聽(tīng)器-->
<listener>
<listener-class> 監(jiān)聽(tīng)器路徑 </listener-class>
</listener>

2. Write a java class.

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);
}
}

After the configuration is completed and the session expires, it successfully enters the sessionDestroyed method and prepares to perform the page jump operation. Suddenly, I discovered how to write a jump. I was stunned and continued. I consulted the experts on the Internet and found that this monitoring is for background statistical processing and cannot realize the page jump function.

We can only give up this method and start using filters to implement

1. Add filter configuration in 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>

2. Create a new SessionFilter class and implement the Filter interface.

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),頁(yè)面請(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=&#39;javascript&#39;>alert(&#39;會(huì)話過(guò)期,請(qǐng)重新登錄&#39;);"
+ "window.top.location.href=&#39;"
+ loginUrl
+ "&#39;;</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. Client JS, used for ajax request session timeout

For jquery

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

For extjs ajax request

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

If an ajax request is not affected by the global method, you can use $ When using the .ajax() method, set the global in the parameter to false. The jquery code is as follows:

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

The above is the Session introduced by the editor. Example code that automatically jumps to the login page after expiration. I hope it will be helpful to everyone. If you want to know more, please pay attention to the PHP Chinese website!

For more related articles on the example code that automatically jumps to the login page after the Session expires, please pay attention to the PHP Chinese website!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)