tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Warren <elki...@yahoo.com>
Subject Re: virtual host getRealPath
Date Sun, 05 Nov 2006 02:36:49 GMT
Hi Chris,

Thanks for your response.  After poking around a little more, it seems that the call


application.getRealPath(request.getServletPath())    
provides the proper local file path for me.  Any reason not to use that?

Peter


Christopher Schultz <chris@christopherschultz.net> wrote:  Peter,

> I'm trying to figure out why I get repeated directory names when calling
> "application.getRealPath(request.getRequestURI())" from an index.jsp
> file. Clearly there's something about virtual hosts and contexts that
> I'm missing.

I suppose you /could/ do this, but usually URIs and directory structures
usually don't map exactly to one another.

> A request for the url: http://www.nomad.org/gallery/
> C:\Documents and Settings\Peter\My Documents\dev\webapps\www.nomad.org\gallery\gallery

Yeah. Since your URL contains the prefix "/gallery" already, it's being
added. When you call "getRealPath", you're getting something rooted in
"...\dev\webapps\www.nomad.org\gallery", which is the root of your
webapp. Since "/gallery" in the URI, too, you're doubling it.

You need to clip out the context path.

This ought to do it for you:

String path = request.getRequestURI();
if(path.startsWith(request.getContextPath()))
path = path.substring(request.getContextPath());
path = application.getRealPath(path);

Note that letting users' URIs drive files being loaded from the disk
might be considered a security risk. I don't know your deployment model
or anything like that; I just figured I'd mention it.

Hope that helps,
-chris



 
---------------------------------
Cheap Talk? Check out Yahoo! Messenger's low  PC-to-Phone call rates.
Mime
  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message