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


Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to