You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mason Jones <ma...@clipshow.com> on 2000/08/24 20:57:21 UTC

How to get ServletContext in bean?

I'm hoping to use the context-params from my web.xml to
hold application-wide configuration information. This is
obviously easy to access from a servlet using the
ServletContext, but I'm not sure how to get the ServletContext
from within a bean that I'm using from a jsp page. If I have
to, I suppose I could put something at the top of each page
which could get the context and then it would be available,
but that doesn't seem very nice.

For example, say that I have a static method in a utility class
which gets a database connection; I'd like to be able to get at
a pair of context-params which define the user/password, so
that the info will be easy to change in the future. Is there an
easy way for a static method to find the ServletContext?

Thanks...


________________________________________
   Mason Jones              mason(at)clipshow.com
   Director of Software Development
   ClipShow, Inc.            www.clipshow.com
   (650) 696-3164           fax (650) 696-3267


Re: How to get ServletContext in bean?

Posted by Alistair Hopkins <al...@berthengron.co.uk>.
I subclass all my beans from an abstract class.

I then have a 'dummy class' extended from this which I instantiate to in 
the init() method of the servlet, giving it the property settings.  I keep 
this as a singleton, so it is not gc'd as long as the servlet is instantiated.

All other beans can then use the get methods of the superclass.

It works fine, although I'm not sure about a distributed environment.



At 03:56 PM 8/24/00 -0400, you wrote:
>Mason,
>At the moment, that is the way I'm doing it, so If you find a better
>way, please let me know too... :)
>Matt
>
>Mason Jones wrote:
>
> > Matt Goss wrote:
> > >Mason,
> > >send a ServletConfig object to the bean from the calling page and
> > >retrieve the ServletContext object from that with
> > >"myServletConfig.getServletContext()";
> >
> > Yeah, I know that I can use the PageContext from the JSP page
> > to get the ServletContext, so theoretically I can pass either one of
> > them to the bean. But I was hoping that instead of having to make
> > sure it's passed around everywhere, maybe there is some global
> > way to access the ServletContext. It would be a lot cleaner, but it
> > may not be possible.
> >
> > Thanks.
> >
> > Mason Jones wrote:
> >
> > > > I'm hoping to use the context-params from my web.xml to
> > > > hold application-wide configuration information. This is
> > > > obviously easy to access from a servlet using the
> > > > ServletContext, but I'm not sure how to get the ServletContext
> > > > from within a bean that I'm using from a jsp page. If I have
> > > > to, I suppose I could put something at the top of each page
> > > > which could get the context and then it would be available,
> > > > but that doesn't seem very nice.
> > > >
> > > > For example, say that I have a static method in a utility class
> > > > which gets a database connection; I'd like to be able to get at
> > > > a pair of context-params which define the user/password, so
> > > > that the info will be easy to change in the future. Is there an
> > > > easy way for a static method to find the ServletContext?
> > > >
> > > > Thanks...
> > > >
> > > > ________________________________________
> > > >    Mason Jones              mason(at)clipshow.com
> > > >    Director of Software Development
> > > >    ClipShow, Inc.            www.clipshow.com
> > > >    (650) 696-3164           fax (650) 696-3267
> >
> > ________________________________________
> >    Mason Jones              mason(at)clipshow.com
> >    Director of Software Development
> >    ClipShow, Inc.            www.clipshow.com
> >    (650) 696-3164           fax (650) 696-3267


Re: How to get ServletContext in bean?

Posted by Matt Goss <mg...@rtci.com>.
Mason,
At the moment, that is the way I'm doing it, so If you find a better
way, please let me know too... :)
Matt

Mason Jones wrote:

> Matt Goss wrote:
> >Mason,
> >send a ServletConfig object to the bean from the calling page and
> >retrieve the ServletContext object from that with
> >"myServletConfig.getServletContext()";
>
> Yeah, I know that I can use the PageContext from the JSP page
> to get the ServletContext, so theoretically I can pass either one of
> them to the bean. But I was hoping that instead of having to make
> sure it's passed around everywhere, maybe there is some global
> way to access the ServletContext. It would be a lot cleaner, but it
> may not be possible.
>
> Thanks.
>
> Mason Jones wrote:
>
> > > I'm hoping to use the context-params from my web.xml to
> > > hold application-wide configuration information. This is
> > > obviously easy to access from a servlet using the
> > > ServletContext, but I'm not sure how to get the ServletContext
> > > from within a bean that I'm using from a jsp page. If I have
> > > to, I suppose I could put something at the top of each page
> > > which could get the context and then it would be available,
> > > but that doesn't seem very nice.
> > >
> > > For example, say that I have a static method in a utility class
> > > which gets a database connection; I'd like to be able to get at
> > > a pair of context-params which define the user/password, so
> > > that the info will be easy to change in the future. Is there an
> > > easy way for a static method to find the ServletContext?
> > >
> > > Thanks...
> > >
> > > ________________________________________
> > >    Mason Jones              mason(at)clipshow.com
> > >    Director of Software Development
> > >    ClipShow, Inc.            www.clipshow.com
> > >    (650) 696-3164           fax (650) 696-3267
>
> ________________________________________
>    Mason Jones              mason(at)clipshow.com
>    Director of Software Development
>    ClipShow, Inc.            www.clipshow.com
>    (650) 696-3164           fax (650) 696-3267

