SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict NikoB's Avatar
    Join Date
    Nov 2005
    Location
    Austria
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Configuration file with servlet/jsp

    I want some configuration options loaded from a servlet/jsp. Right now I found two ways of doing that:
    Either I load the file from the executable path ( new File ("config.properties") ) but then I would need config.properties to reside in a system directory (like int /etc or windows/system32) definitely not where I want that.
    Or I use the classloader and put the file in WEB-INF/classes/config.properties
    But this is also not really a good option, since I need to restart tomcat/reload the context every time something is changed, plus I cant put my app in a war file.

    Is there any way I find out the absolute path of the webapplication?
    f.e. I have a jsp in /home/app/webroot/findpath.jsp when I call it with localhost:8080/findpath.jsp it gives me this path.

    Niko
    Corinis OpenSource Community & Content Management
    http://www.corinis.org

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Check out ServletContext.getRealPath( String )

    ie: request.getSession().getServletContext().getRealPath( "/" );
    will return the real path to the root of your context.

    (of course, from a JSP you have access to the 'application' Object (ServletContext), so application.getRealPath( string ); is all that is needed)

  3. #3
    SitePoint Addict NikoB's Avatar
    Join Date
    Nov 2005
    Location
    Austria
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot.. I knew there had to be a simple way
    <%=application.getRealPath(request.getRequestURI())%>
    Corinis OpenSource Community & Content Management
    http://www.corinis.org

  4. #4
    SitePoint Addict NikoB's Avatar
    Join Date
    Nov 2005
    Location
    Austria
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok tha lst snippet does not ALWAYS work. especially not when we are in another than the root context. To get it to work I use:
    <%=application.getRealPath(request.getRequestURI().substring(request.getContextPath().length()))%>
    Corinis OpenSource Community & Content Management
    http://www.corinis.org


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •