Hans Gerwitz wrote:
> We would like to access some system-level properties in beans that we
> instantiate in JSPs. Presently, we've been fine with using init arguments
> to our JSP servlet (GNUJSP under Apache/JServ) and using
> config.getInitParameter() to explicitly set properties in the JSP. Now,
> though, we find ourselves wishing to have some parameters available at
> initialization, and have some design motivations to avoid passing certain
> environment properties in from JSP.
>
> Can I access the init parameters of the JSP page compilation servlet from
> within a bean? Even better, how would I use a .properties file of my own
> (or access properties of the JServ servlet zone?)
>
The latter (a properties file) is really easy to use, courtesy of the
java.util.ResourceBundle class. If you have a properties file named
"com/mycompany/myapp/MyResources.properties" (relative to the top directory of
your application, so this is the same place you would have Java source code in
package "com.mycompany.myapp"), you can access this properties file by saying:
ResourceBundle bundle = null;
try {
bundle = ResourceBundle.getBundle("com.mycompany.myapp.MyResources");
} catch (MissingResourceException e) {
... deal with the exception ...
}
Now, you can access the keys and values in this resource bundle according to the
methods of the ResourceBundle interface.
There are a couple of really cool things about doing this:
* The JVM loads these resource files based on your class path,
so it no longer matters what the runtime working directory of
your servlet engine is. It can even load these resource files from
a JAR file containing your classes, totally transparently.
* ResourceBundle also includes the ability to localize your resources
based on country and/or language. I've used this in a JSP based
app to internationalize all the prompts and labels of the app, so that
it is presented in (currently) six languages, but there's only one JSP
page source file to maintain.
There's more information about resource bundles in the documentation package for
the JDK 1.1 or 1.2 release.
>
> Thanks in advance,
> --
> [EMAIL PROTECTED]
>
Craig McClanahan
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html