tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Kjome <h...@visi.com>
Subject Re: SOLVED: How to get the context path for a web application?
Date Thu, 12 Aug 2004 13:45:01 GMT
To get the context path at init time, try this....

from:
http://cvs.apache.org/viewcvs.cgi/logging-log4j-sandbox/src/java/org/apache/log4j/servlet/InitShutdownController.java

/**
    * Retrieves the context path of the web application from the servlet 
context.
    *
    * @param context the current servlet context
    * @return the derived context path, guaranteed non-null
    */
   private static String getContextPath(final ServletContext context) {
     //old way to determine context path
     //String tempdir = "" +
     //context.getAttribute("javax.servlet.context.tempdir");
     //int lastSlash = tempdir.lastIndexOf(File.separator);
     //if ((tempdir.length() - 1) > lastSlash) {
     //  logHomePropName = tempdir.substring(lastSlash + 1) + ".log.home";
     //}
     String contextPath = "";

     try {
       //use a more standard way to obtain the context path name
       //which should work across all servers. The tmpdir technique
       //(above) depends upon the naming scheme that Tomcat uses.
       String path = context.getResource("/").getPath();

       //first remove trailing slash, then take what's left over
       //which should be the context path less the preceeding
       //slash such as "MyContext"
       contextPath = path.substring(0, path.lastIndexOf("/"));
       contextPath = contextPath.substring(contextPath.lastIndexOf("/") + 1);
     } catch (Exception e) {
       ;
     }

     return contextPath;
   }


Jake
At 10:28 AM 8/12/2004 +0200, you wrote:
>On Wed, 11 Aug 2004, David Wall wrote:
>
>| > The cause for some of these specs is the fact servlet containers aren't
>| > required to run on file systems.  For example, they may run entirely
>| > inside a DBMS (and Oracle had such a container for a while), in which
>| > case you must deploy in a packed WAR and the subset under a server's
>| > URL name space is not a hard-drive path.
>
>I do know this.
>
>|
>| Nobody "cares" if this maps to a file system or not.
>
>Totally agree - it has nothing at all with filesystem, or whether the
>webapp is stored on a read-only stone-tablet - I want to know what "subset
>of the webserver's URL namespace" I'm mounted beneath, at init time. What
>I'll do with that information is my business!
>   As this is a static element; the webapp is, -per definition- (The
>javadoc snippet) mounted beneath a "subset of the URL namespace"; there is
>no reason I can fathom why this shouldn't be made available to "us coders"
>that are making webapps, at init time. The point is that the webapp won't
>change "mount point" from request to request, will it? So the web
>container must know at init-time which sub-space it will mount the webapp
>under.. Just please make that information available then, at init time!
>
>IMO, that is.
>
>  ( -My- use-case is, as mentioned, that I want to make it easy for
>developers to override some (development-)settings of the framework. In
>the webapp I'll check whether it is possible to do file-system access and
>System-property-getting, and if so, then check for and read the files
>$userdir/devel/develconfig.xml, and then
>$userdir/devel/<webappname>/develconfig.xml.  It if it is not possible,
>then I won't do that, and assume that I am in a production environment or
>something. )
>
>I'd just love to see this extra method added.
>
>Endre.
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message