aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Shared instance variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Shared instance variable" Watch "Shared instance variable" New topic
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: 274

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: 61601
    
  67

Ron Miller wrote:I want to save the servletConfig object in a private variable declared in HttpServlet

Why? For what possible purpose?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by 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: 61601
    
  67

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: 274

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.
 
 
subject: Shared instance variable