Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for valid session: isRequestedSessionIdValid() vs getSession(false)

I'm developing Java Servlets. At the point of checking whether a user is logged in, I want to check if the HTTP request has a valid session. For checking that, I have 2 possibilities:

(1)

protected void processRequest(HttpServletRequest request, 
        HttpServletResponse response) throws ServletException, IOException {

    HttpSession session = request.getSession(false);
    if (session != null) {
        // user is logged in
        ...
    }
}

Since I pass false as an argument, there is no new session created if there is no valid session existing already, and the function returns null, for what I can check.

Or I do:

(2)

    if (request.isRequestedSessionIdValid()) {
        // user is logged in
        ...
    }

Is there any difference, any advantage/disadvantage? Or do both functions do more or less the same?

like image 822
Terry Avatar asked Jan 09 '13 17:01

Terry


People also ask

How do you check if a session is valid or not?

– Retrieve a session from “request. getSession(false);”, this function will return a session if existed , else a null value will return. – Later you can do a “null” checking with the session object, null means no existed session available.

What is the difference between request getSession () and request getSession false?

Calling getSession() and getSession(true) are functionally the same: retrieve the current session, and if one doesn't exist yet, create it. Calling getSession(false), though, retrieves the current session, and if one doesn't exist yet, returns null.

What is request getSession false?

request. getSession(false) will return current session if current session exists. If not, it will not create a new session.

What is the use of getSession ()?

getSession() returns the valid session object associated with the request, identified in the session cookie that is encapsulated in the request object. Calling the method with no arguments creates a session if one does not exist that is associated with the request.


1 Answers

Form Javadoc

isRequestedSessionIdValid boolean isRequestedSessionIdValid() Checks whether the requested session ID is still valid. If the client did not specify any session ID, this method returns false.

Returns: true if this request has an id for a valid session in the current session context; false otherwise

So in sense both are same. But what you need to be aware of is request.getSession(false) will be null only in case of first request to the container. After the first request container creates a session and sends Jsessionid cookie along with response , so that it can track subsequent requests from the same browser. So in your case instead of checking if session is null or not, you should store a session attribute "is_logged_in"=true and check for this attribute as well if session is not null.

like image 158
Subin Sebastian Avatar answered Nov 05 '22 16:11

Subin Sebastian