Mike Engelhart wrote:

> Craig McClanahan wrote:
> >For PropertyResourceBundle objects, Java resolves resource package names using
> the
> > same class path it uses for finding classes.  So, if you have a property
> bundle
> > named "com.mycompany.mypackage.Resources.properties", it should be in the same
> > place (either directory or nested in a JAR/ZIP file) that you would put a
> class
> > named "com.mycompany.mypackage.MyClass.class".  You would access it by saying:
> >
> >     ResourceBundle bundle =
> > ResourceBundle.getBundle("com.mycompany.mypackage.Resources");
> >
>
> Yeah, i figured out the path situation after I wrote some test servlets.
> Thanks.
>
> Regarding ResourceBundles, when the API docs say that this class caches
> resource bundles so that subsequent calls to getBundle won't reload the
> property files, does that mean, I can just call the static
> "ResourceBundle.getBundle("myResourceBundle")" from anywhere in my
> application and get the cached object that is stored in my LocaleBean object
> or do I have to specifically call the resource bundle that I have created to
> share with my servlets and do something like:
>
> LocaleBean lb = getServletContext().getAttribute("LocaleBean");
> ResourceBundle rb = getResourceBundle();
> rb.getBundle("myResourceBundle");
>
> Thanks again.
>
> mike

Your second statement (calling getResourceBundle()) won't work unless you've
created such a method in your JSP page or servlet.

You only have to load the bundle once -- I normally do this in the init() method of
a startup servlet:

    ResourceBundle rb =
ResourceBundle.getBundle("com.mycompany.mypackage.Resources");
    getServletContext().setAttribute("bundleBean", rb);

to make the bundle accessible to all the servlets (and JSP pages) in my
application.  In a servlet, you retrieve a reference to the existing bundle like
this:

    ResourceBundle rb = (ResourceBundle)
getServletContext().getAttribute("bundleBean");

or in a JSP page like this:

    <jsp:useBean name="bundleBean" scope="application"
     type="java.util.ResourceBundle">

Accessing the cached resource bundle is considerably faster than reloading the
resources every time, if you're talking about property file based resources.  For
other implementations (such as a ListResourceBundle) it probably doesn't make much
difference.

In my real apps, I don't like having to deal with the MissingResourceException that
many of the methods of ResourceBundle throw, so I actually wrap it in a class that
converts these into zero length strings (more useful than null values in a
JSP page) if you try to look up a resource key that doesn't exist.

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

Reply via email to