How to check what objects are stored in HttpSesession

You might want to check what objects are stored in HttpSession because either your application is throwing or during performance testing you want to check size of the objects stored in session.

The WebSphere Application Server ships a servlet that can be used to find out what is content of the session as well as size of the objects stored in session. The SessionInspectionServlet is part of AppServer/plugins/, which is already on your web applications class path. So in order to use the SessionInspectionServlet, you will have two choices

  1. Define the SessionInspectionServlet in your web.xml and map it to a URI

  2. Enable Serving servlets by class name, you can set this value in the ibm-web-ext.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <reload-interval value="3"/>
    <enable-directory-browsing value="true"/>
    <enable-file-serving value="true"/>
    <enable-reloading value="true"/>

    <enable-serving-servlets-by-class-name value="true" />


After making this change restart your web application and then access your web application so that the attributes/ objects are added into HttpSession, once that part is done you can access the SessionInspectionServlet and it will show you output like this

In my case i did create a simple SessionInspectionServletDemo servlet like this

package com.webspherenotes.session;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

* Servlet implementation class SessionInspectionServletDemo
public class SessionInspectionServletDemo extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().println("Inside SessionInspectionServletDemo");
HttpSession session = request.getSession();
session.setAttribute("testName", "testValue");
Contact c = new Contact();
session.setAttribute("contact", c);


Inside the doGet() method i a am adding a testName attribute with String value and a contact attribute with object of non Serializable Contact object as value.

I made changes in the Contact object to implement interface and now i can see that the SessionInspectionServlet servlet does not list it under the Non-Seiralizable object and it also shows the size of the session

No comments: