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").
Most Recent servlet Faqs
- How to get the client information in a Servlet?
- Can I control Session timeout?
- What is the difference between RequestDispatcher's forward method and HttpServletResponse's sendRedirect method?
- When do I use HttpSessionListener?
- What is the difference between the request attribute and request parameter?
- What is the defferent between getNamedDispatcher() and getRequestDispatcher()?
- How do you communicate in between applets and servlets?
Most Viewed servlet Faqs
- What is the difference between the request attribute and request parameter?
- When do I use HttpSessionListener?
- How to use ServletContext.getResourceAsStream(java.lang.String path)?
- What is the difference between RequestDispatcher's forward method and HttpServletResponse's sendRedirect method?
- Can I control Session timeout?
- What is the defferent between getNamedDispatcher() and getRequestDispatcher()?
- How to get the client information in a Servlet?