The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes getResource() n getResourceAsStream() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "getResource() n getResourceAsStream()" Watch "getResource() n getResourceAsStream()" New topic
Author

getResource() n getResourceAsStream()

mysha ahamed
Greenhorn

Joined: Aug 03, 2005
Posts: 22
Hi,

Iam not able to understand the real meaning of getResource() n getResourceAsStream()methods in ServletContext.

In the api its given ServletContext.getResource()):

The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a RequestDispatcher instead to include results of an execution.
This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.


In HFSJ its given tht these methods may not be used to access resources in jar files instead v shuld use classloader's getResource() n getResourceAsStream()methods .

But in the api spec its given :Resources can be located in a .war file.


Can anyone pls expalin me the main purpose of these methods n distiction b/w classloaders methods..
Thnx..
Severin Stoeckli
Ranch Hand

Joined: Jul 21, 2004
Posts: 62
the ClassLoader's getResourceAsStream searchs your resource in the classpath, according to the rules of the classLoader.

the Servlet's getResourceAsStream expects a URL to the resource and can access a .war file, but not something in a .jar -file inside the .war file.

/Severin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getResource() n getResourceAsStream()
 
Similar Threads
Get a URL from a Webpage
reading a file located on the server
Accessing different WAR resources
getContextPath() in HttpServletRequest
getResource in ServletContext