1

I have no problem loading and using properties file from JSP files which are located in the root of website (using ResourceBundle class) but when I try to load the same properties file from a JSP which is located in a directory it fails and says the resource can not be found!

Code of the page which is located in a directory

<%@page import="org.apache.log4j.Logger"%>
<%@page import="com.persiangulfcup.utility.LogUtils"%>
<%@page import="java.util.ResourceBundle"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
  Logger logger = LogUtils.getLogger("page/contact");
  ResourceBundle lbls = null;
  ResourceBundle msgs = null;
  try {
    lbls = ResourceBundle.getBundle("labels");
    msgs = ResourceBundle.getBundle("messages");
  } catch (Exception ex) {
    logger.fatal(ex);
  }
%>
<div class="form">
  <div style="text-align: left; font: normal bold 14px arial; cursor: pointer" onclick="contactBox.hide();">X</div>
  <div style="padding-bottom: 10px;font-size: 14px; text-align: center"><%=msgs.getString("contactHeader")%></div>
  <form id="frmContact" onsubmit="try {sendContact();} catch (e) {console.log(e)} return false;">
    <table class="form">
      <tr>
        <td class="caption"><%=lbls.getString("name")%>: </td>
        <td class="data">
          <input id="txtName" type="text" name="txtName"/>
        </td>
      </tr>
      <tr>
        <td class="caption"><%=lbls.getString("email")%>: </td>
        <td class="data">
          <input id="txtEmail" type="text" name="txtEmail"/>
        </td>
      </tr>
      <tr>
        <td class="caption"><%=lbls.getString("subject")%>: </td>
        <td class="data">
          <input id="txtSubject" type="text" name="txtSubject"/>
        </td>
      </tr>
      <tr>
        <td class="caption"><%=lbls.getString("message")%>: </td>
        <td class="data">
          <textarea id="txtMessage" name="txtMessage"></textarea>
        </td>
      </tr>
      <tr>
        <td class="button" colspan="2"><input type="submit" value="<%=lbls.getString("send")%>"/></td>        
      </tr>
      <tr>
        <td style="text-align: center" colspan="2" id="brdContact"></td>        
      </tr>
    </table>
  </form>
</div>
ehsun7b
  • 4,796
  • 14
  • 59
  • 98
  • Eeeek, scriptlets. Use JSTL fmt. http://stackoverflow.com/questions/4276061/how-to-internationalize-a-java-web-application – BalusC Jul 02 '11 at 21:47

1 Answers1

6

This is because you didn't respect a golden rule: don't put anything in the default package. A resource bundle is loaded as a class, from the classpath. It has a fully qualified name, which must be used to load it. And it's not possible to use a class in the default package from a class not in the default package.

So, put your resource bundle in an appropriate package (example : com.persiangulfcup.foo.bar), and load them like this : ResourceBundle.getBundle("com.persiangulfcup.foo.bar.labels").

That said, using scriptlets inside JSPs is a bad practice. You should really use the JSTL, which has a fmt library allowing to use resource bundles, format messages, etc.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • is it applicable for log4j properties too? And the pages in the root are like classes in the default package? – ehsun7b Jul 02 '11 at 10:04
  • The default place for log4j.properties is in the default package, but they're simple properties, not rersource bundles, and are thus not loaded through the same mechanism. Regarding the place of JSPs in the root, I think it depends on the server used, which chooses how to generate the corresponding classes. – JB Nizet Jul 02 '11 at 10:45
  • is it correct now? ResourceBundle lbls = ResourceBundle.getBundle("com.persiangulfcup.config.labels"); – ehsun7b Jul 02 '11 at 11:36
  • If the labels.properties file is in the com.persiangulfcup.config package, then yes, it's correct. – JB Nizet Jul 02 '11 at 12:09