File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet question" Watch "Servlet question" New topic
Author

Servlet question

Bob Moranski
Ranch Hand

Joined: Nov 22, 2000
Posts: 177
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

Joined: Mar 22, 2000
Posts: 12760
    
    5
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

Joined: Nov 22, 2000
Posts: 177
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

Joined: Jan 31, 2000
Posts: 1467
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

Joined: Nov 22, 2000
Posts: 177
Thank you so very much maha!
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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

Joined: Nov 22, 2000
Posts: 177
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.
 
 
subject: Servlet question
 
Similar Threads
difference between Generic servlet & Httpservlet
request object failing to pass in parameters correctly
http request handling
HttpServelt, Servlet
GenericServlet