Re: servlets - application level object

Discussion in 'Java' started by John C. Bollinger, Jul 2, 2003.

  1. Miguel De Anda wrote:
    > Is there some application level object that I can use to store settings such
    > as database connection info and directory settings? I need something that is
    > at least readable throughout my servlets (writing settings after initialized
    > is not important).


    In addition to the other suggestions, you can also store objects as
    attributes of the ServletContext, from which any servlet can later
    retrieve them. (This can in fact be combined with the
    ServletContextListener approach, because the ServletContextListener can
    add attributes to its associated ServletContext -- even itself.)


    John Bollinger
    John C. Bollinger, Jul 2, 2003
    #1
    1. Advertising

  2. "John C. Bollinger" <> wrote in message
    news:bduqjn$v6s$...
    > Miguel De Anda wrote:
    > > Is there some application level object that I can use to store settings

    such
    > > as database connection info and directory settings? I need something

    that is
    > > at least readable throughout my servlets (writing settings after

    initialized
    > > is not important).

    >
    > In addition to the other suggestions, you can also store objects as
    > attributes of the ServletContext, from which any servlet can later
    > retrieve them. (This can in fact be combined with the
    > ServletContextListener approach, because the ServletContextListener can
    > add attributes to its associated ServletContext -- even itself.)
    >
    >
    > John Bollinger
    >
    >


    Would the ServletContext method (not getResourceAsStream) be considered more
    'standard'? We've just moved to jsp and want to do things the way they are
    normally done. It seems like using getResourceAsStream would have to be
    called anytime I need to read properties right? That seems inefficient,
    especially since these are properties that will be needed for more pages.
    Miguel De Anda, Jul 2, 2003
    #2
    1. Advertising

  3. Miguel De Anda wrote:
    > "John C. Bollinger" <> wrote in message
    > news:bduqjn$v6s$...
    >
    >>Miguel De Anda wrote:
    >>
    >>>Is there some application level object that I can use to store settings

    >
    > such
    >
    >>>as database connection info and directory settings? I need something

    >
    > that is
    >
    >>>at least readable throughout my servlets (writing settings after

    >
    > initialized
    >
    >>>is not important).

    >>
    >>In addition to the other suggestions, you can also store objects as
    >>attributes of the ServletContext, from which any servlet can later
    >>retrieve them. (This can in fact be combined with the
    >>ServletContextListener approach, because the ServletContextListener can
    >>add attributes to its associated ServletContext -- even itself.)
    >>
    >>
    >>John Bollinger
    >>

    >
    > Would the ServletContext method (not getResourceAsStream) be considered more
    > 'standard'? We've just moved to jsp and want to do things the way they are
    > normally done. It seems like using getResourceAsStream would have to be
    > called anytime I need to read properties right? That seems inefficient,
    > especially since these are properties that will be needed for more pages.


    For objects that need to persist and especially those that have state
    that may change, storing references as ServletContext attributes is the
    standard means provided by the servlet API. Such attributes might be
    populated from a stream obtained via ServletContext.getResourceAsStream
    if they are static and many or large, but might be better off as context
    parameters (defined in web.xml) if they are static, few, and small.
    Dynamic objects, such as database connections, must of course be created
    at runtime, but the parameters for their creation might come from
    context parameters. Once you have that DB connection you can of course
    use the DB for persistent storage (of most things) if you so choose.

    John Bollinger
    John C. Bollinger, Jul 2, 2003
    #3
    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. Replies:
    2
    Views:
    438
  2. Wendy S
    Replies:
    0
    Views:
    788
    Wendy S
    Jul 1, 2003
  3. Phil Hanna
    Replies:
    0
    Views:
    712
    Phil Hanna
    Jul 2, 2003
  4. pabbu
    Replies:
    8
    Views:
    719
    Marc Boyer
    Nov 7, 2005
  5. Jéjé
    Replies:
    0
    Views:
    232
    Jéjé
    Sep 27, 2005
Loading...

Share This Page