users
[Top] [All Lists]

Re: [cinjug-users] properties file in WAR

To: "Matt Avery" <mavery@xxxxxxxxxxxxxxx>
Subject: Re: [cinjug-users] properties file in WAR
From: "James Carman" <james@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 26 Jun 2003 14:07:31 -0400
Cc: <users@xxxxxxxxxx>
Delivered-to: mailing list users@cinjug.org
Mailing-list: contact users-help@cinjug.org; run by ezmlm
References: <20030626123554.43175.qmail@web21412.mail.yahoo.com> <006501c33be8$78befda0$6601a8c0@NAMRAC2247> <3EFB18F9.1000907@einnovation.com> <00be01c33c00$a1f04310$6601a8c0@NAMRAC2247> <3EFB3304.4050606@einnovation.com>
But, the ResourceBundle class does not support loading bundles (property
file based bundles anyway) this way.  You can't tell it to load them using
the ServletContext.  It HAS to load them using a ClassLoader.  And, the
ResourceBundle class is THE Java way of doing internationalization.  But,
sometimes we have to do things in different ways as a work-around for a
non-compliant app server like Web$phere.  I'm not saying that the
ServletContext.getResource() method is not useful.  I use it all the time.
I'm just saying that the default behaviour of the ResourceBundle class is to
load things from the classpath and you CAN put your properties file on the
classpath in a J2EE-compliant fashion by placing it in the WEB-INF/classes
directory.  That has to be in the classpath, per the Servlet 2.3 (and
earlier versions) specification.  I am VERY surprised that the folks at IBM
would have missed such a trivial thing like this.  Are you sure this was
really the issue?  Try deploying my war file in Web$phere and see if it
works.  I would do it, but I don't have a copy.


----- Original Message ----- 
From: "Matt Avery" <mavery@xxxxxxxxxxxxxxx>
To: "James Carman" <james@xxxxxxxxxxxxxxxxxxxx>
Cc: <users@xxxxxxxxxx>
Sent: Thursday, June 26, 2003 1:53 PM
Subject: Re: [cinjug-users] properties file in WAR


