This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes getResource & getRealPath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "getResource & getRealPath" Watch "getResource & getRealPath" New topic
Author

getResource & getRealPath

Andy Smith
Ranch Hand

Joined: Sep 28, 2003
Posts: 239
Whats the difference between the "getRealPath" and "getResource"???


Share Knowledge to gain it.
SCJP 2, SCWCD 2, SCDJWS, IBM 141 (In Progress), IBM 486 (Next)
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
java.net.URL getResource(String path) method returns a java.net.URL object for the resource that is mapped to the given path. Although the path should start with / it is not an absolute path. It is relative to the document root of this web application. For instance, if you pass a path to a JSP file, it will give you the unprocessed data, i.e., in this case, the JSP source code, when you read the contents.
java.lang.String getRealPath(String path) method returns a string that shows the whole real path for the given virtual path... here is the problem associated with getRealPath method, that I extracted from SCWCD Study Kit...
the problem with this approach is that it is not helpful
when the resource is inside a JAR file. It is also useless when the servlet is running in a
distributed environment where the resource may reside on a different machine. In
such situations, the getResource() method comes handy.


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Andy Smith
Ranch Hand

Joined: Sep 28, 2003
Posts: 239
Thanx a lot ...
i hvae read somewhere that getReaource can not be used for the active jsp's & servlets....
What does it mean??
THANX
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
It means that if you pass a path to a JSP file, it will give you the JSP source code, when you read the contents.... It will not give you as the JSP-generated html page... i.e., you will the response like <html><head></head><body>Hello World!</body></html> as unprocessed data...
You will not get just only Hello World! as you expect....
Hope it is clear...
Andy Smith
Ranch Hand

Joined: Sep 28, 2003
Posts: 239
Hey Naing ....
Thanx a lot... for clearing the confusion....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getResource & getRealPath