Multiple Requests to Tomcat - New threads each time?

Hi all,

I have a web application that is running with Tomcat. I assumed that when a user requests the app Tomcat starts a new thread for that users requests, and then if someone else at the same time uses the app then they too are assigned a seperate instance?

Is this correct and if not how do I specify this? Basically the problem is user A is seeing user B requests and vice versa, they appear to be running in the same thread,

Any advice would be appreciated,

[489 byte] By [johrika] at [2007-10-2 5:30:33]
# 1

> I have a web application that is running with Tomcat.

> I assumed that when a user requests the app Tomcat

> starts a new thread for that users requests, and then

> if someone else at the same time uses the app then

> they too are assigned a seperate instance?

Yes, each request gets a separate thread. But not a separate instance, they will both be using the same instance of the servlet.

> Is this correct and if not how do I specify this?

> Basically the problem is user A is seeing user B

> requests and vice versa, they appear to be running in

> the same thread,

It's possible for threads to share data, and that's what you are seeing. The simplest way to cause threads to share data is to have them use the same object in a non-thread-safe way. For example, using instance variables or class variables in your servlet is not thread-safe, and most likely that is what you are doing.

There is almost never a reason to use instance or class variables in a servlet, so you should probably stop doing that. If there is a reason for doing so, you should synchronize access to the variables in question.

There's a Java threads tutorial which I encourage you to read.

DrClapa at 2007-7-16 1:41:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...