Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Instantiate expensive object *once* in JSP/Tomcat

Thread view: 
Dave - 27 Jan 2007 09:29 GMT
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?
Arne Vajhøj - 27 Jan 2007 16:15 GMT
> 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
Robert Klemme - 27 Jan 2007 16:30 GMT
>> I have an object that is very expensive in terms of time (it reads in
>> an XML mapping file).
[quoted text clipped - 5 lines]
>   - 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
Arne Vajhøj - 28 Jan 2007 03:20 GMT
>>> I have an object that is very expensive in terms of time (it reads in
>>> an XML mapping file).
[quoted text clipped - 10 lines]
> 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
Robert Klemme - 28 Jan 2007 10:22 GMT
>>>> 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?

>> A more proper solution is to implement the interface that listens to
>> application events.  The startup event would create that object and
[quoted text clipped - 6 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.