cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot seem to read properties file from JSP

Former Member
0 Kudos

Hi all,

In an ISA 4.0 application I want to read a properties file from a JSP to decide whether or not to execute some specific JavaScript code. I want the properties file because in it I can easily configure this externally. I put my properties file in /WEB-INF/classes.

I develop on an environment using Tomcat where this works fine. On an acceptance machine using the SAP J2EE engine however this won't work, to my surprise. Can anyone give me a suggestion on why my properties file is not read?

Here's the Java scriptlet code:

 UserSessionData userSessionData = UserSessionData.getUserSessionData(session);
 // theme will be something like "themes/zester":
 String theme = (String) userSessionData.getAttribute(Constants.THEME_KEY);

 boolean showScript = false;
 int slashIndex = theme.indexOf('/');
 String themeKey = null;
 // get the "zester"-like part from the theme:
 if(theme != null && slashIndex > -1) {
  themeKey = theme.substring(slashIndex + 1);
 }
 Properties props = new Properties();

 InputStream is = application.getResourceAsStream("/WEB-INF/classes/script.properties");

 if(is != null) {
  props.load(is);
  is.close();
  Enumeration keysEnum = scriptProps.propertyNames();
  while (themeKey != null && keysEnum.hasMoreElements()) {
   if(theme.toUpperCase().endsWith((String) keysEnum.nextElement())) {
    showScript = true;
    break;
   }
   showScript = false;
  }
 }

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

PS what actually seems to fail here is reading the properties file into the InputStream object; the reference

 InputStream is

holds

 null

Moreover when, again in the JSP, I list the resourcePaths from the ServletContext in the HTML as follows:

<%
 Iterator pathsIt = application.getResourcePaths("/WEB-INF/classes/").iterator();
 while(pathsIt.hasNext()) { %>
    <%= pathsIt.next() %>;
<%
 } %>

my /script.properties file is listed!

kishorg
Advisor
Advisor
0 Kudos

Hi Peter ,

i think u can use ResourceBundle for this.

1) in NWDS , create one package .

for example com.test.bundle

2) put ur property file in this package itself.(Test.properties)

3) u can access this resource in this way..

ResourceBundle bundle = new ResourceBundle("com.test.bundle.Test");

use this bundle object. this have number of methods . wecan make it enumeration . then loop through this and use it.

details of ResourceBundle here

http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html

Regards

Kishor Gopinathan

Former Member
0 Kudos

Hi Kishor,

Many thanks, you've just saved my day!

It worked the way you described with the - indeed minor - remark that I need to instantiate the Resourcebundle using

ResourceBundle res = ResourceBundle.getBundle("com.bla.script")

and catch (ignore a MissingResourceException.

I also tried to load the ResourceBundle directly from the classes folder. But as you said, you need the fully qualified name (com.bla.script) from a package, where script maps to my script.properties file with the default Locale.

I still do not get the reason why I cannot read a properties file from my resources in the classes folder. In my application, Struts does the same, albeit that Struts uses the servlet's ClassLoader.

Many thanks indeed,

Peter-Jan Bosch

kishorg
Advisor
Advisor
0 Kudos

Hi Peter ,

i had face the same problem . i tried to access the property file as u explained . but i got Null pointer exception.

after doing some search on this , i could realize that

the SAP J2EE WAS is not supporting the relative path in some what extend.

but other j2ee servers are supporting this.

it might be bcoz of the difference in the deployement

of applications in these two servers.

when we deploy one EAR in tomcat the EAR file is deployed as in Folder Structure format. but in SAP J2EE WAS the EAR file is in the ZIP supported format.

so when we took relative path refernces here , it will not support it..

Regards

Kishor Gopinathan

Former Member
0 Kudos

Hi Kishor,

Thank you for taking the time!

I agree, this issue may have to do with the deployment. There is however a remark in the <a href="http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletContext.html">ServletContext</a> interface documentation on methods <a href="http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletContext.html#getResource(java.lang.String)">getResource()</a> and <a href="http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String)">getResourceAsStream()</a> that may also be of interest here:

"The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource."

Is this remark of relevance to the subject, or is this thought too far fetched? Mind you, the documentation also states that "This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file". It should, therefore, be possible to read a file from disk???

Cheers,

Peter-Jan Bosch

Answers (0)