于是本人自定义一个类 SessionListener implements HttpSessionListener,并实现了方法sessionDestroyed。
public class SessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent se) { // TODO Auto-generated method stub } @Override public void sessionDestroyed(HttpSessionEvent se) { UserServiceI userService = new UserServiceImpl(); Member user = (Member) se.getSession().getAttribute(Comm.MEMBER_SESSION_NAME); userService.outLogin(user); } }
然后调用
@Override
public void outLogin(Member member) {
member.setIsOnline(0);
memberDao.update(member);
}
去数据库中修改登录状态会报错
报错代码:
严重: Session event listener threw exception
java.lang.NullPointerException
at com.wd.front.service.impl.UserServiceImpl.outLogin(UserServiceImpl.java:62)
at com.wd.front.listener.SessionListener.sessionDestroyed(SessionListener.java:27)
at org.apache.catalina.session.StandardSession.expire(StandardSession.java:806)
at org.apache.catalina.session.StandardSession.isValid(StandardSession.java:658)
at org.apache.catalina.session.ManagerBase.processExpires(ManagerBase.java:534)
at org.apache.catalina.session.ManagerBase.backgroundProcess(ManagerBase.java:519)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1352)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
at java.lang.Thread.run(Unknown Source)
50
看看UserServiceImpl.java:62 这一行哪个对象为空,然后被你引用了属性或方法