您的位置:首页 > 数据库

web 中关于清除session会话 中保存的对象的时候 执行删除语句操作

2014-04-29 18:38 483 查看
web 程序中删除当前用户的一系列操作在session失效之前执行

需要创建一个sessionLisenter 监听类 来监听当前客户端的session

 

package com.zlkj.pub;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.zlkj.dagl.bean.DrvVerifyTempInfo;
import com.zlkj.dagl.bean.VehVerifyTempInfo;
import com.zlkj.dagl.services.AmendArchivesService;
import com.zlkj.dagl.services.VerifyArchivesService;
import com.zlkj.frmwork.bean.UserSession;
import com.zlkj.pub.database.ExcuteResult;
public class SessionListener implements HttpSessionListener{
 /**
  * 执行创建
  */
 public void sessionCreated(HttpSessionEvent arg0) {
  // TODO Auto-generated method stub
  System.out.println("=======================狼来了!!!!");
 }
 /**
  * 执行销毁
  */
 public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
  // TODO Auto-generated method stub
    //得取Session
    // ServletContext servletContext = httpSessionEvent.getSession().getServletContext(); 
   HttpSession servletContext = httpSessionEvent.getSession();
        UserSession userSession = (UserSession) servletContext.getAttribute("userSession");
   
        synchronized (this) {
         System.out.println("用户是"+userSession);
            if(userSession != null && !"".equals(userSession)){
             String yhdh = userSession.getUser().getYhdh();
             //System.err.println("sessionId="+yhdh);
             //获取applicationContext上下文 
             WebApplicationContext webApplicationContext= WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext.getServletContext());
             //获取审核
             VerifyArchivesService  verifyArchivesService = (VerifyArchivesService) webApplicationContext.getBean("verifyArchivesService");
             ExcuteResult resultDrv =  verifyArchivesService.deleteAllObjectVerifyTempInfo(new DrvVerifyTempInfo("",yhdh), "drvVerifyTempInfo");
             ExcuteResult resultVeh =  verifyArchivesService.deleteAllObjectVerifyTempInfo(new VehVerifyTempInfo("",yhdh), "vehVerifyTempInfo");
            }
        }
     
 }
}


web.xml中的配置需要配置的监听

 

<!-- session失效监听 -->
  <listener>
       <listener-class>
           com.zlkj.pub.SessionListener
       </listener-class>
    </listener>
<!--设置失效时间 -->
    <session-config>
       <session-timeout>1</session-timeout>
    </session-config>


绝对可以的 本人亲试了呵呵!希望您能用上哈
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