Less than 42 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet global variables  RSS feed

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I know that these are not safe, but my question is how can I declare a variable in a servlet that all or most methods in the servlet have access to w/o passing it around from method to method?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a scoped variable in the appropriate context. Translation: context.setAttribute(). Determine the lifetime needed for the variable and choose one of request, session or application context.

But such implicitness can come with a confusion cost. If the variable is one that is only used in the processing of a single request, I would resist the urge to laziness and pass it to the methods that need it in the interest of clarity.

If it needs to live beyond the scope of the servlet, for example to be accessed on a forwarded JSP page, then I'd use the scoped variable mechanism.
[ October 05, 2004: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!