aspose file tools*
The moose likes Servlets and the fly likes Use of Servlet Context? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Use of Servlet Context?" Watch "Use of Servlet Context?" New topic
Author

Use of Servlet Context?

Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
Hi,

I've never really been certain about the use of the servlet context outside of defining a few context params in web.xml After learning a little about struts and inspecting the servlet context of the Struts application I noticed that the Action Servlet from Struts was present in the Servlet Context. After that observation I got an idea that I would extend Action Servlet from Struts and put my own Class Object in the Servlet Context so that I could have a better separation of the data and business layers of my application where my business class would live independendently and not rely on action classes to invoke it. Since that time I find that I want to put many service/business objects into the Servlet Context(usually only one per application module) to reduce the number of objects that my application creates. It made no sense to me to have each user create a new instance of the class when perhaps only one application scoped instance of a class may suffice in serving all requests. My question is whether or not this is a good approach to application design or am I making a huge mistake?

Thanks for your help.
[ May 24, 2006: Message edited by: Stu Higgs ]
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
I use this approach for over than 4 years, however I have no idea if it's recommended. I can suggest only to introduce Registry object which goes in context and all other objects are registered in Registry. In this case you can pull your set of objects easily from context and use them in some other place.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Bear in mind that that only makes sense for objects that are naturally singletons in your web app. Also, beware of threading issues since all threads could be accessing the same object simultaneously.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
Thanks for your replies. Bear, when you say Natural Singletons, would this qualify as one for example, I have a Legal Case Manager application with two primary objects, first is a JavaBean named Case which has all the properties, accessors and mutators for a Case. Second is a class Called CaseManager which contains all the business and service methods of the application. I have placed the CaseManager class in the Servlet context to handle requests from my Struts action classes. Would the CaseManager class be a Natural Singleton?

Thanks again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Does it make sense for all users to see the same instance of the object?

Is there absolutely nothing that one user can "do" to the object that will boof it up for everyone (or anyone) else?

Does the lifetime of the object need to span the entire lifetime of the web application?

If the answer to any one of those question is "no", then it doesn't qualify.
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
Thank you...
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
Hi,

I've given this some more thought and think I can explain why I asked this question to begin with and respond to the qualifying questions above as well and maybe get some more feedback I hope.

Question 1:
Does it make sense for all users to see the same instance of the object?


I'm not sure, but the object I am using is the only object used as a business object and through this object the properties of the bean are modified and access to the database is obtained etc..

Question 2:
Is there absolutely nothing that one user can "do" to the object that will boof it up for everyone (or anyone) else?


Yes.

Question 3:
Does the lifetime of the object need to span the entire lifetime of the web application?


I'm not sure, but I think this is where my original question comes into play. What I am trying to determine is does it make more sense and is it more efficient to serve all clients with the Servlet Context stored business object or is it more efficient to create a new instance of the business object for each client in several different places in the code whenever it is needed? For the latter case I also don't see how it is possible to obtain a clear separation of application layers if the business object lives and dies in the Action classes where it would be invoked. On the other hand if it is in the context then it really is separated and independent of the other layers becuase it is created on init and then persists.

Sorry this is so long a post, but I think this is an important concept and I do not want to implement a bad application design.
[ May 24, 2006: Message edited by: Stu Higgs ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

...through this object the properties of the bean are modified and access to the database is obtained...
I'm a little dubious about this. On the face of it, this sounds like it's shaping up to be a bottleneck as clients queue up for access to the database. Maybe you intend to have a connection pool behind this object, or maybe you intend to be caching data to reduce the number of database queries, but in any case you need to watch out for bottlenecks.
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
I do not want to confuse you, but you have also an option to convert your bean in enterprise bean. In this case EJB container will solve all problems related to instatntiation, synchranization, pooling, life cycle and so on. You can also use JNDI to access your bean.
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
Hi,

Thanks for the replies.

this sounds like it's shaping up to be a bottleneck as clients queue up for access to the database


Yes, a connectioon pool is used to hand out connections, but also clients will be queuing up and getting in line to execute CRUD methods via synchronized methods in the object.

For the EJB thanks for informing me about the option. I am currently learning how to work with Entity EJB'S and do not yet implement them in any applications but I am looking forward to using them in the future.

So my question then is which is more efficient? To create many instances of an object, or serve the clients with the one Object stored in the Servlet context? Maybe there is no clear cut answer? As I said earlier in the thread, I got this idea from learning about the Struts Single Threaded throughput Action Servlet. If storing Business layer objects in the Servlet context is or is not a common or recomended practice then that is the information I am seeking. Thanks again all...
[ May 25, 2006: Message edited by: Stu Higgs ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Efficiency should not be your prime concern unless or until a performance problem rears its head.

Rather, code what makes the most sense for the application structure.

It sounds like you may be falling into the "premature optimization" trap.
[ May 25, 2006: Message edited by: Bear Bibeault ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Use of Servlet Context?