The following JUnit test is working in my application:
@Test
public void testMessageResources(){
final InputStream stream = getClass().
getResourceAsStream("/com/service/MessageResources.properties");
Assert.assertNotNull(stream);
}
But in my JSP, I can’t read the file using this code:
<%
final InputStream resourceAsStream = application.
getResourceAsStream("/com/service/MessageResources.properties");
%>
resourceAsStream is always null. I’m using JSP 2.1.
The MessageResources.properties file is on the classpath, but not inside the JSP directory. Is that a problem?
The JSP uses ServletContext.getResourceAsStream(), which doesn’t do the same thing as
Class.getResourceAsStream(). It doesn’t search for the resources in the classpath. If you want to useClass.getResourceAsStream(), then useClass.getResourceAsStream(). It will also work in a Java EE context. Just make sure to use a class loaded from the same classloader as the one you want to use to load your properties file.Such a method call shouldn’t be done in a JSP, though, but in a Servlet or action which forxards to the JSP. JSPs should be used to generate markup.