Hi Can anyone plz let me know what would be the problem in the following code.In this below code I have invalidated my session after creating the new session by using the http session.getsession(true) and setting the attribute also.Once I click the logout i am invalidating the session but that is not became null.

Here I am not getting null System.out.println("sess"+sess)

HttpSession sess = request.getSession(false);
sess.removeAttribute("name");
sess.invalidate(); 
System.out.println("sess"+sess);
有帮助吗?

解决方案

As API said,

Invalidates this session then unbinds any objects bound to it.

It doesn't need to be null, it just unbinds the objects. You can check whether your previously set attributes are still there in the session.

其他提示

Invalidating session means that server forgets it so once you call getSession() again, you will receive new session. Existing variable (sess) is not affected, it is just not stored internally in servlet container. So there is no reason your variable to be set to null in your code.

    HttpSession session = request.getSession();
    Date date = (Date) session.getAttribute("TEST");
    System.out.println("date = " + date);
    if (date == null) {
        session.setAttribute("TEST", new Date());
    }
    if (new Random(System.currentTimeMillis()).nextInt(10) > 7) {
        session.invalidate();
    }

Put this code into servlet and watch container logs. It shall recreate session in 30 percent., otherwise it will print stored key.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top