Re: How to get ServletContext in bean?

Posted by Mason Jones <ma...@clipshow.com>.
Matt Goss wrote:
>Mason,
>send a ServletConfig object to the bean from the calling page and
>retrieve the ServletContext object from that with
>"myServletConfig.getServletContext()";

Yeah, I know that I can use the PageContext from the JSP page
to get the ServletContext, so theoretically I can pass either one of
them to the bean. But I was hoping that instead of having to make
sure it's passed around everywhere, maybe there is some global
way to access the ServletContext. It would be a lot cleaner, but it
may not be possible.

Thanks.

Mason Jones wrote:

> > I'm hoping to use the context-params from my web.xml to
> > hold application-wide configuration information. This is
> > obviously easy to access from a servlet using the
> > ServletContext, but I'm not sure how to get the ServletContext
> > from within a bean that I'm using from a jsp page. If I have
> > to, I suppose I could put something at the top of each page
> > which could get the context and then it would be available,
> > but that doesn't seem very nice.
> >
> > For example, say that I have a static method in a utility class
> > which gets a database connection; I'd like to be able to get at
> > a pair of context-params which define the user/password, so
> > that the info will be easy to change in the future. Is there an
> > easy way for a static method to find the ServletContext?
> >
> > Thanks...
> >
> > ________________________________________
> >    Mason Jones              mason(at)clipshow.com
> >    Director of Software Development
> >    ClipShow, Inc.            www.clipshow.com
> >    (650) 696-3164           fax (650) 696-3267


________________________________________
   Mason Jones              mason(at)clipshow.com
   Director of Software Development
   ClipShow, Inc.            www.clipshow.com
   (650) 696-3164           fax (650) 696-3267


Re: How to get ServletContext in bean?

Posted by Matt Goss <mg...@rtci.com>.
Mason,
send a ServletConfig object to the bean from the calling page and
retrieve the ServletContext object from that with
"myServletConfig.getServletContext()";
Matt Goss

Mason Jones wrote:

> I'm hoping to use the context-params from my web.xml to
> hold application-wide configuration information. This is
> obviously easy to access from a servlet using the
> ServletContext, but I'm not sure how to get the ServletContext
> from within a bean that I'm using from a jsp page. If I have
> to, I suppose I could put something at the top of each page
> which could get the context and then it would be available,
> but that doesn't seem very nice.
>
> For example, say that I have a static method in a utility class
> which gets a database connection; I'd like to be able to get at
> a pair of context-params which define the user/password, so
> that the info will be easy to change in the future. Is there an
> easy way for a static method to find the ServletContext?
>
> Thanks...
>
> ________________________________________
>    Mason Jones              mason(at)clipshow.com
>    Director of Software Development
>    ClipShow, Inc.            www.clipshow.com
>    (650) 696-3164           fax (650) 696-3267

Re: How to get ServletContext in bean?

Posted by Todd Lipcon <tl...@mercea.net>.
I'm not sure if there's an easy way to do exactly what you're asking, but
I'll tell you what I'm working on right now... The way my project works is
this- There's a generic Bean class. Every other Bean extends that class.
The generic Bean provides getDBConnection, releaseDBConnection, and a
bunch of logging stuff. I'm in the process of writing a Singleton that
will provide access to a properties file for everything in the project-
Just create a Singleton that loads global.properties from the classpath,
and then have your database management class access the properties
singleton for the user/pass.

Sorry if that's a little confusing... I'm bad at explaining that sort of
thing without drawing pictures :)

-Todd

+---------------------------------------------------------------+ 
| Perl CGI scripting                    email tlipcon@csoft.net |
| Web Design                            email tlipcon@csoft.net |
|        My Current Projects:                                   |
| NewsWire				www.squirrelsw.com	|
| JavaGroove				www.squirrelsw.com	|
+---------------------------------------------------------------+

On Thu, 24 Aug 2000, Mason Jones wrote:

> 
> I'm hoping to use the context-params from my web.xml to
> hold application-wide configuration information. This is
> obviously easy to access from a servlet using the
> ServletContext, but I'm not sure how to get the ServletContext
> from within a bean that I'm using from a jsp page. If I have
> to, I suppose I could put something at the top of each page
> which could get the context and then it would be available,
> but that doesn't seem very nice.
> 
> For example, say that I have a static method in a utility class
> which gets a database connection; I'd like to be able to get at
> a pair of context-params which define the user/password, so
> that the info will be easy to change in the future. Is there an
> easy way for a static method to find the ServletContext?
> 
> Thanks...
> 
> 
> ________________________________________
>    Mason Jones              mason(at)clipshow.com
>    Director of Software Development
>    ClipShow, Inc.            www.clipshow.com
>    (650) 696-3164           fax (650) 696-3267
> 
>