my dog learned polymorphism
The moose likes Struts and the fly likes Struts file path problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts file path problem" Watch "Struts file path problem" New topic

Struts file path problem

Dave Hendler

Joined: May 23, 2006
Posts: 1
I'm trying to figure out how access an XML file with FileReader somewhere in my webapps's directory without resorting to absolute file paths. Is there a way to have a custom config file in, say, WEB-INF and be able to load that without specifying the full path?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You can use the ServletContext object's getRealPath() method to find the actual path for a given virtual path. So, to get the actual path for your context root, you'd code the following in your Actoin class:

String contextPath = getServlet().getServletContext().getRealPath("/");

Once you have that, you can get any other path relative to it. For example:

String webInfPath = contextPath + "/WEB-INF";

Consultant, Sima Solutions
I agree. Here's the link:
subject: Struts file path problem
It's not a secret anymore!