Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • 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: 6920
  • 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: 6920
  • 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
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic