Java Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we'll see the difference between calling HttpServletRequest#getSession() and HttpServletRequest#getSession(boolean).

2. What's the Difference?

The methods getSession() and getSession(boolean) are very similar. There's a small difference, though. The difference is whether the session should be created if it doesn't exist already.

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. Among other things, this is handy when we want to ask if the session exists.

3. Example

In this example, we are considering this scenario:

  • the user enters the user id and logs in to the application
  • the user then enters the user name and age and wants to update these details for the logged-in user

We'll store the user values in the session to understand the usage of HttpServletRequest#getSession() and HttpServletRequest#getSession(boolean).

First, let's create a servlet where we're using HttpServletRequest#getSession() in its doGet() method:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession();
    session.setAttribute("userId", request.getParameter("userId"));
}

At this point, the servlet will retrieve the existing session or create a new one for the logged-in user, if it doesn't exist.

Next, we'll set the userName attribute in the session.

As we want to update the details of the user for the respective user id, we want the same session and do not want to create a new session to store the user name.

So now, we will use HttpServletRequest#getSession(boolean) with false value:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession(false);
    if (session != null) {
        session.setAttribute("userName", request.getParameter("userName"));
    }
}

This will result in setting the userName attribute on the same session that the userId was previously set.

4. Conclusion

In this tutorial, we've explained the difference between HttpServletRequest#getSession() and HttpServletRequest#getSession(boolean) methods.

The complete example is available over on GitHub.

Java bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
R. G.
R. G.
3 months ago

The example with “false” will create a NullPointerException for the case above mentioned. For every request that doesn’t have a session or when it hasn’t been created […] the servlet will retrieve the existing session or create a new one for the logged-in user, if it doesn’t exist. vs […] we want to update the details of the user for the respective user id, we want the same session and do not want to create a new session. […] I would fix that with: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {     HttpSession session = request.getSession(false);… Read more »

Loredana Crusoveanu
1 month ago
Reply to  R. G.

Hi Ruben,
Thanks for catching that. We’ll update the article.

Comments are closed on this article!