[HELP !! JSP+Servlet] Session is shared among requests

Hi,

It seems like my web application is not able to create multiple sessions. So, a session will not be created untill an existing session is expired.

So, here is a simple algorithm of my application

Step 1 (jsp) -> Get user input then pass it step 2

Step 2 (servlet) -> process the input andcreate a session object and embedd an object to the session object. Redirect page to step 3

Step 3 (jsp) -> Show the output from the embedded object on session object then provide more input to step 4

Step 4 (servlet) -> Using input from step 4 and embedded object on session object, attach the final output to session object and redirect page to step 5

Step 5 (jsp) -> Show the final output that is taken from session object

While i can force servlet to always create a new session in step 2 for each request but when two simultaneous requests going to step 4 (from step 3), only the first request gets the right session, the second request get the first request's session, so at step 5, both requests display the output of the first request. Thus, if at the end of step 5 I invalidate the session, the second request won't get session object at all.

I have tried this from multiple browsers but still it doesnt work. My biggest suspect is that only one session is allowed to be alive in a servlet container.

Does anyone know how to solve this? Thanks

Message was edited by:

QuarterBack

Message was edited by:

QuarterBack

[1534 byte] By [QuarterBacka] at [2007-11-27 2:32:55]
# 1

> Step 2 (servlet) -> process the input and create a

> session object and embedd an object to the

> session object. Redirect page to step 3

By chance is the embedded object declared in class scope of the servlet?

> Step 3 (jsp) -> Show the output from the embedded

> object on session object then provide more input to

A class-scoped object would exhibit this behavior, because there is only 1 servlet shared by multiple threads. If each session uses the same object its data would not be thread-safe. Try declaring the embedded object within doXxx.

developer_jbsa at 2007-7-12 2:49:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hmm... I have no class-scoped objects in my servlet although the embedded object that I attach to session does have class-scoped objects/variables.

And, i just debug my servlet with eclipse, and i found very strange behavior.

So, it seems like two different sessions are using/referring to the same data.

So, when a session in a thread change some attributes, the attributes of the other session in the other request also got changed !!! I am quite sure they are two different sessions (in eclipse's variable watch, they have two different ids).

Could this be a bug in the servlet container (I'm using Apache Tomcat 6)?

QuarterBacka at 2007-7-12 2:49:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> Hmm... I have no class-scoped objects in my servlet

> although the embedded object that I attach to session

> does have class-scoped objects/variables.

> So, it seems like two different sessions are

> using/referring to the same data.

This is to be expected if that data is class-scoped. And you said above that it is. There's only one instance of the class-scoped data so both sessions necessarily refer to that one instance.

> Could this be a bug in the servlet container (I'm

> using Apache Tomcat 6)?

You should consider the likely possibilities before the unlikely possibilities.

DrClapa at 2007-7-12 2:49:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Thanks for the explanation, i am not quite sure if i get the idea, so what you are saying is that...

A local object in a servlet (declared inside doGet/doPost method) shares its instance fields (or class-scoped objects/variables) with other servlet threads.

This is not what I thought. I thought that for any local object (inside doGet/doPost), servlet container will create an instance exclusively for that thread. So, if there are two requests, two local objects will be created, one for each.

Anyway, this is very great information, i really appreciate it.

Do you have any suggestions how we handle instance fields for servlets just like in my cases? Because, it is very common to have instance fields.

Thanks,

QuarterBacka at 2007-7-12 2:49:03 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...