• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session scoped attributes in different apps

 
Todd Farmer
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to determine if session-scoped attributes were persisted and exposed across different web apps running within the same JVM. My expectation was that a session attribute set in one web app would be accessible by another web app running in the same container/JVM. I did a little test, with one app setting a session attribute and displaying it on-screen, and a second app that would just display the attribute on-screen (but not set it). I used JSP, EL and JSTL, all of which are things I am not familiar with, so the results could be tainted by my methods.

Anyhow, I was surprised to see that the session attribute set in the first app was not visible to the second app. Now I need help determining if this is the expected behavior or not (it was not what _I_ expected, but that's not saying a lot). Here's the relevant code in the JSP for the first app:

<c:set var="test" scope="session" value="testString"/>

Test: ${sessionScope.test}

The JSP in the second app contained only:

Test: ${sessionScope.test}

When I ran the JSP in the second app first, no value appeared for the "test" attribute. I then ran the JSP in the first app, and it displayed a value of "testString". When I re-ran the JSP in the second app, there was again no value displayed. I then commented out the c:set tag and re-ran the JSP in the first app. The "testString" value still displayed.

I could very well be using the JSTL or EL expressions incorrectly; this is very new to me. Can somebody please point out what I might be doing wrong, either in testing or in my expectations that session data is persisted at the container level and made accessible to all apps within the container?

Thanks!

Todd Farmer
SCJP 1.4
 
Todd Farmer
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I figured out why the attribute is not accessible to the second JSP; my browser (Mozilla) seems to be sending two different JSESSIONID cookie values to Tomcat, depending on which app is being requested. Looking at the cookies, it appears that they are set with a path value that specifies the app associated with the session. Is this the expected behavior? And is there any way to override this behavior?

If I have two different web apps that need to share session information, is this possible? Or should I plan to make every servlet/JSP that requires access to this shared session information part of a large, single app?

Thanks!

Todd Farmer
 
Colin Fletcher
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Todd, it would depend on what method you are more comfortable with.

The three that come to my mind are (presuming realtime data requirement)

1. portlet from app A to be served in B

2. EJB... build a bean(s) for client based information

3. Temporary Database

Lifecycle of the data, maintenance, accessibility and intended use(s) will help in deciding your most appropriate option.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic