FAQ

Java

JSP

Servlet


Advertisement



How to use ServletContext.getResourceAsStream(java.lang.String path)?

From API document, this method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader. The method returns the resource located at the named path as an InputStream object.

    ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("/yourfilename.cnf");

The ServletContext.getResourceAsStream() method the file can be located anywhere in your web application. It is recommended to keep it under the /WEB-INF directory if you don't want browers being able to access it. Your web application should use the ServletContext.getResourceAsStream() API when accessing web application resources.

The path must begin with a "/" and is interpreted as relative to the current context root. This method returns null if no resource exists at the specified path. For example, using a path that doesn't start with a slash, You will get a null return value from:

ServletContext.getResourceAsStream("WEB-INF/resources/yourfilename.cnf"). 


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |