Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get servlet's full path name

 
Nicolas Tolksdorf
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the most effective way of getting a servlet's full/absolute path?
There is a getRealPath() method in the javax.servlet.ServletConfig interface but i'm not clear about how to implement this interface. Especially since an interface is abstract by definition and can't be instantiated.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't implement the servlet config interface.
An object that already implements that interface is available to you within any servlet via getServletConfig().
That said, what path are you really trying to discover? getRealPath() may not be the correct mechanism.
hth,
bear
[ June 30, 2003: Message edited by: Bear Bibeault ]
 
Scott Duncan
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if this is what you want but:
this.getClass().getName() returns the fully qualified class name. If you want the name of a different servlet than the one currently being ran you can do this:
MyServlet.class.getName()
as long as the appropriate import statement is made to include MyServlet.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the HttpServletRequest object, there are two useful methods for this:
getServletPath() gives you the name of the servlet
getRequestURI() gives you the full path that was called
 
Nicolas Tolksdorf
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.
As i'm loading a properties file ( db name, db address, etc ) in an init() of my parent class. Getting the servlet to automatically discover the full path name to this file is useful as the servlet context changes between my live setup and development setup.
So i've used the getServletContext() method to get a ServletContext object and i was able to then get the full windows path of a folder within the servlet context:
ServletContext servletContext = getServletContext();
String strAbsolutePath = servletContext.getRealPath("properties");

Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic