You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Jonathan Pierce <Jo...@seagram.com> on 2000/04/28 16:13:16 UTC

ServletContext.getRealPath still broken?

ServletContext.getRealPath seems to still return the wrong result in Tomcat3.1.
Is this a known bug?

I added the following two lines to the SnoopServlet example in the Tomcat3.1
distribution to illustrate this:

out.println("request.getContextPath (): " + request.getContextPath ());
    out.println("getServletContext().getRealPath (request.getContextPath ()): "
+ getServletContext().getRealPath (request.getContextPath ()));
    
The results were as below which looks wrong !!!.

request.getContextPath ():
    /examples 

getServletContext().getRealPath (request.getContextPath ()):
D:\Tomcat3.1\dist\tomcat\webapps\examples\examples 

The server.xml entry is:
<Context path="/examples" docBase="webapps/examples" debug="0" reloadable="true"
> 
</Context>

My Tomcat_Home directory is:
D:|Tomcat3.1\dist\tomcat

Jonathan Pierce (Not to be confused with the other Jonathan posting to this
list.)

Re: ServletContext.getRealPath still broken?

Posted by "Craig R. McClanahan" <Cr...@Eng.Sun.COM>.
Jonathan Pierce wrote:

> ServletContext.getRealPath seems to still return the wrong result in Tomcat3.1.
> Is this a known bug?
>

>
> I added the following two lines to the SnoopServlet example in the Tomcat3.1
> distribution to illustrate this:
>
> out.println("request.getContextPath (): " + request.getContextPath ());
>     out.println("getServletContext().getRealPath (request.getContextPath ()): "
> + getServletContext().getRealPath (request.getContextPath ()));
>
> The results were as below which looks wrong !!!.
>
> request.getContextPath ():
>     /examples
>

The context path itself has nothing to do with the location of files on disk -- it
is the start of a request URI that identifies this request as belonging to a
particular application.  You can change the context path by changing the "path"
attribute in your <Context> setting in conf/server.xml, and nothing else should need
to change inside a servlet/JSP application.


>
> getServletContext().getRealPath (request.getContextPath ()):
> D:\Tomcat3.1\dist\tomcat\webapps\examples\examples
>
> The server.xml entry is:
> <Context path="/examples" docBase="webapps/examples" debug="0" reloadable="true"
> >
> </Context>
>
> My Tomcat_Home directory is:
> D:|Tomcat3.1\dist\tomcat
>

If you wanted the pathname to the disk file that corresponds to request URI
"/examples/index.html", you would strip off the context path part from the front,
and call getRealPath("/index.html").  In your scenario, this would return
"D:\Tomcat3.1\dist\tomcat\webapps\examples\index.html".


>
> Jonathan Pierce (Not to be confused with the other Jonathan posting to this
> list.)
>

Craig McClanahan



Re: ServletContext.getRealPath still broken?

Posted by Costin Manolache <co...@costin.dnt.ro>.
Hi Jonathan,

I think getRealPath() return the right result from the point of view
of the spec. ( it may be wrong from other views :-)

It will return the real path _relative_ to the webapp ( or context ),
as opposed to the real path for an absolute URL.

If you want the real path of the context you'll have to call getRealPath("")
( in general - remove the contextPath from the URI ).

The reason seems to be mainly security - to keep different web applications
completely isolated, which is an useful feature. You can still find real paths
in different contexts by using getContext( path ).getRealPath( pathMinuxCtxPath).
( getContext(path)  may allow/deny access to a different context based on
security settings )

If you prefer a different behavior or more informations - mail to the
servletapi-feedback !

Costin

Jonathan Pierce wrote:

> ServletContext.getRealPath seems to still return the wrong result in Tomcat3.1.
> Is this a known bug?
>
> I added the following two lines to the SnoopServlet example in the Tomcat3.1
> distribution to illustrate this:
>
> out.println("request.getContextPath (): " + request.getContextPath ());
>     out.println("getServletContext().getRealPath (request.getContextPath ()): "
> + getServletContext().getRealPath (request.getContextPath ()));
>
> The results were as below which looks wrong !!!.
>
> request.getContextPath ():
>     /examples
>
> getServletContext().getRealPath (request.getContextPath ()):
> D:\Tomcat3.1\dist\tomcat\webapps\examples\examples
>
> The server.xml entry is:
> <Context path="/examples" docBase="webapps/examples" debug="0" reloadable="true"
> >
> </Context>
>
> My Tomcat_Home directory is:
> D:|Tomcat3.1\dist\tomcat
>
> Jonathan Pierce (Not to be confused with the other Jonathan posting to this
> list.)
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org