• Post Reply Bookmark Topic Watch Topic
  • New Topic

resource path  RSS feed

 
shaun michael
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to access an xml file from my servlet using File xmlFile = new File("/menu.xml"), however the servlet keeps looking in C:\ for the file. How come it doesn't recognize this as a relative path and look in the web app for the file?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use context.getResourceAsStream();

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html #getResourceAsStream(java.lang.String)
[ January 08, 2005: Message edited by: Ben Souther ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File looks from the root of the filesystem and knows nothing of your web application.
In fact, if you had the servlet engine running on a different harddrive from your classfiles File would look on the root of that drive and not on the root of the drive containing your classfiles.

You can get the root of your webapplication from the ServletContext and append the relative path to your diskfiles to that before passing the whole construct to your File object for opening.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!