tags:

views:

55

answers:

2

Hello all, I am developing a portlet that is trying to read in a config file. I am developing it in an eclipse project. I currently have the config file placed inside my WEB-INF folder (which is in root/WEB-INF/), and its called config.properties. How can I access this file using relative path in my java source code? (which is in root/src/package/mysource.java)

For example,

File myfile = new File("WHAT DO I PUT HERE/config.properties");

Any help you can provide would be great!

+1  A: 

I would load that using the servlet context and its getResourceAsStream() method instead of a File. Like this....

Sorry, you said portlet. You'll need javax.portlet.PortletContext then.

You can get the PortletContext from the PortletSession. You can create a PortletSession from a PortletRequest.

duffymo
I am making a portlet for liferay, so I dont think I can use that?
ApacheImageUser
Use PortletContext, then. It also has a getResourceAsStream method.
duffymo
How can I call getPortletContext()?
ApacheImageUser
From the PortletSession, of course: http://portals.apache.org/pluto/portlet-api/apidocs/javax/portlet/PortletSession.html
duffymo
How do I get the portletRequest? lol
ApacheImageUser
It's sent to you. It's a parameter passed into the action or render request: http://www.bluesunrise.com/portlet-api/index.html
duffymo
+1  A: 

Since this is a portlet, you'll probably want to use the PortletContext.

Although there is a getRealPath method, I would avoid it as it is dependent on how the portal application is deployed - there is no guarantee that the resource is going to map to a file on the file system. Go instead for getResourceAsStream (or, if you must, getResource).

McDowell
How do I declare a new PortletContext?PortletContext context = ??
ApacheImageUser
It is provided by the container. You can get it from the `GenericPortlet`, or the `PortletSession`, or the `PortletRequest`, or the `PortletConfig`.
McDowell
Im developing my portlet in Java, based on the ZK framework. Right now, I have a class that extends from a GenericForwardComposer so Im not sure how to get those things.
ApacheImageUser
@ApacheImageUser - that sort of information should be stated up front (I've tagged the question); I am not familiar with that framework.
McDowell