You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Shankar Unni <sh...@netscape.net> on 2004/12/27 23:47:27 UTC

How to access s from Servlet.init()?

I'd like to configure a set of web-app-level parameters for use by both 
Java-based Servlets, and JSP pages.  I thought of setting these up as 
<context-param>s at the <web-app> level in web.xml.

For JSPs, all is cool: <%= application.getInitParameter("foo") %> 
returns these parameters.

For Servlets, how do I get at them from the init() method? (I need them 
there!). I find that calling config.getInitParameter() doesn't return 
these (where "config" is the parameter to init() - it only returns the 
actual <init-param>s configured within the <servlet>).

Also, this.getServletConfig() is null, and this.getServletContext() 
throws an NPE from within javax.servlet.GenericServlet.

(All this with Tomcat 5.0.27).

How do I get at a <context-param> from within Servlet.init()?


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: How to access s from Servlet.init()?

Posted by Shankar Unni <sh...@netscape.net>.
QM wrote:

> Which init() overload do you use?  
> 	init()
> 	init( ServletConfig )
> 
> Using the latter, you should be able to call:
> 	ServletConfig#getServletContext() --> getInitParameter()

Hah. Thank you! Yes, I mean the latter version.


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: How to access s from Servlet.init()?

Posted by QM <qm...@brandxdev.net>.
On Mon, Dec 27, 2004 at 02:47:27PM -0800, Shankar Unni wrote:
: For JSPs, all is cool: <%= application.getInitParameter("foo") %> 
: returns these parameters.
: 
: For Servlets, how do I get at them from the init() method? (I need them 
: there!). I find that calling config.getInitParameter() doesn't return 
: these (where "config" is the parameter to init() - it only returns the 
: actual <init-param>s configured within the <servlet>).

Which init() overload do you use?  
	init()
	init( ServletConfig )

Using the latter, you should be able to call:
	ServletConfig#getServletContext() --> getInitParameter()

The other poster had the right idea, though: create a ContextListener
that populates a local (app-specific) object with this data. Code
your app to fetch its needed info from this object.

-QM

-- 

software  -- http://www.brandxdev.net
tech news -- http://www.RoarNetworX.com


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org