views:

38

answers:

2

I have implemented my domain layer classes and i have used them in a java application. Now i want to use same classes in a java web application,but i don't know how can i do it? In the java application we make and run some objects in main(class and method) and use them while program is running. for example an object that hold a collection of data that will be needed for all user requests. My question is: How can i create and hold such objects and data that should be available for all users and clients.

+1  A: 

Place them in the application context. I.e. in a Servlet call getServletContext().setAttribute("name", yourCollection);

Then they can be retrieved by getServletContext().getAttribute("name")

Bozho
The [`ServletContextListener`](http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContextListener.html) may be useful to load (and destroy) the data in question during webapp's startup (and shutdown).
BalusC
A: 

You could create a singleton which represents the common application logic, initialising and cleaning up the common information and objects on web-app startup and shutdown.

All servkets can use the application singleton to retrieve and store.

To initialise and cleanup on web-app startup and shutdown you can define an administrative servlet which has the load-on-startup flag set in the deployment descriptor web.xml managing the application singleton from the init() and destroy() methods.

rsp