tags:

views:

1587

answers:

4

I've gotten as far as this:

private Properties logoUrls = new Properties();
logoUrls.load(new FileInputStream("channelLogos.properties"));

where channelLogos.properties is in the same directory as my JSP. I get a FileNotFound exception. Where does my app actually think I mean by "channelLogos.properties", if not the same directory as the JSP? How can I determine the correct path to load the properties file?

+1  A: 

Take a look at ServletContext.getRealPath(). That should give you the full path to the properties file.

pkaeding
That's great to know about. Thanks! Using that technique, it shows me that my path is seemingly correct to my properties file, but I still get a file not foud exception. Any ideas?
morgancodes
+3  A: 

I'd like to highly recommend reading about Model 2 Servlets. I recommend it to everyone who's still doing Model 1 Servlets, that is, doing "real work" in a JSP.

As to your question: First, throw the properties file in your classpath, then read the file using getResourceAsSttream:

Thread.currentThread().getContextClassLoader().getResourceAsStream("channelLogos.properties");

There are many options, of course, and everyone will have their favorite.

Don Branson
I'm totally on board with the not doing real stuff in a jsp rule, but I'm breaking it in the case.
morgancodes
+1 for using the classpath
Thilo
@morgancodes: I'm totally on board with breaking my own rules from time to time. ;)
Don Branson
+3  A: 

This will do the job:

<%@page import="java.io.InputStream" %>
<%@page import="java.util.Properties" %>

<%
    InputStream stream = application.getResourceAsStream("/some.properties");
    Properties props = new Properties();
    props.load(stream);
%>

Anyway I really think you should have the properties file in the classpath and use a servlet

victor hugo
Yes, that would be the right way to do it. I'm admittedly doing this the quick, ugly, dirty, bad way.
morgancodes
Yes I know, hehe... I just answered what you asked ;)
victor hugo
FYI, there's an application.getResourceAsStream("...") that'll save you a few lines of code.
Jack Leow
@Jack Yeah you're right I edited
victor hugo
+1  A: 

When you say "same directory as JSP", what exactly do you mean by that? That your JSP sits somewhere in, say, /mywebapp/somefolder/my.jsp with mywebapp being your application root and your properties file is /mywebapp/somefolder/channelLogos.properties?

If so, then most likely they're NOT in the same directory. JSP has been compiled and where it is actually located may vary depending on servlet container. Your best bet is to use ServletContext.getRealPath() as suggested by pkaeding with properties file path relative to webapp context as an argument. Using the above example:

private Properties logoUrls = new Properties();
logoUrls.load(new FileInputStream(servletContext.getRealPath("/somefolder/channelLogos.properties")));

That said, keep in mind that if you insist on putting your properties in the same folder as JSP you should take care to restrict it from being publicly accessible (unless that's the intention).

ChssPly76
Thanks. This works. However, I was planning on doing all of this inside jspInit, which doesn't have easy access to the servletContext. Maybe a classpath approach really is best.
morgancodes
`getServletConfig().getServletContext()` will get you servletContext from within `jspInit` method. That said, classpath approach is totally better and MVC is better yet :-) I've only provided this answer because you've insisted that you want to do it in JSP alone.
ChssPly76