Instantiate expensive object *once* in JSP/Tomcat

Discussion in 'Java' started by Dave, Jan 27, 2007.

  1. Dave

    Dave Guest

    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?
    Dave, Jan 27, 2007
    #1
    1. Advertising

  2. Dave wrote:
    > 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
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jan 27, 2007
    #2
    1. Advertising

  3. On 27.01.2007 17:15, Arne Vajhøj wrote:
    > Dave wrote:
    >> 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


    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
    Robert Klemme, Jan 27, 2007
    #3
  4. Robert Klemme wrote:
    > On 27.01.2007 17:15, Arne Vajhøj wrote:
    >> Dave wrote:
    >>> 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

    >
    > 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
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jan 28, 2007
    #4
  5. On 28.01.2007 04:20, Arne Vajhøj wrote:
    > Robert Klemme wrote:
    >> On 27.01.2007 17:15, Arne Vajhøj wrote:
    >>> Dave wrote:


    >>>> 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
    >> 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.


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

    Kind regards

    robert
    Robert Klemme, Jan 28, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Dalberg

    Least expensive server pdf control?

    John Dalberg, Sep 15, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    368
    John Dalberg
    Sep 15, 2005
  2. Replies:
    0
    Views:
    4,350
  3. tiewknvc9
    Replies:
    4
    Views:
    1,750
  4. Replies:
    4
    Views:
    143
    Anthony Jones
    Oct 5, 2006
  5. Gancy
    Replies:
    4
    Views:
    173
    Rasto Levrinc
    Feb 3, 2005
Loading...

Share This Page