On Thu, 29 Aug 2002, Mark R. Diggory wrote:

> Date: Thu, 29 Aug 2002 15:13:21 -0400
> From: Mark R. Diggory <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>
> To: Tomcat Users List <[EMAIL PROTECTED]>
> Subject: Re: Is this a Bug in request.getServletPath() ?
>
> Craig R. McClanahan wrote:
>
> >You seem to be making an unwarranted assumption, that there *is* such a
> >thing as a directory for a context.
> >
>
> Not neccessarily unwarranted, It is apparent that there is a JNDI
> Context available for resources in Tomcat

True, but that's an internal implementation detail, and is not true for
all versions of Tomcat -- only for 4.0 and later.

> and that the Servlet API
> specifies that the Implementor is required to provide URLConnections
> (Jar/Http what have you)  to the said resources, so that a file could
> reside in a War package or Database or what have you.
>
> Saying that "/" is required in the Servlet Spec. for getResource("/") is
> suggesting that there *is* a concept of a "path" even if the path is a
> "name" resolved in a JNDI Context/SubContext . I'm simply suggesting
> that it would be more powerfull to adopt some simple means to resolve
> resources relative to "Object" making the request as well as absolutely.
>

In other words, you want relative paths as arguments to
ServletContext.getResource()?  If so, how do you propose to determine what
object the reference is supposed to be relative *to*?  About the only
thing that might make sense is the context-relative part of the request
URI, but it's a pretty simple matter of String concatenation to come up
with the corresponding context-relative path starting with "/".

> >Such a thing is not required by the
> >servlet spec -- it's perfectly reasonable to run webapps directly from a
> >WAR file, or to represent the static contents in a database, or lots of
> >other interesting possibilities -- and in none of those cases is there a
> >directory to give you the path for.
> >
>
> For example: In the Java Object model.
>  object.getClass().getResource("....") provides a Mechanism to resolve
> both resources relative to the Class/Package of the object and
> absolutely from the classpath root. Now, are Java packages neccessarily
> directory structures? No, they can reside in a Jar File or in the case
> of "Corba/RMI/Object servers what have you", just about any where. But
> when it comes down to it object.getClass().getResource("/foobar.txt")
> means absolutely and object.getClass().getResource("foobar.txt") means
> relatively. Do they not convey this nature even though they may not
> actually be file paths?
>
> As such I still like the idea of the following options at least!
>
> application.getResource("/") resolve to the subcontext of the
> Servlet Context Root
>
> application.getResource("")  resolve to the subcontext of the
> Servlet/JSP
>

More precisely, to the context-raltive part of the request URI, right?
There won't necessarily be a static resource that corresponds to every
valid servlet mapping.

> This would seem logical and much more powerfull, would it not?

Perhaps, although the servlet API has lived with context-relative paths
for resources since 2.0 days.  At any rate, it's not something that Tomcat
will implement unless the servlet spec is changed -- and to make a
suggestion for that, the appropriate email address is
<[EMAIL PROTECTED]>.

Given that Servlet 2.4 is in Proposed Final Draft at the moment, it is
highly unlikely that the expert group would accept a change like this for
2.4 even if they liked the idea, so it would probably go on the plate for
consideration in a subsequent version.

Craig

>
> >You should also note that allowing ".." to resolve "above" the webapp
> >would open up huge security holes for malicious webapps to negatively
> >affect the operation of other webapps on the same server.
> >
> application.getResource("..") resolve to the parent subcontext/directory
> of the Servlet/JSP
>
> Ok, I'll give up on this one as it *is* seemingly file path like in
> nature...I don't have a valid JNDI context like example of such an
> operation.
>
> >
> >The ServletContext.getResource() family of methods work the way they do in
> >order to ensure portable access to resources within the web application,
> >no matter how the servlet container chooses to implement the APl's.
> >
>
> >>-Mark Diggory
> >>
> >>
> >
> >Craig
> >
> >
> >
> >
> >>
> >> Shapira, Yoav wrote:
> >>
> >>
> >>
> >>>Hi,
> >>>I believe that if you access it via a mapping, that's how you will see
> >>>its location.  So if you go to the real location in the browser, i.e.
> >>>/Jaxp/JSPTransformExample.jsp, you will get that location instead of the
> >>>mapped one.
> >>>
> >>>In general, you are not guaranteed to have access to the server's
> >>>mappings, much less the ability to introspect them as you would like in
> >>>this use case.
> >>>
> >>>A better approach may be to use getResource() and/or
> >>>getResourceAsStream(), as that would give you the resource and would
> >>>work in a WAR, which the getRealPath() approach won't.
> >>>
> >>>Yoav Shapira
> >>>Millennium ChemInformatics
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>>-----Original Message-----
> >>>>From: Mark R. Diggory [mailto:[EMAIL PROTECTED]]
> >>>>Sent: Thursday, August 29, 2002 9:53 AM
> >>>>To: Tomcat Users List
> >>>>Subject: Is this a Bug in request.getServletPath() ?
> >>>>
> >>>>I'm trying to get a file in the filesystem relative to the location of
> >>>>
> >>>>
> >>>>
> >>>>
> >>>a
> >>>
> >>>
> >>>
> >>>
> >>>>JSP Page. At first Blush the following would seem appropriate:
> >>>>
> >>>><%
> >>>>String jsp_location =
> >>>>
> >>>>
> >>>>
> >>>>
> >>>application.getRealPath(request.getServletPath());
> >>>
> >>>
> >>>
> >>>
> >>>>String relative =
> >>>>
> >>>>
> >>>>
> >>>>
> >>>jsp_location.substring(0,jsp_location.lastIndexOf("/"));
> >>>
> >>>
> >>>
> >>>
> >>>>... get the resource using this path ...
> >>>>
> >>>>%>
> >>>>
> >>>>however, when one uses a servlet mapping on a JSP Page like below.
> >>>>
> >>>><servlet>
> >>>> <servlet-name>JSPTestMap</servlet-name>
> >>>> <jsp-file>/Jaxp/JSPTransformExample.jsp</jsp-file>
> >>>></servlet>
> >>>><servlet-mapping>
> >>>> <servlet-name>JSPTestMap</servlet-name>
> >>>> <url-pattern>/Jaxp/MapExample.jsp</url-pattern>
> >>>></servlet-mapping>
> >>>>
> >>>>request.getServletPath() returns
> >>>>
> >>>><url-pattern>/Jaxp/MapExample.jsp</url-pattern>
> >>>>
> >>>>and not
> >>>>
> >>>><jsp-file>/Jaxp/JSPTransformExample.jsp</jsp-file>
> >>>>
> >>>>is there any way I can get hold of the original real location of the
> >>>>
> >>>>
> >>>>
> >>>>
> >>>JSP
> >>>
> >>>
> >>>
> >>>
> >>>>Page I'm working in and have it also work with servlet-mappings?
> >>>>
> >>>>-Mark Diggory
> >>>>
>
>
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>
>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to