using .properties file in web app

H

harryos

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
 
T

Tom Anderson

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
 
L

Lew

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.
 
T

Tom Anderson

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
 
L

Lew

Tom Anderson said:
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.
 
H

harryos

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,229
Latest member
GloryAngul

Latest Threads

Top