View RSS Feed

My Java Tips

Reading files from JSP

Rate this Entry
by , 05-24-2011 at 01:44 PM (2575 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");

    ...
    %>

Submit "Reading files from JSP" to Facebook Submit "Reading files from JSP" to Digg Submit "Reading files from JSP" to del.icio.us Submit "Reading files from JSP" to StumbleUpon Submit "Reading files from JSP" to Google

Categories
JSP

Comments