scopes: session vs. application

I'm a bit confised. What exactly is the difference between the scopes "session" and "application"?

If "Session information is scoped only to the current web application (ServletContext)", then what is the difference? I get that application is suppose to be a bigger scope than session. But how so?

Before, when I couldn't access the session attributes from a JSP in a different directory, I was able to access the application attributes from that same JSP. So, I guess they (the two JSPs in different directories) are in the same application but somehow do not share session. But this then invalidates the API spec "Session information is scoped only to the current web application (ServletContext)". It seems that Session info is scoped to less than the current web application. But again, how much less?

Much thanks.

[845 byte] By [rhibiscusa] at [2007-9-19]
# 1

The session scope is NOT available to the web application (all the users) but rather is tied to a specific user - hence session. An object placed in the session will be available to only that specific user. A shopping cart, which is specific to an individual, is something that would go in the session.

The application scope is global and all JSPs/Servlets can retrieve these objects regardless of who the user is. Global variables and objects that all users need to have access to can be placed here. Remember, however, that if you change a value here - all other users that access this object will get the new stuff too. So you definitely DO NOT want to put user/session specific values here.

Cheers

shooterDoga at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2

Wonderful reply. Thank you. That does clear up some things for me.

The thing is though, why is it that when I have two JSPs residing in different folders and I go from one JSP to the other and all of the sudden am dealing with two sessions?

To give you a very simple example:

The output of one run of /OneTwo/One/sessionTest.jsp is:

Hello This is a session test. Setting session variable hello....

This is the session attribute hello now: hello

session id = ds816a11190050i1h1aq

Go to this other page to check session: /OneTwo/Two/sessionTest.jsp

When I clicked on the link and went to /OneTwo/Two/sessionTest.jsp, I got:

Hello This is a session test. Getting session variable hello....

This is the session attribute hello now: null

session id = ds816a11190050j1h40g

Go to this other page to check session: /OneTwo/One/sessionTest.jsp

--/OneTwo/One/sessionTest.jsp--

<HTML>

<HEAD><TITLE>Session Test</TITLE></HEAD>

<BODY >

<pre>

Hello This is a session test. Setting session variable hello....

<% session.setAttribute("hello", "hello"); %>

This is the session attribute hello now: <%= (String) session.getAttribute("hello") %>

session id = <%= session.getId() %>

Go to this other page to check session: <A HREF="/OneTwo/Two/sessionTest.jsp">/OneTwo/Two/sessionTest.jsp</A>

</pre>

</BODY>

</HTML>

--/OneTwo/Two/sessionTest.jsp--

<HTML>

<HEAD><TITLE>Session Test</TITLE></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF">

<pre>

Hello This is a session test. Getting session variable hello....

<% String hello = (String) session.getAttribute("hello"); %>

This is the session attribute hello now: <%= hello %>

session id = <%= session.getId() %>

Go to this other page to check session: <A HREF="/OneTwo/One/sessionTest.jsp">/OneTwo/One/sessionTest.jsp</A>

</pre>

</BODY>

</HTML>

rhibiscusa at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3
My guess is a server misconfiguration, so that thecontainer thinks "/OneTwo/One" is a different "context"(and therefore web application) to "/OneTwo/Two".Can you get / retrieve this value if you push the datainto application scope?
darrosea at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4
> Can you get / retrieve this value if you push the> data> into application scope?yup. does this change your answer?thanks.
rhibiscusa at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5
Nevermind, you all. Thanks for all your help. Turned out I wasn't putting the files in the same big directory after all. Sigh. Sorry for all the madness. Session is conquered.
rhibiscusa at 2007-7-8 > top of java,Enterprise & Remote Computing,Web Tier APIs...