i am facing some problem i want to update login table on session expire ... here define method always gives new session and also update data on some condition
public HttpSession getNewSession(HttpServletRequest request,HttpServletResponse response){
HttpSession httpSession = request.getSession(false);
log.debug("Incoming httpSession.getId() :: " + httpSession.getId());
long lastAccessedTime = (System.currentTimeMillis()-httpSession.getLastAccessedTime())/1000;
log.debug((System.currentTimeMillis()-httpSession.getLastAccessedTime()) + " milliSecond.");
log.debug( lastAccessedTime + " Second. ");
log.debug( lastAccessedTime + " lastAccessedTime>=900");
if(lastAccessedTime>=900){
log.debug( "true lastAccessedTime>=900");
String userName = null != httpSession.getAttribute(USER_NAME)?httpSession.getAttribute(USER_NAME)+"":"";
CGLoginService documentManagementService = new CGLoginIMPL();
if(userName.length()>=1){
CGLoginDTO cgTrgLoginDTO = new CGLoginDTO();
cgTrgLoginDTO.setLoginFlag("false");
cgTrgLoginDTO.setUpdatedDate(new Timestamp(new Date().getTime()));
cgTrgLoginDTO.setLockFlag("false");
cgTrgLoginDTO.setLockCount(0);
CGLoginDTO cgLoginDTO = new CGLoginDTO();
cgLoginDTO.setLoginId(userName);
try {
documentManagementService.updateUsers(cgTrgLoginDTO, cgLoginDTO, null, false);
} catch (BuisnessException e) {
e.printStackTrace();
log.error(" Exception found while updating user is "+e);
}
}
httpSession.invalidate();
log.debug("httpSession.invalidate();");
}else{
log.debug("lastAccessedTime>=900 where session not expire and Outgoing httpSession.getId() :: " + httpSession.getId());
log.debug("user name :: " +httpSession.getAttribute("userName"));
if(httpSession !=null && httpSession.getAttribute("userName") !=null){
log.debug( "true httpSession !=null && httpSession.getAttribute(USER_NAME) !=null");
Object userName = httpSession.getAttribute("userName") ;
Object userRole = httpSession.getAttribute("userRole");
Object userToken = (httpSession.getAttribute("userToken") !=null)?httpSession.getAttribute("userToken"):"";
Object lastLogin = (httpSession.getAttribute("lastLogin") !=null)?httpSession.getAttribute("lastLogin"):"";
httpSession.invalidate();
httpSession = request.getSession(true);
httpSession.setMaxInactiveInterval(900);
httpSession.setAttribute("userName", userName);
httpSession.setAttribute("userRole", userRole);
httpSession.setAttribute("userToken", userToken);
httpSession.setAttribute("lastLogin",lastLogin);
log.debug("Outgoing httpSession.getId() :: " + httpSession.getId());
return httpSession;
}else
log.debug("session not map define value ");
httpSession = null;
}
return httpSession;
}
i also use HttpSessionListener interface for this stuff but session.invalidate() method also use this destroy method of listener .. because all time need new session id....