aspose file tools*
The moose likes Servlets and the fly likes Making an object accessible to servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Making an object accessible to servlets" Watch "Making an object accessible to servlets" New topic
Author

Making an object accessible to servlets

Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
I wasn't really sure what to name this post or where to put it, so I apologize if I'm littering.

I wrote an object that allows for the manipulation and access of user accounts for a webapp I'm writting. How can I make that object available to all servlets and also be able to ensure a certain line of code is executed if the container (tomcat) restarts or undeploys the app?

Thank you for atleast reading this post,
-D.P.
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Clarification:
The object loads an xml file of data into a hashmap when it is instantiated, any changes to that hashmap should eventually be saved.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61652
    
  67

Originally posted by Daniel Prene:

I wrote an object that allows for the manipulation and access of user accounts for a webapp I'm writting. How can I make that object available to all servlets


Depends what you mean by "available". If you want to make it so that the object class is available to servlets so that they can instantiate an instance of the class, then be sure that the class is on the claspath and import it into the servlet classes.

If you by "available" you mean to make an instance of the object available (which I suspect is what you may mean by your follow-on question), then storing the instance in application context will make that instance available to all servlets (be sure to account for synchronization when you do).


and also be able to ensure a certain line of code is executed if the container (tomcat) restarts or undeploys the app?


ServletContextListener
[ January 30, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Thank you! One more question though;

storing the instance in application context

How is that done? Code-wise?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#setAttribute(java.lang.String,%20java.lang.Object)


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Making an object accessible to servlets