0

I want to access xml files from java class, lying under web/resources folder.. i.e

---Web pages
|
|--Web-INF
|
|-resources
| |-data.xml
|
|-other jsps
---Source Folder
|-databean

I want to access data.xml from a class under databean package. Is it possible? How?

MalTec
  • 1
  • 1
  • 2

1 Answers1

4

Use ServletContext#getResource() or #getResourceAsStream() to obtain resources which are available in the webcontent.

InputStream input = getServletContext().getResourceAsStream("/WEB-INF/resources/data.xml"); 

You however normally do that in a Servlet, not a JSP.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • hello sir, i want it access in a java class. Not even in servlet. Can u help me over it? – MalTec Mar 04 '11 at 14:51
  • Pass the `ServletContext` to it as constructor/method argument so that you can use it, or move the file to the classpath, so that you can use `getClass().getResourceAsStream(path)` instead. – BalusC Mar 04 '11 at 14:52
  • What's the better and practical way? – MalTec Mar 04 '11 at 15:14
  • Depends on who needs the file. If it are the Java classes, put in classpath. If it are the Servlets/JSP pages, put in webcontent. You apparently don't need it in Servlet/JSP at all, in contrary to what the question title suggests :) – BalusC Mar 04 '11 at 15:15