File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple servlet question

 
Vinnie Jenks
Ranch Hand
Posts: 207
  • 0
  • 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
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic