• Post Reply Bookmark Topic Watch Topic
  • New Topic

number of instances of JSP  RSS feed

 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a little clarification on how tomcat handles jsp requests. does tomcat only create one instance of a jsp, and then use that instance for all requests for that page? or does it create additional instances if multiple requests for the same page come in at the same time?
i have an instance of a helper servlet loaded on start up that is currently not synchronized on the assumption that only one instance of a jsp is loaded. however, if that isn't the case i'll need to re-code some methods.
info or a link to the appropriate docs would be great. thanks.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP processing proceeds like this: The original JSP page is translated into pure Java source code for a servlet. This code is compiled and then loaded just like any other servlet. Therefore there is only one copy.
However (important!) any number of requests can be processed "at one time" by that one copy so you must pay attention to synchronization issues.
Furthermore, once the intial translation is done, the same servlet instance stays in memory for future requests to that JSP - thats why the first request takes longer than subsequent ones.
Bill
 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill,
thanks for the info - i will synchronize those methods. however, i'm still a little confused by what you mean when you say "at one time". does that mean that even though there is only one instance of a compiled jsp, it can be accessed by multiple threads simultaneously? this would imply that tomcat has more than one thread to handle jsp requests (which would make sense for scalability issues). but then i start wondering about the safety of pages that use sessions - if there are unsynchronized methods in the page, could the sessions become crossed by separate threads handling different sessions calling the methods that access page variables? or am i worrying about something that tomcat takes care of internally?
Thanks for the help,
Jon
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All very key worries, they represent the big conceptual jump one has to make when moving to web services.
Tomcat creates a separate Thread with separate request and response objects for every request, so your doGet or doPost method starts with its own objects.
Tomcat also manages sessions so that there is a separate session object for each user - note that a user may have multiple requests at work - say for a frameset plus images, etc.
You must not use any instance variables for user specific data, or, as you say, they will be shared among all users. Instead, keep user specific data in the session. A session can store any object and automatically make it available on the next request by that user. Any objects you need can be created as local references inside servlet methods.
Note that there is no question of synchronization unless you are using instance variables to store user specific data.
Bill
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!