• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

J2SE getResource() andgetResourceAsStream()

 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JEE nothing but java right?
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OH well,I'm sure that only the ServletContext resource methods will appear on the exam
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure Charles Lyons book said that the J2SE version used real paths and could access stuff outside the web-app.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic