Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts file path problem

 
Dave Hendler
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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";
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic