| Author |
J2SE getResource() andgetResourceAsStream()
|
Duran Harris
Ranch Hand
Joined: Nov 09, 2008
Posts: 571
|
|
|
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: 571
|
|
|
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: 571
|
|
|
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: 571
|
|
|
I'm sure Charles Lyons book said that the J2SE version used real paths and could access stuff outside the web-app.....
|
 |
 |
|
|
subject: J2SE getResource() andgetResourceAsStream()
|
|
|