• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic File Path

 
Jhodee Rigodon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Good day. I am trying to access an xml file (books.xml) from index.jsp. books.xml is inside the WEB-INF folder.

Here's my code:

<%
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db =dbf.newDocumentBuilder();

Document doc=db.parse("dynamic file path here");

NodeList nl = doc.getElementsByTagName("book");
%>

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out ServletContext and the getRealPath method. GetContextPath? Something like that.
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Check out ServletContext and the getRealPath method. GetContextPath? Something like that.


Also don't use it to WRITE files, only read them. Additionally, be aware that this method may return NULL if the appserver didn't explode the WAR or support deployment of exploded WARs - neither of which is actually part of the J2EE standard.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the file is always going to be inside your app's directory structure (as you've indicated it will be) then you should look at ServletContext.getResourceAsStream.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String) instead of reading it with a file reader.

If you access it this way, you won't have to worry about the issues with getRealPath mentioned by Tim.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+1
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:+1

+2!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+3
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shhh!

Do I hear five? Five, five, five for this one-of-a-kind topic.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You.


Whoo Hoo!

Look at me, everybody!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!