How to check if a session is invalid

前端 未结 3 1902
天命终不由人
天命终不由人 2020-12-13 04:39

How to check if a session is invalid or not? There is no method in the API.

Is it the same as isNew()? And what is the difference if not?

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 04:54

    If you want to know whether it valid based on a request:

    request.isRequestedSessionIdValid()
    
      or
    
    HttpSession sess = request.getSession(false);
    if (sess != null) {
       // it's valid
    }
    

    If you have stored a reference to the session and need to validate I would

    try {
      long sd = session.getCreationTime();
    } catch (IllegalStateException ise) {
      // it's invalid
    }
    

提交回复
热议问题