Scroll to top

State Management in JSP

In this tutorial we are going to take a look at the following:

  • Session objects
  • Cookies

JSP Session objects

Alright, so a session object is created once for a user for their browser session. It is always unique for a given user, and it’s used to keep track of the user’s activities on your site. So consider an online banking application, an e-Store or an online exam application. For every user who accesses these applications, you are going to want to keep track of every action of the users on those sites. Specifically, every action specific to every user. For the purpose of this tutorial we are going to develop a JSP page to manage a To-Do List.

We are going to create a page where we will store the list of items in the user’s session object, and each user will have their own To-Do list. Here is a general overview of how this is all working.

See, the page will be the same, but for every user a unique session will be created which will store their information. These session objects are stored in the memory, not in the file system or on the database. These objects are just in the server’s memory, which will be cleared or deleted once the user logs out. There are ways to store them for later use but we won’t be looking into that at the moment.

You might have noticed that every user is assigned a unique session ID. The server does that for you, and you being a developer don’t have to worry about handling the session ID or making use of it in your applications, the Tomcat Server does that for you.

Coding a session object

So let’s take a look at some of the coding methods we can use to manipulate a session object. Here’s how we add data to a session object:

session.setAttribute(String name, Object value)

The name is just a label and the value is the stuff you want to place in the session object. So if I was to place an array list into the session, this is how I’d do it.

List<String> items = new ArrayList<>();

session.setAttribute(“myToDoList”, items);

Remember, you can place any kind of data into the session object, may it be integers or your custom objects. Next, we take a look at how to retrieve data from the session object:

object session.getAttribute(String name)

The session.getAttribute method takes just the name of the object as an argument and returns the plain object. So if I was to retrieve data from the object I previously created, I’d do it like this:

List<String> myStuff = (List<String>) session.getAttribute(“myToDoList”);

Here we are just getting the myToDoList session object, downcasting it to the appropriate object type and then assigning it to myStuff object.

Some other JSP Session Methods

  • isNew() – the return type of this method is Boolean and it returns true if the session is true.
  • getId() – the return type of this method is String and it returns the ID of the session object.
  • invalidate() – the return type of this method is void and it invalidates the session object, unbinding all data associated to it.
  • setMaxInactiveIntervals(long mills) – the return type of this method is void and it sets the idle time for a session object to expire. This method takes milliseconds as argument.

This isn’t the complete list of arguments, for more information make sure to check out the JSP documentation.

Coding JSP Sessions

As we discussed earlier on, let’s create a JSP page which will act as a To-Do list and will operate using JSP sessions. We’ll:

  • Create an HTML form to add data to the list.
  • Add the item to the list.
  • Display all the items from the session.

Let’s begin:

<!-- The form will be submitting data to this page itself -->
<form action="todo-demo.jsp">
            Add New Item: <input type="text" name="theItem" />
<input type="submit" value="Submit" />
<!-- Let's add the entered item to the list -->
<!-- The list to contain the items -->
            List<String> items = (List<String>) session.getAttribute("myToDoList");
<!-- Let's check if the item exists -->
            items = new ArrayList<String>();
<!-- This will give me a new list which can hold the items, if one doesnt already exist -->
            session.setAttribute("myToDoList", items);
<!-- We are just binding the session to the recently created list -->
<!--Let's add items to the list -->
            String theItem = request.getParameter("theItem");
<!-- Let's fetch the items from the session and display them -->
<b>To do items: </b><br/>
                   for(String temp: items){

And we are done. If you have been following this code, you won’t have any trouble understanding this page.


First we created a simple form which would take the task as input from the user. Then we have created a list which contains will contain all the elements, then we have checked if the session object exists, if it doesn’t a new one will be created and it will be associated with the list. Then, the item entered by the user is being added into the list.

The last scriptlet is just fetching the items from the list, using a “for loop”, and printing them out in the order they were added.

In the next tutorial, we are going to take a look at cookies.

Author avatar

1 comment

  1. Dan T.

    Thank you very much for these articles – they’re straightforward and easy to understand, and the examples are simple but that’s what makes them so useful. I appreciate your time and effort in this.

Post a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.