Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Re: servlets - application level object (http://www.velocityreviews.com/forums/t123970-re-servlets-application-level-object.html)

John C. Bollinger 07-02-2003 02:35 PM

Re: servlets - application level object
 
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
jobollin@indiana.edu


Miguel De Anda 07-02-2003 07:22 PM

Re: servlets - application level object
 

"John C. Bollinger" <jobollin@indiana.edu> wrote in message
news:bduqjn$v6s$1@hood.uits.indiana.edu...
> 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
> jobollin@indiana.edu
>


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.



John C. Bollinger 07-02-2003 10:00 PM

Re: servlets - application level object
 
Miguel De Anda wrote:
> "John C. Bollinger" <jobollin@indiana.edu> wrote in message
> news:bduqjn$v6s$1@hood.uits.indiana.edu...
>
>>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
>>jobollin@indiana.edu

>
> 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
jobollin@indiana.edu



All times are GMT. The time now is 04:57 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.