How do I write and read object from HTTP Session?

In this post you will learn how to write and read object from HTTP Session in JavaServer Page. The first example that we are looking at is using the classic JSP scriptlet, this is a very old way to work with JSP but it is good for you to know a history. We write a JSP scriptlet inside the <% %> symbols. We can use the provided session object. To set an attribute in the session object we use the setAttribute(String name, Object value) method. In the example we create an attribute called loginDate and set the value to the current date.

To read a value from a session object we use the getAttribute(String name) method. This method return a type of Object, so we need to cast it to the original object. In this case we cast it to a java.util.Date. And then we print out the value read from the session object>.

<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>JSP - Session Write</title>
</head>
</html>
<body>
<%
    // Creates a session attribute named login-date to store a java.util.Date.
    session.setAttribute("loginDate", new Date());

    // Read back the java.util.Date object from the session attribute.
    Date loginDate = (Date) session.getAttribute("loginDate");
%>
Login Date: <%= loginDate %>
</body>
JSP Session Write & Read

JSP Session Write & Read

The second way that you can use to read values from session object is using the JSP Expression Language (EL). It looks like the following code snippet. You can use the sessionScope implicit object combined with the session attribute name. You can see two ways to use the sessionScope object below. The simplest one is to use the attribute name as the EL expression, and it will look smartly to find the value in the available scope.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>JSP - Session Read EL</title>
</head>
<body>
    <p>Login Date: ${sessionScope.loginDate}</p>

    <p>Login Date: ${sessionScope["loginDate"]}</p>

    <p>Login Date: ${loginDate}</p>
</body>
</html>

And you will also got the same value from the session as shown in the picture below:

JSP Session Read Using EL

JSP Session Read Using EL

How do I know session last access time?

package org.kodejava.example.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class SessionLastAccessTime extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        Date date = new Date(session.getLastAccessedTime());

        PrintWriter writer = response.getWriter();
        writer.println("Last accessed time: " + date);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
}

This servlet will return a result like:

Last accessed time: Thu Jan 03 00:47:34 ICT 2008