aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes J2SE getResource() andgetResourceAsStream() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "J2SE getResource() andgetResourceAsStream()" Watch "J2SE getResource() andgetResourceAsStream()" New topic
Author

J2SE getResource() andgetResourceAsStream()

Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

I was reading a recent post about how to access a text file in the root of a web-app when I remembered something I read in Charles Lyons book.He mentioned that getResource and getResourceAsStream have J2SE counterparts in the io package.If so,why would one use the J2EE methods over the J2SE methods and vice versa??


===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

JEE nothing but java right?
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
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.


ServletContext.getResourceAsStream()

Since the class loader object is JVM specific, the method provided by j2se will fail if the application is distributed.
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Ok...I read the bit about classloaders.So I just need to know that the J2SE version wont work on distributed apps.
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
Scrap that distributed application idea. I looked into your question and I guess the only difference is that java.lang.Class.getResourceAsStream can only access resources in the directory or jar file from which the class was loaded from whereas the other method can get resources anywhere in the container. See this
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

OH well,I'm sure that only the ServletContext resource methods will appear on the exam
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

I'm sure Charles Lyons book said that the J2SE version used real paths and could access stuff outside the web-app.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: J2SE getResource() andgetResourceAsStream()
 
Similar Threads
Deployment Descriptior - WEB-INF
IDE: eclipse; Problem: embedded Images
Class.getResource(String) and Class.getResourceAsStream(String)
Can't load XSLT file during init
reading config file from JAR file