You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Stephen Suen <st...@gmail.com> on 2006/05/18 10:56:20 UTC

Catching RequestDispatcher not found errors

Hi,

I'm using RequestDispatcher to dynamically include content. When the target
resource does not exist, a 404 error page returns from Tomcat. Acording to
the API, the included servlet cannot change the response status code or set
headers, and any attempt to make a change is ignored.

I did a hard work trying to find where in the source code and how Tomcat
set such a status code to the response, but failed.

Can any body tell me where I can find such source code, and give me some
basic explanation on it.

Thanks in advance

-- 
Stephen Suen
stephen.suen@gmail.com

Re: Catching RequestDispatcher not found errors

Posted by Stephen Suen <st...@gmail.com>.
On 5/19/06, Mark Thomas <ma...@apache.org> wrote:
>
>
> The RequestDispatcher is created in this class:
>
> http://svn.apache.org/repos/asf/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/core/ApplicationContext.java
>
> ApplicationDispatcher implements RequestDispatcher
>
> http://svn.apache.org/repos/asf/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/core/ApplicationDispatcher.java
>
> The wrapper that checks if it is included before calling sendError is
>
> http://svn.apache.org/repos/asf/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/core/ApplicationHttpResponse.java
>
> HTH,
>
> Mark



Thanks for your help.

Acording to the API, sendError from the *included servlet* will be ignored.
I think maybe this doesn't necessarily mean *Tomcat itself* can't do it. If
i'm trying to include a resource not existed after the response being
committed, the error message will be appended to the response, and sendError
be ignored.

Here's the snippet from
http://svn.apache.org/repos/asf/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java
:

    if (requestUri == null) {
        requestUri = request.getRequestURI();
    } else {
        // We're included, and the response.sendError() below is going
        // to be ignored by the resource that is including us.
        // Therefore, the only way we can let the including resource
        // know is by including warning message in response
        response.getWriter().write(
            sm.getString("defaultServlet.missingResource ",
            requestUri));
    }

    response.sendError(HttpServletResponse.SC_NOT_FOUND,
                       requestUri);

If the response has already been committed, doing that will get a 404 error
page returned to the client, that means Tomcat set this status somewhere
else, or from some wrapper, but I failed to find it.

Any idea?

Best Regards

Stephen

Re: Catching RequestDispatcher not found errors

Posted by Stephen Suen <st...@gmail.com>.
Hi, Mark

Thanks for your patient help. I have figured out the problem. It's
not relevant to Tomcat.

I tested my web application from NetBeans IDE with HTTP monitor enabled,
NetBeans put a wrapper on the Tomcat's response object. I think that wrapper
caused the problem.

Thanks

Best Regards

Stephen

Re: Catching RequestDispatcher not found errors

Posted by Mark Thomas <ma...@apache.org>.
Stephen Suen wrote:

> The call to sendError should be ignored, then why a 404 error page returns?
> I think maybe Tomcat overrides this behaviour or does some trick elsewhere.
> Can you give me more help?

The RequestDispatcher is created in this class:
http://svn.apache.org/repos/asf/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/core/ApplicationContext.java

ApplicationDispatcher implements RequestDispatcher
http://svn.apache.org/repos/asf/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/core/ApplicationDispatcher.java

The wrapper that checks if it is included before calling sendError is
http://svn.apache.org/repos/asf/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/core/ApplicationHttpResponse.java

HTH,

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Catching RequestDispatcher not found errors

Posted by Stephen Suen <st...@gmail.com>.
On 5/19/06, Mark Thomas <ma...@apache.org> wrote:Stephen Suen wrote:
> Can any body tell me where I can find such source code, and give me some
> basic explanation on it.

http://svn.apache.org/repos
>
>
> /asf/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java
>
> Search for SC_NOT_FOUND
>
> Mark



Thanks for the help.

I have found this, but as it says in the following source code:

    if (requestUri == null) {
        requestUri = request.getRequestURI();
    } else {
        // We're included, and the response.sendError() below is going
        // to be ignored by the resource that is including us.
        // Therefore, the only way we can let the including resource
        // know is by including warning message in response
        response.getWriter().write(
            sm.getString("defaultServlet.missingResource",
            requestUri));
    }

    response.sendError(HttpServletResponse.SC_NOT_FOUND,
                       requestUri);

The call to sendError should be ignored, then why a 404 error page returns?
I think maybe Tomcat overrides this behaviour or does some trick elsewhere.
Can you give me more help?

Best Regards

Stephen Suen

Re: Catching RequestDispatcher not found errors

Posted by Mark Thomas <ma...@apache.org>.
Stephen Suen wrote:
> Can any body tell me where I can find such source code, and give me some
> basic explanation on it.

http://svn.apache.org/repos/asf/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/servlets/DefaultServlet.java

Search for SC_NOT_FOUND

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org