You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Dunkle, Ed" <Ed...@ameriserve.com> on 2000/05/03 01:44:37 UTC

getting the absolute path of the requested resource in a servlet

This seems to be an anomalie:
getServletConfig().getServletContext().getRealPath(req.getRequestURI())

returns something like:
abspath/mycontext/mycontext/filename

I would think the desired behavior would be:
abspath/mycontext/filename

Comments??


Thanks,
Ed

Re: getting the absolute path of the requested resource in a servlet

Posted by Craig McClanahan <Cr...@eng.sun.com>.
"Dunkle, Ed" wrote:

> This seems to be an anomalie:
> getServletConfig().getServletContext().getRealPath(req.getRequestURI())
>
> returns something like:
> abspath/mycontext/mycontext/filename
>
> I would think the desired behavior would be:
> abspath/mycontext/filename
>
> Comments??
>

The argument to getRealPath() is supposed to be context-relative, but you are
passing it a server-relative URI (the getRequestURI() value includes the
context path prefix).

The way to get what you want is to construct a context-relative URI out of
the appropriate request path elements:

    String uri = request.getServletPath();
    if (request.getPathInfo() != null)
        uri += request.getPathInfo();
    String pathname = getServletContext().getRealPath(uri);

and you will get back the desired behavior you asked for.

>
> Thanks,
> Ed
>

Craig McClanahan