• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shared instance variable

 
Ron Miller
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron Miller wrote:I want to save the servletConfig object in a private variable declared in HttpServlet

Why? For what possible purpose?
 
Ron Miller
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic