Reading files from JSP
by, 05-24-2011 at 01:44 PM (2694 Views)
Working in Servlets/JSP, you might have encountered The javax.servlet.ServletContext API. It provides methods to access resources in general.
- getResource(String resource): will returns a URL for the resource in the web application.
- getResourceAsStream(String resource): will returns an InpuStream for the resource in the web application
- getResourcePaths(String path): will a directory-like listing of all the paths to resources within the web application.
An example snippet is presented below:
Java Code:<% ... // Get the file as an input stream // The path is relative to the application context, and data.txt is placed // under the WEB-INF folder of the application InputStream is = config.getServletContext().getResourceAsStream("/data.txt"); ... %>