views:

248

answers:

2

I use jsp/Servlets for my web layer. Is there any tool to examine session attributes in a web session?

A: 

Which servlet container are you using? If it's JBoss, see this page.

dj_segfault
I don't understand, the link you pointed only has a tutorial on EJB3.!
ruwan.jayaweera
When you said session attributes, I assumed you meant session beans. Sorry if that was a bad assumption.
dj_segfault
+2  A: 

Of course. It's not actually a tool, but a simple code snippet. Somewhere in a servlet/jsp/filter of yours add the following:

Session session = request.getSession();
Enumeration attributeNames = session.getAttributeNames();
while (attributeNames.hasMoreElements() {
    String name = attributeNames.nextElement();
    String value = session.getAttribute(name);
    System.out.println(name + "=" + value);
}

and you will have all attributes of the session printed on the console.

Alternatively, in JSP do:

<c:forEach items="${sessionScope}" var="attr">
    ${attr.key}=${attr.value}<br>
</c:forEach>

this will print all attributes of the session on the page.

Update: It turns out you have a wrong understanding of the session. The session data is at the server-side. The client only hold a unique identifier by which its data is referred at the server. This identifier is most often the "session cookie", but can also be part of the url (JSESSIONID). So the client cannot see the contents of the session directly. If you want your session attributes to be displayed with meaninful values (different from their hashcode) override their toString() method.

Bozho
Thank you, this was the solution I also came up with finally, but isn't there a tool, (like firefox plugin) which can do this? (because if a browser can clear the session attributes why cant it display them?)Also if the attribute is an object, using this technique, it will only show the hashcode of that object.
ruwan.jayaweera
you seem to be misunderstanding the session concept. see mu update
Bozho
I understand that the session data is on the server side, but what I'm asking is whether we can view the session variables with out having code (like the ones you have given above) in the server side? For example, can we view the session variables of the stackoverflow.com?Also to avoid the hash code is override toString() that I understand. but what I want is to drill down an object to view its child objects and its child objects ....
ruwan.jayaweera
1. There is no way to view the session contents without code 2. you can use reflection to drill down an object
Bozho