tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tommy Pham <tommy...@gmail.com>
Subject Re: TC 9.0.21: ContextListener + Servlet app
Date Fri, 21 Jun 2019 10:04:03 GMT
Hi Mark,

On Thu, Jun 20, 2019 at 11:57 PM Mark Thomas <markt@apache.org> wrote:

> On 21/06/2019 03:21, Tommy Pham wrote:
> > </snip>
> >
> > After some further research, it appears the issue I'm encountering is
> known
> > since 2007 by IBM at least:
> >
> > https://www-01.ibm.com/support/docview.wss?uid=swg21259282
> >
> > While reviewing the JSR-369 history, at jcp.org, it seems that this is
> > since servlet spec 2.3, perhaps even earlier.  Does anyone know if the
> > email spec-submit@jcp.org is the correct email which I can submit a
> > proposal for updating the JSR-369?
>
> It isn't.
>
> The Java EE specs have moved to Eclipse and are in the process of
> re-branding to Jakarta EE. The process now is to raise an issue here:
>
> https://github.com/eclipse-ee4j/servlet-api
>
> However...
>
> >  I think the section 3.6 of the servlet
> > spect should be changed from:
> >
> > "The getPathTranslated method computes the real path of the pathInfo of
> the
> > request."
> >
> > to:
> >
> > The getPathTranslated method computes the real path of the servletPath
> and
> > pathInfo of the request.  If the pathInfo is not valid, then compute the
> real
> > path of the servletPath.
>
> Since I'm one of the folks who would respond to any such request, I can
> give you a heads up on the likely response.
>
> That is never going to happen. There are multiple reasons:
>
> 1. It would break backwards compatibility.
>
> 2. The purpose of getPathTranslated() is to map *just* the pathInfo to
> the file system. There are use cases that need this functionality.
>
> 3. You can use ServletContext.getRealPath(String path)
>
> > Since the requestURI is comprise of /contextPath/servletPath/pathInfo.  I
> > think the above would be more accurate to translate the requested path
> to a
> > static file on the file system as I've seen by various cases, and
> possibly
> > IBM's too.
>
> What is more likely to be accepted (but no guarantees) is a new method
> on ServletRequest getRealPath() (note the lack of parameter) that calls
> ServletContext#getRealPath() with servletPath + pathInfo (with
> appropriate null handling).
>
> Mark
>
>
Thank you for the clarification and the heads up. I greatly appreciate it.
I've been thinking about different methods of approach and I think this is
better than servletPath + pathInfo:

        return getServletContext() == null ? null : getServletContext().
getRealPath(getRequestURI().substring(getContextPath().length()));

due to less conditional checks yielding better execution time and since I
can't see any condition that either requestURI or contextPath could be null:

            hsr = (HttpServletRequest) request;

            String pt = null;
            final long startTime1 = System.currentTimeMillis();
            pt = getPathTranslatedR1();
            final long endTime1 = System.currentTimeMillis();
            logger.debug(String.format("Correction of PathTranslated R1: %s
ms %s", (endTime1 - startTime1), pt));
            pt = null;
            final long startTime2 = System.currentTimeMillis();
            pt = getPathTranslatedR2();
            final long endTime2 = System.currentTimeMillis();
            logger.debug(String.format("Correction of PathTranslated R2: %s
ms %s", (endTime2 - startTime2), pt));

     private String getPathTranslatedR1() {
        if (hsr.getServletContext() != null) {
            if (hsr.getServletPath() != null) {
                if (hsr.getPathInfo() != null) {

                    return
hsr.getServletContext().getRealPath(hsr.getServletPath()
+ hsr.getPathInfo());
                }
                return hsr.getServletContext().
getRealPath(hsr.getServletPath());
            }
            return null;
        }
        return null;
    }

    private String getPathTranslatedR2() {

        return hsr.getServletContext() == null ? null : hsr.
getServletContext().
getRealPath(hsr.getRequestURI().substring(hsr.getContextPath().length()));
    }

Correction of PathTranslated R1: 1 ms
D:\apache-tomcat-9.0.21\webapps\erm-0.0.1-SNAPSHOT\css\jquery-ui\base\index.html
Correction of PathTranslated R2: 0 ms
D:\apache-tomcat-9.0.21\webapps\erm-0.0.1-SNAPSHOT\css\jquery-ui\base\index.html


As for the new method of getRealPath(), how does the web container knows
which is the correct one?  Does it then check if the file actually exists
for both getRealPath() and getPathTranslated() and compare?  Going forward
to have progress of my project, I'll have write to my own servlet to handle
static content instead of forwarding to TC's default servlet.  On the
bright side, I think I can have stricter enforcement ie:  "/css/*.css"

Thanks,
Tommy

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message