Instantiate expensive object *once* in JSP/Tomcat

D

Dave

I have an object that is very expensive in terms of time (it reads in an XML
mapping file).

How do I load this object into memory once - and once only - when the server
starts? How do I access this object in my standard JSP code?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Dave said:
I have an object that is very expensive in terms of time (it reads in an XML
mapping file).

How do I load this object into memory once - and once only - when the server
starts? How do I access this object in my standard JSP code?

One idea:
- make it a singleton
- create a startup servlet that references it

Arne
 
R

Robert Klemme

One idea:
- make it a singleton
- create a startup servlet that references it

A more proper solution is to implement the interface that listens to
application events. The startup event would create that object and
place it in the web application's context while the shutdown even would
trigger cleanup code.

Kind regards

robert
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Robert said:
A more proper solution is to implement the interface that listens to
application events. The startup event would create that object and
place it in the web application's context while the shutdown even would
trigger cleanup code.

Regarding application context instead of singleton: then in general
that is a very bad design because all the code using it becomes
tied to being used in web app context. If it is only to be used
in JSP pages then no problem, they are already tied to web app
context.

Regarding ServletContextListener instead of startup servlet: fine.

Arne
 
R

Robert Klemme

Regarding application context instead of singleton: then in general
that is a very bad design because all the code using it becomes
tied to being used in web app context. If it is only to be used
in JSP pages then no problem, they are already tied to web app
context.

That's the exact reason why I proposed this approach here: OP asked for
access from JSP code. :)

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top