You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Ryan O'Hara <oh...@genome.chop.edu> on 2006/07/31 18:16:41 UTC

Initializing Parameter

Is there a way to initialize an array (or any other Java object, for  
that matter), so that it is available to all users?  I would like to  
execute some Java methods to precompute and store two arrays to avoid  
creating them every visit for every user.  I read something about  
param-name and param-value in web.xml, however it looks like this  
only works for type String and does not allow Java code.  Thanks for  
any ideas.

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


Re: Initializing Parameter

Posted by Christopher Schultz <ch...@christopherschultz.net>.
David,

> ServletContextListener is a new feature of servlet spec 2.4 (tomcat
> 5.0.x, 5.5.x).

Correction: this is available in servlet spec 2.3 (Tomcat 4.x).

-chris


Re: Initializing Parameter

Posted by David Kerber <dc...@verizon.net>.
Where are you getting the parameter from?  Is it going to change a lot?  
I store parameters in the server.xml in the <GlobalNamingResources> 
section as <Environment> entries, and then retrieve them with a call to 
this routine:

    public static String getEnvironmentVariable( String envVarName, 
String varDefault) {
        String     parm;
        try    {
              InitialContext initCtx = new InitialContext();
              if    (initCtx == null) throw new Exception( "No Context 
for database setting" );
                  Context envCtx = (Context) initCtx.lookup( 
"java:comp/env" );
                  parm = (String) envCtx.lookup( envVarName );
           } catch(Exception e) {
            parm = varDefault;
           }
           return parm;
    }


.  These values don't change once the system is started, so it works for 
me.  You'll need to do something else if they can change at runtime.


Ryan O'Hara wrote:

> One more question:  What is the best method for retrieving the  
> values?  Thanks again.
>
> -Ryan
>
> On Jul 31, 2006, at 2:47 PM, David Smith wrote:
>
>> 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
>



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


Re: Initializing Parameter

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Ryan,

>> Not really.  If the question is "how do I retrieve the stuff I put in
>> servletcontext?", the answer is getServletContext().getAttribute(
>> attributeName ).
>
> Thanks a lot, David.

Yeah, sorry. I misunderstood the question the first time :(

So, your only option is to use ServletContext.getAttribute ;)

-chris



Re: Initializing Parameter

Posted by Ryan O'Hara <oh...@genome.chop.edu>.
Thanks a lot, David.

Ryan

> Christopher Schultz wrote:
>
> Not really.  If the question is "how do I retrieve the stuff I put  
> in servletcontext?", the answer is getServletContext().getAttribute 
> ( attributeName ).
> Then just cast the result to the type it's supposed to be (String,  
> Array, Hashtable, etc., ...) since getAttribute() returns type  
> Object for everything.
>
> --David
>
> ---------------------------------------------------------------------
> 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
>
>


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


Re: Initializing Parameter

Posted by David Smith <dn...@cornell.edu>.
Christopher Schultz wrote:

>Ryan,
>
>  
>
>>One more question:  What is the best method for retrieving the values? 
>>    
>>
>
>You only have one option: ServletContext.getInitParameter
>
>There are, however, some tools that can help you load data that is not
>String-based. Jakarta commons beanutils package (specifically the
>'converters' portion) can help to your string-to-whatever conversions.
>
>Hope that helps,
>
>-chris
>
>
>  
>
Not really.  If the question is "how do I retrieve the stuff I put in 
servletcontext?", the answer is getServletContext().getAttribute( 
attributeName ).
Then just cast the result to the type it's supposed to be (String, 
Array, Hashtable, etc., ...) since getAttribute() returns type Object 
for everything.

--David

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


Re: Initializing Parameter

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Ryan,

> One more question:  What is the best method for retrieving the values? 

You only have one option: ServletContext.getInitParameter

There are, however, some tools that can help you load data that is not
String-based. Jakarta commons beanutils package (specifically the
'converters' portion) can help to your string-to-whatever conversions.

Hope that helps,

-chris



Re: Initializing Parameter

Posted by Ryan O'Hara <oh...@genome.chop.edu>.
One more question:  What is the best method for retrieving the  
values?  Thanks again.

-Ryan

On Jul 31, 2006, at 2:47 PM, David Smith wrote:

> 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

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


Re: Initializing Parameter

Posted by David Smith <dn...@cornell.edu>.
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


Re: Initializing Parameter

Posted by Ryan O'Hara <oh...@genome.chop.edu>.
> You are creating a ServletContextListener, which must be configured in
> the "listeners" section of your web.xml file: simply mention the
> ServletContextListener in there, and the contextInitialized() method
> will be called when the webapp is initialized (i.e. once for the  
> life of
> the webapp).
>
> If you restart Tomcat, your webapp will be initialized again during
> startup and your code will execute again.
>
> -chris

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

=============

Chris and David,

Fantastic, thanks so much.  I'll give this a shot and see how it  
goes.  Your advice has been extremely helpful.  Thanks again!

-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


Re: Initializing Parameter

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Ryan,

> 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)?

It's right here:

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

> More specifically, would this be executed during a restart of Tomcat?
> If so, how (by what class/method)?

You are creating a ServletContextListener, which must be configured in
the "listeners" section of your web.xml file: simply mention the
ServletContextListener in there, and the contextInitialized() method
will be called when the webapp is initialized (i.e. once for the life of
the webapp).

If you restart Tomcat, your webapp will be initialized again during
startup and your code will execute again.

-chris



Re: Initializing Parameter

Posted by Ryan O'Hara <oh...@genome.chop.edu>.
> 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

Re: Initializing Parameter

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Ryan,

> Is there a way to initialize an array (or any other Java object, for
> that matter), so that it is available to all users?  I would like to
> execute some Java methods to precompute and store two arrays to avoid
> creating them every visit for every user.  I read something about
> param-name and param-value in web.xml, however it looks like this only
> works for type String and does not allow Java code.  Thanks for any ideas.

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