On Wed, 27 Nov 2002, Rasputin wrote:
> Date: Wed, 27 Nov 2002 18:43:46 +0000 > From: Rasputin <[EMAIL PROTECTED]> > Reply-To: Tomcat Users List <[EMAIL PROTECTED]>, > Rasputin <[EMAIL PROTECTED]> > To: Tomcat Users List <[EMAIL PROTECTED]> > Subject: Re: setting Context attributes in web.xml/server.xml? > > * Craig R. McClanahan <[EMAIL PROTECTED]> [1153 17:53]: > > > > On Wed, 27 Nov 2002, Rasputin wrote: > > > > > * Andy Eastham <[EMAIL PROTECTED]> [1140 16:40]: > > > > > Rasputin, > > > > It's in web.xml > > > > Sorry, I can't see it. > > > > > > There's a > > > > > > <context-param> > > > > > > element, but I need a context *attribute*, and I don't think they > > > can be set in tomcat. > > > The <context-param> element, as you note, sets a context init > > parameter, not a context attribute. Context > > attributes must be set programatically, not from a web.xml file. > > Gotcha - makes sense actually, as getInitParameter returns a String, > whereas getAttribute returns an Object. > > > A convenient way to set things up is to create a ServletContextListener > > (assumes you're running Tomcat 4 or later) and configure it in a > > <listener> element in web.xml. The contextInitialized() method will be > > called when your web application is first started, and that is a perfect > > place to set up whatever context attributes you need. > > Lovely, that's the sort of mechanism I was looking for - is it portable > between servlet containers though? ServletContextListener is indeed portable to any Servlet 2.3 or later container. APIs that start "java." or "javax." are portable (although sometimes version specific), while APIs that start "org.apache." are not portable. :-). > For the record, the load-on-startup hack works pretty well. Cheers. > Load-on-startup is indeed a hack (although it's about the only way to do this for pre-Servlet-2.3), because there is no guarantee that the servlet container will leave your servlet in memory for the lifetime of the web application. Craig -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>