setting Context attributes in web.xml/server.xml?

2002-11-27 Thread Rasputin

Hi folks,

I'm porting a servlet-based reporting system from iPlanet to TOmcat 4.1.12,
and it initialises itself based on a context attribute 'baseDir'.

iPlanet lets you set initial context attributes through the server.
How do I set these in Tomcat4? Is it even possible? If iPlanet 4 has a feature tomcat
doesn't, I'd be amazed.

NB: I've tried context parameters, which tomcat does support, but
they're no good. To clarify, I need an attribute I can retrieve via

ServletContext con = getServletContext();
String dirname = (String) con.getAttribute(baseDir);

to return the value. Being able to do this in web.xml
would be extremely useful...

(Worst case scenario, I can write a new servlet and init-on-startup
it to explicitly set the attribute, but that's clankier
than a bicycle made of saucepans)

-- 
Rasputin :: Jack of All Trades - Master of Nuns

--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




RE: setting Context attributes in web.xml/server.xml?

2002-11-27 Thread Shapira, Yoav
Howdy,
You can do this in your app's web.xml:

context-param
  param-namebaseDir/param-name
  param-value/foo/bar/param-value
/context-param

And then in your servlet:
String baseDir = getServletContext().getInitParameter(baseDir);

 If iPlanet 4 has a feature tomcat doesn't, I'd be amazed.

If it were a spec-mandated feature, like context-params, then I'd be
more than amazed: I'd be fairly angry ;)

Yoav Shapira
Millennium ChemInformatics

--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




Re: setting Context attributes in web.xml/server.xml?

2002-11-27 Thread Rasputin
* 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.

 See sample chapter on www.moreservlets.com for more info.

Actually, I bought the book :)
--
Rasputin :: Jack of All Trades - Master of Nuns

--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




Re: setting Context attributes in web.xml/server.xml?

2002-11-27 Thread Craig R. McClanahan


On Wed, 27 Nov 2002, Rasputin wrote:

 Date: Wed, 27 Nov 2002 17:08:20 +
 From: Rasputin [EMAIL PROTECTED]
 Reply-To: Tomcat Users List [EMAIL PROTECTED],
  Rasputin [EMAIL PROTECTED]
 To: Andy Eastham [EMAIL PROTECTED]
 Cc: [EMAIL PROTECTED]
 Subject: Re: setting Context attributes in web.xml/server.xml?

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

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.


  See sample chapter on www.moreservlets.com for more info.

 Actually, I bought the book :)

Craig


--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




Re: setting Context attributes in web.xml/server.xml?

2002-11-27 Thread Boris Prochazka
Please read the online documentation page:

http://jakarta.apache.org/tomcat/tomcat-4.1-doc/config/context.html

Under heading Context Parameters.

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



See sample chapter on www.moreservlets.com for more info.



Actually, I bought the book :)
--
Rasputin :: Jack of All Trades - Master of Nuns

--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




--
  \\V//
  (o o)
--ooO--(_)--Ooo-
	You know you've achieved perfection in design,
		NOT when you have nothing more to add,
			but when you have nothing more to remove.
.
Boris Prochazka E-mail: [EMAIL PROTECTED]
Arenavagen 33, Box 101  http://www.ipunplugged.com
SE-121 28 Stockholm Globen  [home]   +46  8  - 6040786
[office] +46  8  - 7255919
SWEDEN 
	 
		[mobile] +46 70  - 5125122


--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]



Re: setting Context attributes in web.xml/server.xml?

2002-11-27 Thread Rasputin
* Boris Prochazka [EMAIL PROTECTED] [1159 17:59]:
 Please read the online documentation page:
 
 http://jakarta.apache.org/tomcat/tomcat-4.1-doc/config/context.html
 
 Under heading Context Parameters.

Sorry, to clarify :

 element, but I need a context *attribute*, and I don't think they
 can be set in tomcat.

context Parameter != context attribute.

Thanks anyway!

-- 
Rasputin :: Jack of All Trades - Master of Nuns

--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




Re: setting Context attributes in web.xml/server.xml?

2002-11-27 Thread Rasputin
* 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?
For the record, the load-on-startup hack works pretty well. Cheers.


-- 
Rasputin :: Jack of All Trades - Master of Nuns

--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




RE: setting Context attributes in web.xml/server.xml?

2002-11-27 Thread Shapira, Yoav
Howdy,

Lovely, that's the sort of mechanism I was looking for - is it portable
between servlet containers though?
For the record, the load-on-startup hack works pretty well. Cheers.

Yes, it's portable.  The ServletContextListener is part of the SRV 2.3
spec.

Yoav Shapira
Millennium ChemInformatics

--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




Re: setting Context attributes in web.xml/server.xml?

2002-11-27 Thread Craig R. McClanahan


On Wed, 27 Nov 2002, Rasputin wrote:

 Date: Wed, 27 Nov 2002 18:43:46 +
 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]




Re: setting Context attributes in web.xml/server.xml?

2002-11-27 Thread Rasputin
* Craig R. McClanahan [EMAIL PROTECTED] [1125 20:25]:

  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.

Right, thanks. It's more elegant, so is probably The Right Thing...
 
  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.

Damn, I was folowing all this up until you said that :)

Unless I've got this back-asswards, once the Attribute is saved in
the ServletContext ( which happens in init() ), won't it stay there?
I thought that was the point of a ServletContext?
Sure, it'll vanish when the server restarts, but if the
WorkAround servlet init() method replaces it during 'load-on-startup',
it shouldn't matter, should it?

-- 
Rasputin :: Jack of All Trades - Master of Nuns

--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]




Re: setting Context attributes in web.xml/server.xml?

2002-11-27 Thread Craig R. McClanahan


On Thu, 28 Nov 2002, Rasputin wrote:

 Date: Thu, 28 Nov 2002 01:19:24 +
 From: Rasputin [EMAIL PROTECTED]
 To: Craig R. McClanahan [EMAIL PROTECTED]
 Cc: [EMAIL PROTECTED]
 Subject: Re: setting Context attributes in web.xml/server.xml?

 * Craig R. McClanahan [EMAIL PROTECTED] [1125 20:25]:

   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.

 Right, thanks. It's more elegant, so is probably The Right Thing...

   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.

 Damn, I was folowing all this up until you said that :)

 Unless I've got this back-asswards, once the Attribute is saved in
 the ServletContext ( which happens in init() ), won't it stay there?
 I thought that was the point of a ServletContext?
   Sure, it'll vanish when the server restarts, but if the
 WorkAround servlet init() method replaces it during 'load-on-startup',
 it shouldn't matter, should it?

It will stay in the ServletContext attributes until you remove it, or
until the web application is shut down.

Most people who set things up in the init() method of a load-on-startup
servlet will also clean up after themselves in the destroy() method of the
same servlet -- for example, if you open a database connection at startup,
you generally want to close it at shutdown.  With a load-on-startup
servlet, there are no guarantees how many times this might happen during
the lifetime of your application.  With contextInitialized() and
contextDestroyed() methods of ServletContextListener, you get that
guarantee.  That's why the latter is better.

Craig


--
To unsubscribe, e-mail:   mailto:[EMAIL PROTECTED]
For additional commands, e-mail: mailto:[EMAIL PROTECTED]