*
The moose likes JSP and the fly likes JSP and PropertyResourceBundle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP and PropertyResourceBundle" Watch "JSP and PropertyResourceBundle" New topic
Author

JSP and PropertyResourceBundle

Poonam Yadav
Greenhorn

Joined: Sep 09, 2003
Posts: 7
Hi,
I am trying to use PropertyResourceBundle in a jsp file. I am using following code:
<% PropertyResourceBundle res = (PropertyResourceBundle) PropertyResourceBundle.getBundle("test.properties");
// we get text in the same way
String strHello = res.getString("HELLO_TEXT");
String strGoodbye = res.getString("GOODBYE_TEXT");
%>
test.properties file resides in WEB-INF/classes dir. But when I try to run my jsp page I get following error:
java.util.MissingResourceException: Can't find bundle for base name test.properties, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:707)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:604)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:546)

Please help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60739
    
  65

Hi Poonam, welcome to the Ranch!
The most likely problem is that current directory is not what you think it is, and the properties file is not being found.
You will either need to specify an absolute path to the file, or use ServletContext.getRealPath() to find the path to the file within the web application.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Poonam Yadav
Greenhorn

Joined: Sep 09, 2003
Posts: 7
Hi Bear,
thanks for the reply. This is the first time I am trying this. So I reaally didn't follow what do you mean by this. I have apache server with Broadvision as application server. I tried your getRealPath() also. I get following error:
org.apache.jasper.JasperException: Unable to compile class for JSP/local/home/bvadmin/bv1to1_var/scratchpart/bvdev1_yad_0/_0002fproperty_0002ejspproperty_jsp_10.java:1402: No method matching getRealPath() found in interface javax.servlet.ServletContext.
out.println(ServletContext.getRealPath()) ;

Could you please send me some code example.
Thanks in advance
P
Poonam Yadav
Greenhorn

Joined: Sep 09, 2003
Posts: 7
hi,
I was able to work with getRealPath, but I am still getting following error:
Can't find bundle for base name /local/home/yad99999/scripts/partner/WEB-INF/classes/test.properties, locale en_US
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
This is a stab in the dark, but when you work with ResourceBundles, you don't need to specify the ".properties" in the name. Thus if you have a properties file named "test.properties", then you can read it in by using:
ResourceBundle res = ResourceBundle.getBundle("test");
Note that if you declare it as "ResourceBundle" instead of "PropertyResourceBundle" that you don't need to cast it, and you can still call the "getString(...)" method.
If you want to put "test.properties" in a sub-directory, say "properties", then you would load it by using:
ResourceBundle res = ResourceBundle.getBundle("properties/test");
This assumes that you have a directory .../WEB-INF/classes/properties.
The main thing is to remove the ".properties" from the resource name.
Poonam Yadav
Greenhorn

Joined: Sep 09, 2003
Posts: 7
Hi,
I just tried the solution you offered:
Now new code looks like:
PropertyResourceBundle res = PropertyResourceBundle.getBundle("test");
// we get text in the same way
String strHello = res.getString("HELLO_TEXT");
String strGoodbye = res.getString("GOODBYE_TEXT");
and test.properties is in WEB-iNF/classes dir.
But I still get following error:
Can't find bundle for base name test, locale en_US
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
I tried the code you provided and it didn't compile correctly. Here's what you have:

I believe you should try:

or

The first method is preferable. Even though "res" is declared as a "ResourceBundle", you actually end up with an instance of "PropertyResourceBundle", which is what you want.
Poonam Yadav
Greenhorn

Joined: Sep 09, 2003
Posts: 7
I tried your way too I got the same error, remember my file name is test.properties
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
The "ResourceBundle" class automatically appends ".properties" to whatever name you specify, and it changes all periods (.) to path separators (/). So if the actual file name is "test.properties", and it is in your "../WEB-INF/classes" directory, then to load it you want to use:
ResourceBundle res = ResourceBundle.getBundle("test");
As you are changing your JSP, are you re-deploying it to the correct directory? Is it getting re-compiled? If you are using TomCat, you should have a ..\work\Standalone\localhost directory where the source files for the JSPs are kept in a sub-directory with that context name. You should be able to find the source for your JSP and make sure that has the latest version of your code. If not, it may mean that the version of the JSP that you are changing isn't getting deployed into the Tomcat directory.
Poonam Yadav
Greenhorn

Joined: Sep 09, 2003
Posts: 7
Yes, it's compiled but I am still getting the same error.
Poonam Yadav
Greenhorn

Joined: Sep 09, 2003
Posts: 7
The code is working now. The change I did is name of the property file test_en_US.properties).
But the problem is if I make changes to property file, jsp doesn't reflect those changes until I recompile the jsp page.
Any suggestions...
Poonam
Srinivas Jag
Greenhorn

Joined: Jun 30, 2003
Posts: 9
Originally posted by Poonam Yadav:
The code is working now. The change I did is name of the property file test_en_US.properties).

I thought that if a locale specific file was not available, the default bundle, in this case 'test.properties' would be picked up. But that doesn't seem to be happening here. Any ideas?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSP and PropertyResourceBundle
 
Similar Threads
Populating the value into dropdown box from .propertied file
Including property file in the jar
Properties file
location of a property file
properties file in websphere