This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple servlet question  RSS feed

 
Vinnie Jenks
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been learning java on and off for about a year now and while I've covered a lot of ground and built a few small applications successfully, I'm still confused about one simple thing - the scope of global variables in a servlet.

A servlet being multi-threaded, wouldn't global variables in a servlet be unsafe to rely on from request-to-request? For example, an object stored globally could change between two users and the data wouldn't be reliable, is that correct? What about a Hibernate session, as another example? Couldn't the session be closed in one thread invalidating another user's request to that session in another thread?

I was reading this article about optimizing servlets/jsp for performance:

http://javaboutique.internet.com/tutorials/tuning/index2.html

...they talk about using init() to make references to a data source, which is then stored in a global variable.

I'm using hibernate and would love it if I could call objects once in init() and not have to make subsequent calls each time the page is requested.

So, with init(), is it only called once per thread (per user request)?

Thanks in advance for clearing it up!

-v
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classes have members, not "globals"! Yes, init() is only called once, when a servlet is created.

Moving this to the "Servlets" forum.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be clear,

The init method is called once, when the servlet is loaded not once per request.

And you are correct, the ServletContext should not be considered thread-safe.
A general rule of thumb when using Servlet context is to write to it when the app loads and then treat the objects bound to it as read-only.
If you need multiple requests to read and write to context scope, you will need to synchronize your access to it.
 
Vinnie Jenks
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
Just to be clear,

The init method is called once, when the servlet is loaded not once per request.

And you are correct, the ServletContext should not be considered thread-safe.
A general rule of thumb when using Servlet context is to write to it when the app loads and then treat the objects bound to it as read-only.
If you need multiple requests to read and write to context scope, you will need to synchronize your access to it.


Alright, yes, I had read that...my mistake...once - not once per request. I guess I'm not clear when the objects bound in init() go *out* of scope exactly, I'll catch up on some reading.

So, when calling methods in jar libraries, for example, labeling the method "synchronized" should resolve my context problems? Or, would I need a local method in the servlet that is synchronized that would make the call to classes in the jars?

Thanks again!

-v
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vinnie Jenks:

I'm not clear when the objects bound in init() go *out* of scope exactly.
-v


When they go out of scope depends on what "scope" object you bind them to.
If you bind an object to context scope, it will be kept around for the life of your application (unless you explicitly remove it). Likewise session and request scope will keep objects around for the life of the session or request respectively.

Since the init method occurs before any requests are handled, the only scope you will have access to from the init method is context.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!