using .properties file in web app

Discussion in 'Java' started by harryos, Dec 4, 2008.

  1. harryos

    harryos Guest

    i am trying to write a web app where a jsp page uses ResourceBundle to
    get a property name and looks up its value from a include.properties
    file.

    include.properties
    -------------------------
    external-include=WEB-INF/jspf/header_tag.jsp

    The main jsp contains
    <html>
    <% java.util.ResourceBundle bundle =
    java.util.ResourceBundle.getBundle("include");
    String segment = bundle.getString("external-include");%>
    <jsp:include page="<%=segment %>"/>
    <body>
    ....
    </body>
    </html>

    I have put the include.properties in WEB-INF directory.
    When i try to access the main jsp page ,I get this error=>
    java.util.MissingResourceException: Can't find bundle for base name
    include, locale en_US.

    Can someone help me figure out how to correct this?

    thanks
    harry
     
    harryos, Dec 4, 2008
    #1
    1. Advertising

  2. harryos

    Tom Anderson Guest

    On Thu, 4 Dec 2008, harryos wrote:

    > i am trying to write a web app where a jsp page uses ResourceBundle to
    > get a property name and looks up its value from a include.properties
    > file.
    >
    > include.properties
    > -------------------------
    > external-include=WEB-INF/jspf/header_tag.jsp
    >
    > The main jsp contains
    > <html>
    > <% java.util.ResourceBundle bundle =
    > java.util.ResourceBundle.getBundle("include");
    > String segment = bundle.getString("external-include");%>
    > <jsp:include page="<%=segment %>"/>
    > <body>
    > ...
    > </body>
    > </html>
    >
    > I have put the include.properties in WEB-INF directory.
    > When i try to access the main jsp page ,I get this error=>
    > java.util.MissingResourceException: Can't find bundle for base name
    > include, locale en_US.


    Where exactly have you put the properties file? Resources need to be on
    your classpath. If your classpath is rooted in WEB-INF/classes, as is
    often the case, you need to put the properties file in there, rather than
    in WEB-INF itself.

    tom

    --
    I'm angry, but not Milk and Cheese angry. -- Mike Froggatt
     
    Tom Anderson, Dec 4, 2008
    #2
    1. Advertising

  3. harryos

    Lew Guest

    On Dec 4, 11:41 am, Tom Anderson <> wrote:
    > On Thu, 4 Dec 2008, harryos wrote:
    > > i am trying to write a web app where a jsp page uses ResourceBundle to
    > > get a property name and looks up its value from a include.properties
    > > file.

    >
    > > include.properties
    > > -------------------------
    > > external-include=WEB-INF/jspf/header_tag.jsp

    >
    > > The main jsp contains
    > > <html>
    > > <% java.util.ResourceBundle bundle =
    > > java.util.ResourceBundle.getBundle("include");
    > >      String segment = bundle.getString("external-include");%>
    > > <jsp:include page="<%=segment %>"/>
    > > <body>
    > > ...
    > > </body>
    > > </html>

    >
    > > I have put the include.properties in WEB-INF directory.
    > > When i try to access the main jsp page ,I get this error=>
    > > java.util.MissingResourceException: Can't find bundle for base name
    > > include, locale en_US.

    >
    > Where exactly have you put the properties file? Resources need to be on
    > your classpath. If your classpath is rooted in WEB-INF/classes, as is
    > often the case, you need to put the properties file in there, rather than
    > in WEB-INF itself.


    The web-app classpaths is set by the container, and generally includes
    the application root directory (the one above WEB-INF/) and WEB-INF/
    classes/ both.

    So it should be possible to refer to the properties file in WEB-INF/
    as "WEB-INF/foo.properties", or whatever name. Possible, but not
    actually correct.

    Tom's advice is correct, because a properties file is used by the Java
    classes, rather than by the display layer, so properly belongs in the
    WEB-INF/classes/ rooted part of the classpath.

    --
    Lew
     
    Lew, Dec 4, 2008
    #3
  4. harryos

    Tom Anderson Guest

    On Thu, 4 Dec 2008, Lew wrote:

    > On Dec 4, 11:41 am, Tom Anderson <> wrote:
    >> On Thu, 4 Dec 2008, harryos wrote:
    >>> i am trying to write a web app where a jsp page uses ResourceBundle to
    >>> get a property name and looks up its value from a include.properties
    >>> file.

    >>
    >>> include.properties
    >>> -------------------------
    >>> external-include=WEB-INF/jspf/header_tag.jsp

    >>
    >>> The main jsp contains
    >>> <html>
    >>> <% java.util.ResourceBundle bundle =
    >>> java.util.ResourceBundle.getBundle("include");
    >>>      String segment = bundle.getString("external-include");%>
    >>> <jsp:include page="<%=segment %>"/>
    >>> <body>
    >>> ...
    >>> </body>
    >>> </html>

    >>
    >>> I have put the include.properties in WEB-INF directory.
    >>> When i try to access the main jsp page ,I get this error=>
    >>> java.util.MissingResourceException: Can't find bundle for base name
    >>> include, locale en_US.

    >>
    >> Where exactly have you put the properties file? Resources need to be on
    >> your classpath. If your classpath is rooted in WEB-INF/classes, as is
    >> often the case, you need to put the properties file in there, rather than
    >> in WEB-INF itself.

    >
    > The web-app classpaths is set by the container, and generally includes
    > the application root directory (the one above WEB-INF/) and WEB-INF/
    > classes/ both.


    Ah, i stand corrected.

    Is the classpath controlled by entries in the WAR's manifest?

    tom

    --
    I'm angry, but not Milk and Cheese angry. -- Mike Froggatt
     
    Tom Anderson, Dec 4, 2008
    #4
  5. harryos

    Lew Guest

    Lew wrote:
    >> The web-app classpaths is set by the container, and generally includes
    >> the application root directory (the one above WEB-INF/) and WEB-INF/
    >> classes/ both.



    Tom Anderson <> wrote:
    > Ah, i stand corrected.
    >
    > Is the classpath controlled by entries in the WAR's manifest?


    I don't believe so. According to my reading and experiments it
    depends on which classloader you use. The standard is to look in
    <app>/WEB-INF/classes/ for the root of classes and resources, and any
    JARs in <app>/WEB-INF/lib/. The container classloader will also look
    in <app>/. There are a couple of subtleties I don't remember offhand,
    dependencies on whether you're using 'ServletContext#getResource
    ()' ('getResourceAsStream()') or the same-named methods from 'Class'
    or 'ClassLoader'.

    The Tomcat docs explain how there are multiple classloaders in that
    product:
    <http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html>

    More full-blown Java EE containers like WebSphere or whatever have
    even more sophisticated class-loading strategies.

    --
    Lew
     
    Lew, Dec 4, 2008
    #5
  6. harryos

    harryos Guest

    On Dec 5, 12:34 am, Lew <> wrote:
    > Tom's advice is correct, because a properties file is used by the Java classes, rather than by the display layer, so properly belongs in the WEB-INF/classes/ rooted part of the classpath.


    thanks for the replies,

    I packed the include.properties file into WEB-INF/classes directory
    using the following

    <war....>
    ....
    <zipfileset prefix ="WEB-INF/classes"
    dir="${include.props.dir}" >
    <include name="**/*.properties" />
    </zipfileset>
    </war>
    Now the jsp can find the file using ResourceBundle.getBundle
    ("include")

    Thanks again guys
    harry
     
    harryos, Dec 5, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bml6YW0=?=

    convert java web app 2 .NET web app

    =?Utf-8?B?bml6YW0=?=, Feb 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    395
    =?Utf-8?B?bml6YW0=?=
    Feb 28, 2006
  2. =?Utf-8?B?bml6YW0=?=

    convert java web app 2 .NET web app

    =?Utf-8?B?bml6YW0=?=, Feb 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    412
    =?Utf-8?B?bml6YW0=?=
    Feb 28, 2006
  3. Replies:
    1
    Views:
    411
    Patrice
    Feb 9, 2007
  4. Sonal
    Replies:
    3
    Views:
    671
  5. Maras
    Replies:
    0
    Views:
    118
    Maras
    Jun 17, 2004
Loading...

Share This Page