aspose file tools*
The moose likes Servlets and the fly likes Question about servlet initialization, threads, sessions, etc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Question about servlet initialization, threads, sessions, etc" Watch "Question about servlet initialization, threads, sessions, etc" New topic
Author

Question about servlet initialization, threads, sessions, etc

Bobby Pardridge
Greenhorn

Joined: Aug 01, 2011
Posts: 2
I'm trying to understand how instances of HttpServlet are created and managed in Tomcat.

So far my understanding is that there is one instance of HttpServlet for each user. So if I have the following code:

public class Test extends HttpServlet {

public int x;
public static int y;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
x = x++;
y = y++;
}
}

And I have user1 and user2 make 5 get requests, then for both users the value of x should be 5 and the value of y should be 10.

Am I totally missing something here?

Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61598
    
  67

Bobby Pardridge wrote:Am I totally missing something here?

Yes.

So far my understanding is that there is one instance of HttpServlet for each user

Whatever led you to this understanding needs to be expunged from your source material, as it is completely incorrect.

One instance of the servlet is created. Period. *

So if I have the following code:

You will have endless threading problems.

Bottom line: no instance variables in a servlet. None. Ever.



* There are exceptions, but none that are pertinent at this point.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bobby Pardridge
Greenhorn

Joined: Aug 01, 2011
Posts: 2
So I should make all my variables static?

Right now I have a ConcurrentHashMap instance variable in my HttpServlet that I want other classes to be able to read from, which is why I would like to make it static, and I was just wondering what the consequences of doing so would be.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61598
    
  67

Bobby Pardridge wrote:So I should make all my variables static?

Of course not. That would be even worse!

Your servlets need to be written in a thread-safe manner as only one instance of the servlet will be shared amongst all threads in the web app.

I was just wondering what the consequences of doing so would be.

Endless problems and headaches.

Data that only needs to be known to the current method should be variables declared in the method.

Anything else needs to be stored as a scoped variable in one of request, session or application scopes as appropriate for the life-cycle of the data.

Let me rephrase the bottom line: no class-level variables in a servlet unless they are read-only and (pseudo) final.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about servlet initialization, threads, sessions, etc