• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like beginner question, am I right?
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Adrian Muscalu
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic