This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes getRealPath() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getRealPath()" Watch "getRealPath()" New topic
Author

getRealPath()

Bob Moranski
Ranch Hand

Joined: Nov 22, 2000
Posts: 177
Hi, I have a question about ServletContext's getRealPath(String) method. What is its parameter? And what does this method return?
Thanks.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
The method returns the path to a file, given the end part of it. This is probably best illustrated with an example:
Imagine a web server (or web application) serving files from c:\files\fred which has a servlet published as "showme" designed to show a Java source file with syntax coloring.
A request comes in to the server for the URL /servlets/showme?file=projects/source/Example.java
Rather than hard code the "c:\files\fred" into the servlet (which would be very un-portable) the servlet can call
String path = context.getRealPath(req.getParameter("file"));
which should return:
c:\files\fred\projects\source\Example.java
ready for opening with a FileReader, or whatever.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Bob Moranski
Ranch Hand

Joined: Nov 22, 2000
Posts: 177
Thank you very much Frank!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getRealPath()
 
Similar Threads
how to handle file uploads in spring
How to read a resource file from war file ??
Can I retrieve the server root from a bean?
create a file in the root directory
absolute path