aspose file tools*
The moose likes Servlets and the fly likes How can I retrieve the server path from inside the servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How can I retrieve the server path from inside the servlet?" Watch "How can I retrieve the server path from inside the servlet?" New topic
Author

How can I retrieve the server path from inside the servlet?

Adrian Muscalu
Ranch Hand

Joined: May 08, 2000
Posts: 73
Looks like beginner question, am I right?


SCJP2, SCWCD
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
What do you mean exactly, when you say 'server path'?
Given a URL of(for example)
'http://www.ugh.com:9023/happy/servlets/Fred?a=2&b=3'
which maps to (say)
/usr/loopy/web/myserver/happy/Fred.class
which parts are you looking for?


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

Joined: May 08, 2000
Posts: 73
Say that my server is located at c:\jswdk-1.0 and I am interested to access a file c:\jswdk-1.0\MyFolder\Myfile.xml. Is it possible to access it via ./MyFolder/Myfile.xml? I encoutered an error (needed protocol).
Yours greenhorn,
Adrian Muscalu
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
If you mean access the file using a browser, that all depends on where you have set the "document root" of your server.
If you mean access the file from your server-side Java code, that all depends on where the "current directory" is set when you run your server. Or you could, of course, use a full path.
henley
Greenhorn

Joined: Jul 06, 2000
Posts: 16
Frank seems to be right on. One other note, if you are getting errors about needing protocols, then you may be using the wrong classes to access the file. When accessing local files, all you need to do is give the path information (whether it be absolute or relative). Protocols do not come into play. Good Luck.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
I think what you are looking for is the
getRealPath method in the ServletContext class.
Input to this method is the webserver's alias for a file,
output is the real path which you can open a file to inside the servlet.
WBB
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I retrieve the server path from inside the servlet?