Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getRealPath()

 
Bob Moranski
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bob Moranski
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Frank!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic