문제

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