• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

JSP and PropertyResourceBundle

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Poonam Yadav
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried your way too I got the same error, remember my file name is test.properties
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it's compiled but I am still getting the same error.
 
Poonam Yadav
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic