• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet question

 
Bob Moranski
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So all request to a HttpServlet gets the same object, but what if one request comes to the HttpServlet before another request is finished, will not the parameters, headers from the second request corrupt the first request?
Thanks!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each request gets its own Thread - all of the objects - such as the HttpServletRequest object - relevant to that request have references only in that Thread - in other words they are local references, not instance variables or class variables.
Getting used to this is the first big mental jump in understanding programming with servlets. You can't store data unique to a particular request/response in an instance variable of your servlet.
Bill

------------------
author of:
 
Bob Moranski
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bill! But you only refer to the variables defined within a doGet() or doPost method right? You can create fields that all threads of request can access by declaring it in the class scope?
All requests will get the same thisInstance object?
public class MyServlet extends HttpServlet{
Object thisInstance = new Object();
}
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob,
All individual request threads start their journey from service(...) method. So any variables defined/declared OUTSIDE service() method will be shared by all threads.
So all class variables and instance variables will be shared by all of those request threads. If we don't want them share, don't define in instance/class scope.
regds
maha anna
 
Bob Moranski
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so very much maha!
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I overlooked to tell this point. I am sure you know this already. The service(..) method will in turn call doGet(..) or doPost(..) depending upon the request header type. So all varaibles defined inside service(..) or doGet(...) or doPost(..) are specific to each thread and not shared by other request threads.
regds
maha anna

[This message has been edited by maha anna (edited December 09, 2000).]
 
Bob Moranski
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I know that. Just not was confused about how each request uses the same object yet still kept its fields uncorrupted. Now I know. Thank you maha.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic