| Author |
Shared instance variable
|
Ron Miller
Greenhorn
Joined: Jun 28, 2010
Posts: 12
|
|
Can someone explain how multi-threading works in a container when there are multiple http requests all happening at the same time?
I want to save the servletConfig object in a private variable declared in HttpServlet but I am worried about whether one request can see the servletConfig of another request.
Thanks.
|
 |
Michael Angstadt
Ranch Hand
Joined: Jun 17, 2009
Posts: 272
|
|
|
Only one ServletConfig instance exists per servlet. So, because multiple threads run through a servlet (one thread per HTTP request), each thread will be accessing the same ServletConfig instance. Given ServletConfig's methods, do you think this will be a problem?
|
SCJP 6 || SCWCD 5
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56168
|
|
Ron Miller wrote:I want to save the servletConfig object in a private variable declared in HttpServlet
Why? For what possible purpose?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Ron Miller
Greenhorn
Joined: Jun 28, 2010
Posts: 12
|
|
Bear Bibeault wrote:
Ron Miller wrote:I want to save the servletConfig object in a private variable declared in HttpServlet
Why? For what possible purpose?
I am studying for SCWCD that's why.
so if I declare a variable at servlet level the variable will be shared among all Http Requests? (and therefore not a good way to save info specific to that user, session, or request)
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56168
|
|
Snippy is not a winning attitude. Knowing why someone wants to do something is usually key to figuring out the best way to accomplish something.
Moved to the SCWCD forum.
|
 |
Michael Angstadt
Ranch Hand
Joined: Jun 17, 2009
Posts: 272
|
|
Bear Bibeault wrote:
Ron Miller wrote:I want to save the servletConfig object in a private variable declared in HttpServlet
Why? For what possible purpose?
I think what Bear was getting at was that you don't need to store the ServletConfig in a variable. You can retrieve it anytime by calling getServletConfig().
Ron Miller wrote:so if I declare a variable at servlet level the variable will be shared among all Http Requests? (and therefore not a good way to save info specific to that user, session, or request)
Yes, you have to make sure your servlets are thread safe.
|
 |
Ron Miller
Greenhorn
Joined: Jun 28, 2010
Posts: 12
|
|
|
Understood. Thanks.
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Shared instance variable
|
|
|