my dog learned polymorphism
The moose likes Servlets and the fly likes Global access to object between classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Global access to object between classes" Watch "Global access to object between classes" New topic

Global access to object between classes

Larry Homes

Joined: Jan 18, 2009
Posts: 25

I am writing a project with a couple of classses. The main class is a servlet and it makes class to all the other classes. In the init method of the servlet, I load and cache many things into repository-like objects so it doesn't have to instantiate make those objects everytime the servlet is called. Also, the servlet starts when tomcat starts. The problem is many of the classes the servlet calls needs those obejects, so for now, I have been passing the repositores around, but it is getting a little impractical. Some of the constructors are getting too many paramaters because of the objects that need to be passed around.

I was wondering, was there a way to make those objects global? Or some way to allow all classes to acccess those repository objects? I have googled for this, and I don't think there is, but I figured I should ask and make sure.

If caching these objects in repository like objects is the wrong way to load things on init, can you please provide recommendations as to better way to do so?

This was difficult to explain, so if any clarifications are required, I'd be happy to provide them.
Alecsandru Cocarla
Ranch Hand

Joined: Feb 29, 2008
Posts: 158
Check out ServletContext and its setAttribute() method. And maybe also take a short look in a book or on the net about servlets and servlet containers.

SCJP 1.4 100%
SCJD 99.5%
Larry Homes

Joined: Jan 18, 2009
Posts: 25
Yes, I already took a look at ServletConext. I thought that might not work because there is only one servlet, the main file. The rest of the classes called don't even import the servlet-api.jar.

Can someone tell me if I would be able to use getContext.getAttribute() in a regular class after the object has been bound to the context with the setAttributes method in the main servlet? It would be a shame to do all that work if it wouldn't work, though I will try anyway if no answers.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Well, clearly you can only use getContext().getAttribute("X") in a class with a getContext() method declared. And that would mean you can only realistically call it in a servlet.

But that shouldn't be a problem. Each servlet should be responsible for getting the attributes required and passing them to the objects that need them via parameters. If you have too many parameters, then maybe the problem is that the objects you store in the attributes are too fine-grained and they should be lumped together in some other objects which are easier to pass around.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

If you are concerned about the number of arguments, you can abstract the collection of these data items into a single object that can be passed around.

[Asking smart questions] [About Bear] [Books by Bear]
Larry Homes

Joined: Jan 18, 2009
Posts: 25
Yes, we considered abstracting it with a wrapper class, but decided that should be avoided if possible. We already have enough layers of abstraction. At this point though, it seems like we can either leave it as is, or wrap it in a class.

Oh well, thanks for your help.
I agree. Here's the link:
subject: Global access to object between classes
It's not a secret anymore!