> No, I didn't neglect that fact.  If you use
> ServletContext.getResourceAsStream() (e.g
> i18n_resources/en_US.properties) you can always locate the resource
> relative to the top of your webapp instead of having to always put it in
> WEB-INF/classes.
>
> Also, we have had problems in the past with loading resources out of
> WEB-INF/classes using WebSphere.  Admittedly, this is because WebSphere
>   bent the spec a little with their classloader gyrations, but
> nevertheless, it didn't work.  Just use
> ServletContext.getResourceAsStream() since it works in any appserver and
> it doesn't matter if the war file is packed or unpacked.
>
> James Carman wrote:
> > Matt, you are neglecting something.  The ClassLoader can load resources
out
> > of ALL roots.  And the J2EE spec DOES account for this.  It declares
that
> > the WEB-INF/classes directory is one of the root directories for the
> > webapp's classloader.  So, putting a properties file in the
WEB-INF/classes
> > directory IS a J2EE compliant way to do it.
> >
> > ----- Original Message ----- 
> > From: "Matt Avery" <mavery@xxxxxxxxxxxxxxx>
> > To: <users@xxxxxxxxxx>
> > Sent: Thursday, June 26, 2003 12:02 PM
> > Subject: Re: [cinjug-users] properties file in WAR
> >
> >
> >
> >>If the ResourceBundle static method is indeed using the
> >>ClassLoader.getResourceAsStream() method (I haven't looked at it) this
> >>could explain the difference in behavior.  There is no telling where the
> >>ClassLoader will determine the "root" to be because this is not of the
> >>J2EE spec.  Different appservers can implement the classloading in
> >>different ways, for example, the classloader's root when running JBoss
> >>is in <JBOSS_HOME>/bin, while in WebSphere it is in <WebSphere
> >>root>/apps, niether of which have anything to do with where the war file
> >>lives.
> >>
> >>The question here is why do we have a properties file and does it need
> >>to change per appserver.  If it is an external resource because you
> >>don't want to hard code something (e.g. internationalized text), then it
> >>should be deployed with the war file and accessed with the
> >>ServletContext.getResourceAsStream() because this is part of the J2EE
> >>spec and will give you the same behavior on every appserver.
> >>
> >>If the properties are specific to the server, it would make more sense
> >>to get them out of JNDI or otherwise put them on the server.  In any
> >>case, I would not recommend using the ClassLoader.getResourceAsStream()
> >>method to load up the properties file because you will have to determine
> >>the correct location for every appserver because the root of the
> >>ClassLoader is not part of the J2EE spec.
> >>
> >>James Carman wrote:
> >>
> >>>Yes, the ResourceBundle class CAN use an absolute path, but it is not
in
> >>>this case (at least by the code shown it isn't).  Here's a WAR file you
> >>>can deploy in your environment.  If it doesn't work, then you've got
> >>>something wrong, as it works in the reference implementation (Tomcat
> >>>4.1.24).  Basically, I have one index.jsp file that just loads up a
> >>>ResourceBundle to find one string called "name" with the value "James
> >>>Carman."  It merely prints "Hello, James Carman!" when it works
> >>>properly.  The properties file (MessageResources.properties) is located
> >>>in WEB-INF/classes and the ResourceBundle class picks it up just fine.
> >>>If you try moving the MessageResources.properties file to the document
> >>>root, it fails!
> >>>
> >>>    ----- Original Message -----
> >>>    *From:* Maoshi GU <mailto:maoshi@xxxxxxxxx>
> >>>    *To:* James Carman <mailto:james@xxxxxxxxxxxxxxxxxxxx> ; Herbers,
> >>>    Joe <mailto:joe.herbers@xxxxxxx>
> >>>    *Cc:* users@xxxxxxxxxx <mailto:users@xxxxxxxxxx>
> >>>    *Sent:* Thursday, June 26, 2003 8:35 AM
> >>>    *Subject:* Re: [cinjug-users] properties file in WAR
> >>>
> >>>    ResourceBundle can use an absolute path, e.g, /usr/local, where you
> >>>    can put your property files. If no defined, it uses where your
class
> >>>    file lives. In your case, I believe it gets the file from your jsp
> >>>    file directory, which may be the doc root.
> >>>
> >>>    */James Carman <james@xxxxxxxxxxxxxxxxxxxx
> >>>    <mailto:james@xxxxxxxxxxxxxxxxxxxx>>/* wrote:
> >>>
> >>>        Ok, first of all you should NOT do this in JSP code. If you
> >>>        really want
> >>>        this sort of support, either use Struts or JSTL. Anyway, it
> >>>        looks like it
> >>>        SHOULD be placed in the WEB-INF/classes directory based upon
> >>>        this code. The
> >>>        ResourceBundle class will use the ClassLoader.getResource()
> >>>        approach. I'd
> >>>        have to see your war file setup to figure out why it's not
> >
> > working.
> >
> >>>
> >>>        ----- Original Message -----
> >>>        From: "Herbers, Joe"
> >>>        To: "James Carman"
> >>>        Sent: Wednesday, June 25, 2003 4:35 PM
> >>>        Subject: RE: [cinjug-users] properties file in WAR
> >>>
> >>>
> >>>        Well, this JSP code calls the static method
> >>>        ResourceBundle.getBundle Here's
> >>>        the code (which I didn't write). Thanks for the help.
> >>>
> >>>        userLang = request.getParameter ("txtLanguage");
> >>>        userCountry = request.getParameter ("txtCountry");
> >>>        if (userLang != null && userCountry != null)
> >>>        {
> >>>        userLocale = new Locale (userLang, userCountry);
> >>>        }
> >>>        //default locale is US
> >>>        else
> >>>        {
> >>>        userLocale=Locale.US;
> >>>        }
> >>>        session.putValue("myLocale",userLocale);
> >>>        bundle = ResourceBundle.getBundle("Message",userLocale);
> >>>        for (Enumeration e = bundle.getKeys();e.hasMoreElements();) {
> >>>        String key = (String)e.nextElement();
> >>>        String s = bundle.getString(key);
> >>>        session.putValue(key,s);
> >>>        }
> >>>        String REQATTR_LOGINERROR =
> >>>        (String)session.getValue("launch.loginerror");
> >>>
> >>>
> >>>        -----Original Message-----
> >>>        From: James Carman [mailto:james@xxxxxxxxxxxxxxxxxxxx]
> >>>        Sent: Wednesday, June 25, 2003 4:20 PM
> >>>        To: Herbers, Joe; Timothy Dennison; CinJUG (E-mail)
> >>>        Subject: Re: [cinjug-users] properties file in WAR
> >>>
> >>>        Well, it depends on how you're loading the file. Are you using
> >
> > the
> >
> >>>        ServletContext.getResource() or ClassLoader.getResource()
> >>>        method? If you
> >>>        use the ServletContext.getResource() method, the searching is
> >>>        done rooted at
> >>>        the root of your webapp. But, if you use the classloader, it
> >>>        will be rooted
> >>>        at WEB-INF/classes (or the root of one of your jars in
> >
> > WEB-INF/lib).
> >
> >>>        ----- Original Message -----
> >>>        From: "Herbers, Joe"
> >>>        To: "James Carman" ; "Timothy Dennison"
> >>>        ; "CinJUG (E-mail)"
> >>>        Sent: Wednesday, June 25, 2003 3:23 PM
> >>>        Subject: RE: [cinjug-users] properties file in WAR
> >>>
> >>>
> >>>        Sorry I'm slow following up to these helpful responses to my
> >>>        question. I
> >>>        just got around to testing this out. It only worked when I put
> >>>        the file
> >>>        (messages.properties) in the WAR's root dir. It couldn't locate
> >>>        it if I put
> >>>        it in WEB-INF/classes or WEB-INF/lib. This was using an
exploded
> >>>        WAR in
> >>>        JBoss' deploy dir.
> >>>
> >>>        So I can just put it in the root when I make a WAR file. But
I'm
> >>>        curious
> >>>        how this is supposed to work. James mentioned reading the
docs -
> >
> > any
> >
> >>>        recommendations for docs on how WAR files can be put together?
> >>>
> >>>        Thanks, Joe
> >>>
> >>>
> >>>        -----Original Message-----
> >>>        From: James Carman [mailto:james@xxxxxxxxxxxxxxxxxxxx]
> >>>        Sent: Saturday, June 14, 2003 12:52 PM
> >>>        To: Timothy Dennison; CinJUG (E-mail)
> >>>        Subject: Re: [cinjug-users] properties file in WAR
> >>>
> >>>        You can put it in the WEB-INF/classes directory as Tim has
> >>>        suggested, or it
> >>>        can be in the top-level of any of the jars in the WEB-INF/lib
> >>>        directory and
> >>>        it will also be found. If you're using Tomcat. Try doing a
> >>>        System.out.println() on one of your classes' ClassLoader
object.
> >>>        It will
> >>>        give you a rundown of all of the places where it will look for
> >>>        resources.
> >>>        It's VERY helpful when trying to figure out where things are
> >>>        loaded from and
> >>>        in what order. The docs help too. :-) I would suggest using the
> >>>        WEB-INF/classes directory, though. That's the absolute first
> >>>        place it will
> >>>        look.
> >>>
> >>>
> >>>        ----- Original Message -----
> >>>        From: "Timothy Dennison"
> >>>        To: "CinJUG (E-mail)"
> >>>        Sent: Saturday, June 14, 2003 6:15 AM
> >>>        Subject: Re: [cinjug-users] properties file in WAR
> >>>
> >>>
> >>>         > You should be able to put the file in WEB-INF/classes
> >>>         > as this location will always be on the classpath.
> >>>         > tim
> >>>         >
> >>>         > --- "Herbers, Joe" wrote:
> >>>         > > I'm trying to create a WAR file to deploy an app.
> >>>         > > One class references a resource file called
> >>>         > > message.properties via the following code. Where in
> >>>         > > the WAR should/can I put the properties file? In
> >>>         > > the root? I think it needs to be in the classpath,
> >>>         > > so I do I need to put a CLASSPATH statement in
> >>>         > > Manifest.mf or can I put it somewhere that will
> >>>         > > automatically be in the classpath? Thanks.
> >>>         > >
> >>>         > > bundle =
> >>>         > > ResourceBundle.getBundle("Message",userLocale);
> >>>         > >
> >>>
> >>>
> >>>
> >>>
>
  ------------------------------------------------------------------------
> >
> >>>    Do you Yahoo!?
> >>>    SBC Yahoo! DSL
> >>>
> >
> >
<http://pa.yahoo.com/*http://rd.yahoo.com/evt=1207/*http://promo.yahoo.com/s
> > bc/>
> >
> >>>    - Now only $29.95 per month!
> >>
> >>-- 
> >>Matthew Avery
> >>Senior Developer
> >>(513) 470-5316
> >>http://www.einnovation.com/
> >>
> >>
> >>
> >>
> >
> >
> >
> >
>
> -- 
> Matthew Avery
> Senior Developer
> (513) 470-5316
> http://www.einnovation.com/
>
>
>
>


<Prev in Thread] Current Thread [Next in Thread>