This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like beginner question, am I right?
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • 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
  • 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: 6920
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Liar, liar, pants on fire! refreshing plug:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic