You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Brad Whitaker <db...@yahoo.com> on 2009/02/17 02:55:09 UTC

HttpServletResponse.sendRedirect() not working?

I'm having a problem with Tomcat 6.0.18 that I don't understand. I'm invoking
HttpServletResponse.sendRedirect() but no redirect is being sent to the
browser. Is there a valid reason why this might occur, or is this likley a
bug in Tomcat?

My code has thrown an exception prior to invoking the redirect but I can't
find anything in the Servlet spec that would indicate this is a problem. The
HttpServletResponse.sendRedirect() is within a try/catch block and no
exception is being thrown in response to sendRedirect(). My app is built on
Grails (which in turn is built on top of Spring) and I don't fully
understand the exception resolution process used by my frameworks, but I
have verified that sendRedirect() is being executed and not throwing an
exception. This same code is working as desired when executed on Jetty (i.e.
the desired redirect is received by the browser) but when executed in Tomcat
I see the default Tomcat error page displayed rather than the redirect.

Any advice or suggestions would be appreciated.
-- 
View this message in context: http://www.nabble.com/HttpServletResponse.sendRedirect%28%29-not-working--tp22049004p22049004.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: HttpServletResponse.sendRedirect() not working?

Posted by Qiao Jin <br...@gmail.com>.
I don't know much about these frameworks either. But from a logical point of
view, if you are seeing the error page by TC, that would indicate to me that
TC has been triggered to handle the exception. Was the exception rethrown or
somehow propagated upward after Spring's exception processing? The error
page by TC might give you a clue as well.

Re: HttpServletResponse.sendRedirect() not working?

Posted by Brad Whitaker <db...@yahoo.com>.
Thanks for the feedback and suggestions. I've decided to take a different
approach with my error handling. I think the error handling strategy I was
trying to implement was a bad idea and I have abandoned it. (In particular,
I think I was trying to do a redirect after my frameworks had done one or
more forwards. Several filters are involved as well. I tried to diagnose it
all but it became too complicated for me to unravel.)

BTW, I analyzed the behavior in Jetty and found that the client was
responding to the redirect before the original request had completed, i.e.
the handling of the redirect request was taking place in my servlet even
before the original error-causing request had completed. The debugging
statements for both requests were intermingled in my log files. 

Brad


Christopher Schultz-2 wrote:
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Brad,
> 
> On 2/17/2009 12:32 AM, Brad Whitaker wrote:
>>         try {
>> 
>>             if(LOG.isDebugEnabled()) {
>>                 LOG.debug("Executing redirect with response
>> ["+response+"]");
>>             }
>>             String redirectUrl = response.encodeRedirectURL(actualUri);
>>             response.sendRedirect(redirectUrl);
>> 
>>         } catch (IOException e) {
>>             throw new ControllerExecutionException("Error redirecting
>> request for url ["+actualUri +"]: " + e.getMessage(),e);
>>         } catch (Exception ex) {
>>             LOG.error("redirectResponse: caught Exception=" + ex);
>>             throw new ControllerExecutionException("Error redirecting
>> request for url [" + actualUri + "]: " + ex.getMessage(), ex);
>>         }
>>         return null;
> 
> This should totally work.
> 
>> I'm certain that the redirect is never sent to the browser. I've been
>> using
>> Firebug and HttpWatch to monitor each request and response. When I
>> execute
>> this code on Jetty I do see the redirect sent to the browser.
> 
> How do you know that no exceptions are being thrown?
> 
>> I took a look at the Error Handling sections of the Servlet spec and it
>> wasn't clear to me if perhaps the redirect is not permitted after an
>> exception has been thrown.
> 
> Exceptions are orthogonal to request/response processing. If you allow
> the exception to propagate (that is, you don't catch it), then Tomcat
> typically sends a 500 Server Error response to the client. There is no
> requirement for exception handling in the servlet spec (other than those
> implied by the API itself) and no reason a "regular" response can't be
> given even in an exceptional condition.
> 
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
> 
> iEYEARECAAYFAkmcXiIACgkQ9CaO5/Lv0PArfQCdFhJqH2nEPLirJrjfo8+P7Efk
> V/IAnRFdu9dTlMd7B/m3zlut1Xk7Ej5t
> =Mb/0
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/HttpServletResponse.sendRedirect%28%29-not-working--tp22049004p22090697.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: HttpServletResponse.sendRedirect() not working?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Brad,

On 2/17/2009 12:32 AM, Brad Whitaker wrote:
>         try {
> 
>             if(LOG.isDebugEnabled()) {
>                 LOG.debug("Executing redirect with response
> ["+response+"]");
>             }
>             String redirectUrl = response.encodeRedirectURL(actualUri);
>             response.sendRedirect(redirectUrl);
> 
>         } catch (IOException e) {
>             throw new ControllerExecutionException("Error redirecting
> request for url ["+actualUri +"]: " + e.getMessage(),e);
>         } catch (Exception ex) {
>             LOG.error("redirectResponse: caught Exception=" + ex);
>             throw new ControllerExecutionException("Error redirecting
> request for url [" + actualUri + "]: " + ex.getMessage(), ex);
>         }
>         return null;

This should totally work.

> I'm certain that the redirect is never sent to the browser. I've been using
> Firebug and HttpWatch to monitor each request and response. When I execute
> this code on Jetty I do see the redirect sent to the browser.

How do you know that no exceptions are being thrown?

> I took a look at the Error Handling sections of the Servlet spec and it
> wasn't clear to me if perhaps the redirect is not permitted after an
> exception has been thrown.

Exceptions are orthogonal to request/response processing. If you allow
the exception to propagate (that is, you don't catch it), then Tomcat
typically sends a 500 Server Error response to the client. There is no
requirement for exception handling in the servlet spec (other than those
implied by the API itself) and no reason a "regular" response can't be
given even in an exceptional condition.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkmcXiIACgkQ9CaO5/Lv0PArfQCdFhJqH2nEPLirJrjfo8+P7Efk
V/IAnRFdu9dTlMd7B/m3zlut1Xk7Ej5t
=Mb/0
-----END PGP SIGNATURE-----

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


Re: HttpServletResponse.sendRedirect() not working?

Posted by Brad Whitaker <db...@yahoo.com>.
There's a lot of processing after the sendRedirect(). The sendRedirect() is
contained in Grails platform code and I confess that I don't fully
understand the motivation behind all of the processing. The sendRedirect()
has occurred after an application defined exception has been thrown. A
Spring exception resolver has forwarded the request after the exception has
been thrown. sendRedirect() occurs after the forward.

Here is the code that sends the redirect. It is Grails code except that I
added some of the verbose logging statements and the "catch (Exception ex)"
just to be sure I wasn't missing an exception:

    private Object redirectResponse(String actualUri, HttpServletResponse
response) {
        if(LOG.isDebugEnabled()) {
            LOG.debug("Dynamic method [redirect] forwarding request to
["+actualUri +"]");
        }

        try {

            if(LOG.isDebugEnabled()) {
                LOG.debug("Executing redirect with response
["+response+"]");
            }
            String redirectUrl = response.encodeRedirectURL(actualUri);
            response.sendRedirect(redirectUrl);

        } catch (IOException e) {
            throw new ControllerExecutionException("Error redirecting
request for url ["+actualUri +"]: " + e.getMessage(),e);
        } catch (Exception ex) {
            LOG.error("redirectResponse: caught Exception=" + ex);
            throw new ControllerExecutionException("Error redirecting
request for url [" + actualUri + "]: " + ex.getMessage(), ex);
        }
        return null;
    }


I'm certain that the redirect is never sent to the browser. I've been using
Firebug and HttpWatch to monitor each request and response. When I execute
this code on Jetty I do see the redirect sent to the browser.

I took a look at the Error Handling sections of the Servlet spec and it
wasn't clear to me if perhaps the redirect is not permitted after an
exception has been thrown.

Any suggestions would be appreciated.


Qiao Jin wrote:
> 
> What's after the sendRedirect() call in your code? And what's in the
> final?
> Can you post a simple code sample that would demonstrate the problem?
> 
> On Mon, Feb 16, 2009 at 5:55 PM, Brad Whitaker <db...@yahoo.com> wrote:
> 
>>
>> I'm having a problem with Tomcat 6.0.18 that I don't understand. I'm
>> invoking
>> HttpServletResponse.sendRedirect() but no redirect is being sent to the
>> browser. Is there a valid reason why this might occur, or is this likley
>> a
>> bug in Tomcat?
>>
>> My code has thrown an exception prior to invoking the redirect but I
>> can't
>> find anything in the Servlet spec that would indicate this is a problem.
>> The
>> HttpServletResponse.sendRedirect() is within a try/catch block and no
>> exception is being thrown in response to sendRedirect(). My app is built
>> on
>> Grails (which in turn is built on top of Spring) and I don't fully
>> understand the exception resolution process used by my frameworks, but I
>> have verified that sendRedirect() is being executed and not throwing an
>> exception. This same code is working as desired when executed on Jetty
>> (i.e.
>> the desired redirect is received by the browser) but when executed in
>> Tomcat
>> I see the default Tomcat error page displayed rather than the redirect.
>>
>> Any advice or suggestions would be appreciated.
>> --
>> View this message in context:
>> http://www.nabble.com/HttpServletResponse.sendRedirect%28%29-not-working--tp22049004p22049004.html
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/HttpServletResponse.sendRedirect%28%29-not-working--tp22049004p22051065.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


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


Re: HttpServletResponse.sendRedirect() not working?

Posted by Qiao Jin <br...@gmail.com>.
What's after the sendRedirect() call in your code? And what's in the final?
Can you post a simple code sample that would demonstrate the problem?

On Mon, Feb 16, 2009 at 5:55 PM, Brad Whitaker <db...@yahoo.com> wrote:

>
> I'm having a problem with Tomcat 6.0.18 that I don't understand. I'm
> invoking
> HttpServletResponse.sendRedirect() but no redirect is being sent to the
> browser. Is there a valid reason why this might occur, or is this likley a
> bug in Tomcat?
>
> My code has thrown an exception prior to invoking the redirect but I can't
> find anything in the Servlet spec that would indicate this is a problem.
> The
> HttpServletResponse.sendRedirect() is within a try/catch block and no
> exception is being thrown in response to sendRedirect(). My app is built on
> Grails (which in turn is built on top of Spring) and I don't fully
> understand the exception resolution process used by my frameworks, but I
> have verified that sendRedirect() is being executed and not throwing an
> exception. This same code is working as desired when executed on Jetty
> (i.e.
> the desired redirect is received by the browser) but when executed in
> Tomcat
> I see the default Tomcat error page displayed rather than the redirect.
>
> Any advice or suggestions would be appreciated.
> --
> View this message in context:
> http://www.nabble.com/HttpServletResponse.sendRedirect%28%29-not-working--tp22049004p22049004.html
> Sent from the Tomcat - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: HttpServletResponse.sendRedirect() not working?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Brad,

On 2/16/2009 8:55 PM, Brad Whitaker wrote:
> Is there a valid reason why this might occur, or is this likley a
> bug in Tomcat?

A bug is unlikely. I would guess that 100% of webapps out there use
redirects and they work perfectly fine.

> My code has thrown an exception prior to invoking the redirect but I can't
> find anything in the Servlet spec that would indicate this is a problem.

Er... if your code throws an exception then the sendRedirect call will
never occur because your method is done executing. Can you post some code?

> The
> HttpServletResponse.sendRedirect() is within a try/catch block and no
> exception is being thrown in response to sendRedirect().

The exception is probably happening before the sendRedirect. Are you
swallowing any exceptions (that is, catching them and ignoring them)?

> I
> have verified that sendRedirect() is being executed and not throwing an
> exception.

How have you verified that the sendRedirect is being executed?

> This same code is working as desired when executed on Jetty (i.e.
> the desired redirect is received by the browser) but when executed in Tomcat
> I see the default Tomcat error page displayed rather than the redirect.

Everything else is the same? All the libraries are at the same patch
level, etc.?

Are you seeing any errors in your Tomcat log files?

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkmcXX0ACgkQ9CaO5/Lv0PA4lQCbBogI29p8tS7vSsRRgfasrJVn
LAYAn09+8k1ydcCT7nU0mNOwIVRQUiF7
=Sa/K
-----END PGP SIGNATURE-----

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


RE: HttpServletResponse.sendRedirect() not working?

Posted by Martin Gainty <mg...@hotmail.com>.
if you were forwarding..that would be a TC error

a redirect sends response back to browser-client  
its up to browser-client to then accept the response and re-direct the client Request to new url

//an alternative strategy would be to build the URL consisting of URL and http-get parameters e.g.
String url_string ="http://somehost.com"+"&parm1=value1&parm2=value2";
//call the URL directly (essantially a forward as the session is preserved)
java.net.URL url = new java.net.URL(url_string);
//read the response from url into Buffered Reader
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

HTH
Martin 
______________________________________________ 
Disclaimer and confidentiality note 
Everything in this e-mail and any attachments relates to the official business of Sender. This transmission is of a confidential nature and Sender does not endorse distribution to any party other than intended recipient. Sender does not necessarily endorse content contained within this transmission. 




> Date: Mon, 16 Feb 2009 17:55:09 -0800
> From: dbwbca@yahoo.com
> To: users@tomcat.apache.org
> Subject: HttpServletResponse.sendRedirect() not working?
> 
> 
> I'm having a problem with Tomcat 6.0.18 that I don't understand. I'm invoking
> HttpServletResponse.sendRedirect() but no redirect is being sent to the
> browser. Is there a valid reason why this might occur, or is this likley a
> bug in Tomcat?
> 
> My code has thrown an exception prior to invoking the redirect but I can't
> find anything in the Servlet spec that would indicate this is a problem. The
> HttpServletResponse.sendRedirect() is within a try/catch block and no
> exception is being thrown in response to sendRedirect(). My app is built on
> Grails (which in turn is built on top of Spring) and I don't fully
> understand the exception resolution process used by my frameworks, but I
> have verified that sendRedirect() is being executed and not throwing an
> exception. This same code is working as desired when executed on Jetty (i.e.
> the desired redirect is received by the browser) but when executed in Tomcat
> I see the default Tomcat error page displayed rather than the redirect.
> 
> Any advice or suggestions would be appreciated.
> -- 
> View this message in context: http://www.nabble.com/HttpServletResponse.sendRedirect%28%29-not-working--tp22049004p22049004.html
> Sent from the Tomcat - User mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

_________________________________________________________________
See how Windows connects the people, information, and fun that are part of your life.
http://clk.atdmt.com/MRT/go/msnnkwxp1020093175mrt/direct/01/