tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Smith <d...@cornell.edu>
Subject Re: Initializing Parameter
Date Mon, 31 Jul 2006 18:47:40 GMT
ServletContextListener is a new feature of servlet spec 2.4 (tomcat 
5.0.x, 5.5.x).  The essential parts are:

1. write a class implementing the javax.servlet.ServletContextListener 
interface.  The interface itself requires two methods -- 
contextInitialized() [see below] and contextDestroyed().
2. Add the listener to your web.xml in a <listener> attribute.
3. Deploy your app and the contextInitialized() method below will be 
executed once each time the webapp is started.

The servlet spec itself and the servlet api javadocs have complete 
information on implementing this.

--David

Ryan O'Hara wrote:

>> You're looking for <init-param>. You can add these parameters to any
>> filter or servlet, or at the top-level for the entire webapp, and get
>> them when the filter or servlet runs (or any time you can get an
>> instance of the ServletContext which represents the webapp).
>> The usual way of doing this is to create a ServletContextListener  which
>> implements the init() method, grabs the values from the <init-param>
>> parameters, and stuffs them into the "application" scope. Although  this
>> is typically called the "application", you get it like this:
>>
>> public void contextInitialized(ServletContextEvent sce)
>> {
>>     ServletContext application = sce.getServletContext();
>>
>>     String myParam = application.getInitParameter("my-param");
>>
>>     application.setAttribute("myParam", new Integer(myParam));
>> }
>>
>> You can pretty much do anything you'd like at this point.
>>
>> Hope that helps,
>>
>> -chris
>
>
> Hey Chris,
>
> Awesome, I do in fact use param-name and param-value (init-param).   
> So, I understand I have to make an addition to web.xml like:
>
> <init-param>
>      <param-name>variableName</param-name>
>      <param-value>variableValue</param-value>
> </init-param>
>
> However, I'm not exactly sure what I'm supposed to do after this.  I  
> see that your code accounts for non-String variables by binding an  
> object to the param-name (via set—Āttribute()), but I'm not sure of a  
> couple of things.  Where would this code go that gets and sets the  
> value, so that it would only run once collectively for all users (not  
> once per user)?  More specifically, would this be executed during a  
> restart of Tomcat?  If so, how (by what class/method)?
>
> Thanks for your quick response.  Your help is greatly appreciated.
>
> -Ryan



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message