What is different between ClassLoader.getResourceAsStream() and Class.getResourceAsStream()?
A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code. According to API document:
The ClassLoader.getResourceAsStream(name) returns an Inputstream for reading the specified resource or null if the resource could not be found. The name of a resource is a '/'-seperated path name that identifies the resource. The name no leading '/' (all namea are absolute).
The Class.getResourceAsStream(name) returns an Inputstream for a resource with a given name or null if no resource with the given name is found. The name of a resource is a '/'-seperated path name that identifies the resource. If the name with a leading "/" indicates the absolute name of the resource is the portion of the name following the '/'. All other names are relative to the class's package, the absolute name is of the following form:
modified_package_name/name
where the modified_package_name is the package name of this object with '/' substituted for '.'.
In Class.getResourceAsStream(name), the rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String).
For example, watch the leading slash:
ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
Class.getResourceAsStream ("/some/pkg/resource.properties");
Most Recent servlet Faqs
- What is the getInputStream() of ServletRequest for?
- 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()?
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?
- How to get the client information in a Servlet?
- What is the defferent between getNamedDispatcher() and getRequestDispatcher()?