You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by jim ma <ma...@gmail.com> on 2008/11/20 09:03:22 UTC

How to prevent Tomcat redirect my request

Hi all,
I deployed a web application "foo.war" into tomcat 5.x .  When I used
httpclient to send post request to "http://localhost:8080/foo", I always
get the http 302 redirect response .  How can I prevent Tomcat to reply
redirect response and directly adding the slash  to my request
url "http://localhost:8080/foo/" to get what I want . I see web browser can
automcatically resend the request to redirected location but httpclient
can not.  Is there  configurations for that ?   Could anyone shed  some
lights ?

Thanks

Jim

Re: How to prevent Tomcat redirect my request

Posted by André Warnier <aw...@ice-sa.com>.
jim ma wrote:
>> Tell your clients to generate URLs like:
>> http://host/your_servlet?url=url_to_fetch
>> In other words you should implemet proxy that will handle redirects itself.
>> Finally, your clients will not see any 302 redirects.
> 
> 
> I like this idea.  It is greatly helpful  , I think it works for my problem.
> 
That is a cute idea.

Here is another one :

First, I am not telling you that you should put an Apache in front of 
your Tomcat just for this. (*)

But, if you are anyway planning to put an Apache in front of your Tomcat 
for any reason (load balancing, serving other types of content, cgi, 
..), then
in Apache you can easily "catch" a request before it gets directed to 
any directory (or to Tomcat), in time to rewrite the URL internally 
(thus without sending a 302 to the client).

In Apache, mod_rewrite can be set up to act at the Server level, very 
early in the request cycle (RewriteCond, RewriteRule directives).
You can also do all sorts of subtle things with mod_perl handlers.






(*) I was tempted to do just that, just to provoke a blasting reply from 
Chuck or Chris, but I won't.

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


Re: How to prevent Tomcat redirect my request

Posted by jim ma <ma...@gmail.com>.
> Tell your clients to generate URLs like:
> http://host/your_servlet?url=url_to_fetch
> In other words you should implemet proxy that will handle redirects itself.
> Finally, your clients will not see any 302 redirects.


I like this idea.  It is greatly helpful  , I think it works for my problem.


>
>
> --
> Mikolaj Rydzewski <mi...@ceti.pl>
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: How to prevent Tomcat redirect my request

Posted by Mikolaj Rydzewski <mi...@ceti.pl>.
jim ma wrote:
> The problem is that I can not simply correct that URL and
> send it for the f
> first  time.   The request url is generated by other  application or in some
> condition sometimes provided by  user . The client side  can not  judge "
> http://localhost:8080/foo"  is a request for "foo"   resource in root
> context or a request url for the default page in foo context.   Server side
> has that information and can judge it .  If there is a foo context , it will
> reply the redirect response.
> httpunit can help that.  Actually I want to know if there is second
> option(configure tomcat to support that ) 
Tomcat behaves correctly. Your clients send incorrect URLs.

You really should not hack Tomcat to handle redirects in a different way.

Since  you use HttpClient (I mean 
org.apache.commons.httpclient.HttpClient) you can check response status 
code and perform second request if status code is one of 
HttpStatus.SC_MOVED_TEMPORARILY or HttpStatus.SC_MOVED_PERMANENTLY. E.g.:


client.executeMethod(method);
responseCode = method.getStatusCode();
responseBody = method.getResponseBodyAsString();
           
if (responseCode == HttpStatus.SC_MOVED_TEMPORARILY || responseCode == 
HttpStatus.SC_MOVED_PERMANENTLY) {
    String redirectLocation;
    Header locationHeader = method.getResponseHeader("Location");
    if (locationHeader != null) {
        redirectLocation = locationHeader.getValue();
        method = new GetMethod(redirectLocation);
        client.executeMethod(method);
        responseCode = method.getStatusCode();
        responseBody = method.getResponseBodyAsString();
    }
}


I really do not understand your problem ;-)

If you don't have absolutely any control on URLs clients generate or 
http-client code your application uses you can try following:
Implement simle servlet that will accept any URL as parameter. Servlet 
will use http-client in way described before (e.g. will handle redirects).
Tell your clients to generate URLs like: 
http://host/your_servlet?url=url_to_fetch
In other words you should implemet proxy that will handle redirects itself.
Finally, your clients will not see any 302 redirects.

-- 
Mikolaj Rydzewski <mi...@ceti.pl>


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


Re: How to prevent Tomcat redirect my request

Posted by jim ma <ma...@gmail.com>.
On Fri, Nov 21, 2008 at 10:53 AM, Caldarale, Charles R <
Chuck.Caldarale@unisys.com> wrote:

> > From: jim ma [mailto:mail2jimma@gmail.com]
> > Subject: Re: How to prevent Tomcat redirect my request
> >
> > If I get 302 response and write some code to resend
> > quest to the redirected location
>
> The point everyone's trying to make is that you should send the correct URL
> the *first* time, rather than sending one that doesn't comply with the HTTP
> spec and letting Tomcat correct it for you.
>

Thanks , Chunk.  The problem is that I can not simply correct that URL and
send it for the f
first  time.   The request url is generated by other  application or in some
condition sometimes provided by  user . The client side  can not  judge "
http://localhost:8080/foo"  is a request for "foo"   resource in root
context or a request url for the default page in foo context.   Server side
has that information and can judge it .  If there is a foo context , it will
reply the redirect response.
httpunit can help that.  Actually I want to know if there is second
option(configure tomcat to support that ) .

RE: How to prevent Tomcat redirect my request

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: jim ma [mailto:mail2jimma@gmail.com]
> Subject: Re: How to prevent Tomcat redirect my request
>
> If I get 302 response and write some code to resend
> quest to the redirected location

The point everyone's trying to make is that you should send the correct URL the *first* time, rather than sending one that doesn't comply with the HTTP spec and letting Tomcat correct it for you.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.

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


Re: How to prevent Tomcat redirect my request

Posted by André Warnier <aw...@ice-sa.com>.
jim ma wrote:
> On Thu, Nov 20, 2008 at 10:24 PM, André Warnier <aw...@ice-sa.com> wrote:
> 
>> jim ma wrote:
>>
>>> On Thu, Nov 20, 2008 at 6:48 PM, André Warnier <aw...@ice-sa.com> wrote:
>>>
>>>  Mikolaj Rydzewski wrote:
>>>>  jim ma wrote:
>>>>>  I still want to know if it is possible to  stop Tomcat from
>>>>>> redirecting.
>>>>>> If
>>>>>> yes, that will be zero code effort for our current implementation.
>>>>>>
>>>>>>
>>>>>>  Why is it a problem for you to use http://localhost:8080/foo/ URL?
>>>>>  I agree with the above, but in case it is not an option, you may want
>>>>> to
>>>>>
>>>> look at something like this :
>>>>
>>>> http://www.tuckey.org/urlrewrite/
>>>>
>>>> It does many more things, but I guess it can rewrite "/foo" into "/foo/"
>>>> internally too.
>>>>
>>>>
>>>  I  just debugged the code. It returns http status code 302 and redirected
>>> location  http://localhost:8080/foo/ before reach the code related to url
>>> rewrite configuration in web.xml .
>>>
>>>  Yes, that's true of course. Stupid me.
>> Urlrewrite is a servlet filter, so it will not see the request before it
>> has been directed to the webapp, and thus the redirect will happen before.
>> Duh.
>> I guess you would need some kind of re-directing Valve for that.
> 
> 
> I also try to add a rewrite Valve to StandardEngine. And it is also does not
> work . Before reach that Valve, tomcat already replied that
> redirect response.
> 
> 
> 
>> Now, about what Mikolaj wrote before (using the /foo/ URL) I think you
>> misunderstand what he is saying.
>> What he meant is probably this :
>> You seem to be using a http client that is not a browser, but some kind of
>> program or module.  Can you not make sure that this program or module does
>> not send URLs like "/foo", but itself transforms them into "/foo/" at the
>> source ?
> 
> 
> Yes , I can do that . If I get 302 response and write some code to resend
> quest to the redirected location, then I can always get what I want . It is
> not efficient , you know.  It needs to talk to tomcat server twice . Is
> there shortcut way to do that ?
> 
Yes : make it so that your *first* request contains the trailing "/".
In many words :
currently your "httpclient" sends a first request with the URL "/foo", 
and Tomcat then responds with a 302 to "/foo/".
Can you not make it so that your "httpclient" sends the request for 
"/foo/" right away, the first time ?
(then Tomcat would not need to send a 302, it would give you "/foo/" 
right away).

Or, in another way again : why does your httpclient send a first request 
"/foo" without the trailing "/" ?


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


Re: How to prevent Tomcat redirect my request

Posted by jim ma <ma...@gmail.com>.
On Thu, Nov 20, 2008 at 10:24 PM, André Warnier <aw...@ice-sa.com> wrote:

> jim ma wrote:
>
>> On Thu, Nov 20, 2008 at 6:48 PM, André Warnier <aw...@ice-sa.com> wrote:
>>
>>  Mikolaj Rydzewski wrote:
>>>
>>>  jim ma wrote:
>>>>
>>>>  I still want to know if it is possible to  stop Tomcat from
>>>>> redirecting.
>>>>> If
>>>>> yes, that will be zero code effort for our current implementation.
>>>>>
>>>>>
>>>>>  Why is it a problem for you to use http://localhost:8080/foo/ URL?
>>>>
>>>>  I agree with the above, but in case it is not an option, you may want
>>>> to
>>>>
>>> look at something like this :
>>>
>>> http://www.tuckey.org/urlrewrite/
>>>
>>> It does many more things, but I guess it can rewrite "/foo" into "/foo/"
>>> internally too.
>>>
>>>
>>  I  just debugged the code. It returns http status code 302 and redirected
>> location  http://localhost:8080/foo/ before reach the code related to url
>> rewrite configuration in web.xml .
>>
>>  Yes, that's true of course. Stupid me.
> Urlrewrite is a servlet filter, so it will not see the request before it
> has been directed to the webapp, and thus the redirect will happen before.
> Duh.
> I guess you would need some kind of re-directing Valve for that.


I also try to add a rewrite Valve to StandardEngine. And it is also does not
work . Before reach that Valve, tomcat already replied that
redirect response.



> Now, about what Mikolaj wrote before (using the /foo/ URL) I think you
> misunderstand what he is saying.
> What he meant is probably this :
> You seem to be using a http client that is not a browser, but some kind of
> program or module.  Can you not make sure that this program or module does
> not send URLs like "/foo", but itself transforms them into "/foo/" at the
> source ?


Yes , I can do that . If I get 302 response and write some code to resend
quest to the redirected location, then I can always get what I want . It is
not efficient , you know.  It needs to talk to tomcat server twice . Is
there shortcut way to do that ?

Re: How to prevent Tomcat redirect my request

Posted by André Warnier <aw...@ice-sa.com>.
jim ma wrote:
> On Thu, Nov 20, 2008 at 6:48 PM, André Warnier <aw...@ice-sa.com> wrote:
> 
>> Mikolaj Rydzewski wrote:
>>
>>> jim ma wrote:
>>>
>>>> I still want to know if it is possible to  stop Tomcat from redirecting.
>>>> If
>>>> yes, that will be zero code effort for our current implementation.
>>>>
>>>>
>>> Why is it a problem for you to use http://localhost:8080/foo/ URL?
>>>
>>>  I agree with the above, but in case it is not an option, you may want to
>> look at something like this :
>>
>> http://www.tuckey.org/urlrewrite/
>>
>> It does many more things, but I guess it can rewrite "/foo" into "/foo/"
>> internally too.
>>
> 
>  I  just debugged the code. It returns http status code 302 and redirected
> location  http://localhost:8080/foo/ before reach the code related to url
> rewrite configuration in web.xml .
> 
Yes, that's true of course. Stupid me.
Urlrewrite is a servlet filter, so it will not see the request before it 
has been directed to the webapp, and thus the redirect will happen before.
Duh.
I guess you would need some kind of re-directing Valve for that.

Now, about what Mikolaj wrote before (using the /foo/ URL) I think you 
misunderstand what he is saying.
What he meant is probably this :
You seem to be using a http client that is not a browser, but some kind 
of program or module.  Can you not make sure that this program or module 
does not send URLs like "/foo", but itself transforms them into "/foo/" 
at the source ?

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


Re: How to prevent Tomcat redirect my request

Posted by jim ma <ma...@gmail.com>.
On Thu, Nov 20, 2008 at 6:48 PM, André Warnier <aw...@ice-sa.com> wrote:

> Mikolaj Rydzewski wrote:
>
>> jim ma wrote:
>>
>>> I still want to know if it is possible to  stop Tomcat from redirecting.
>>> If
>>> yes, that will be zero code effort for our current implementation.
>>>
>>>
>> Why is it a problem for you to use http://localhost:8080/foo/ URL?
>>
>>  I agree with the above, but in case it is not an option, you may want to
> look at something like this :
>
> http://www.tuckey.org/urlrewrite/
>
> It does many more things, but I guess it can rewrite "/foo" into "/foo/"
> internally too.
>

 I  just debugged the code. It returns http status code 302 and redirected
location  http://localhost:8080/foo/ before reach the code related to url
rewrite configuration in web.xml .


>
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: How to prevent Tomcat redirect my request

Posted by jim ma <ma...@gmail.com>.
On Thu, Nov 20, 2008 at 6:48 PM, André Warnier <aw...@ice-sa.com> wrote:

> Mikolaj Rydzewski wrote:
>
>> jim ma wrote:
>>
>>> I still want to know if it is possible to  stop Tomcat from redirecting.
>>> If
>>> yes, that will be zero code effort for our current implementation.
>>>
>>>
>> Why is it a problem for you to use http://localhost:8080/foo/ URL?
>>
>>  I agree with the above, but in case it is not an option, you may want to
> look at something like this :
>
> http://www.tuckey.org/urlrewrite/
>
> It does many more things, but I guess it can rewrite "/foo" into "/foo/"
> internally too.
>
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: How to prevent Tomcat redirect my request

Posted by André Warnier <aw...@ice-sa.com>.
Mikolaj Rydzewski wrote:
> jim ma wrote:
>> I still want to know if it is possible to  stop Tomcat from 
>> redirecting. If
>> yes, that will be zero code effort for our current implementation.
>>   
> Why is it a problem for you to use http://localhost:8080/foo/ URL?
> 
I agree with the above, but in case it is not an option, you may want to 
look at something like this :

http://www.tuckey.org/urlrewrite/

It does many more things, but I guess it can rewrite "/foo" into "/foo/" 
internally too.


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


Re: How to prevent Tomcat redirect my request

Posted by jim ma <ma...@gmail.com>.
On Thu, Nov 20, 2008 at 6:19 PM, Mikolaj Rydzewski <mi...@ceti.pl> wrote:

> jim ma wrote:
>
>> I still want to know if it is possible to  stop Tomcat from redirecting.
>> If
>> yes, that will be zero code effort for our current implementation.
>>
>>
> Why is it a problem for you to use http://localhost:8080/foo/ URL?
>

 Because I would like to let tomcat server http://localhost:8080/foo
successfully and do not reply a redirect response and resend that redirected
location .  It
is more efficient , right ?


> --
> Mikolaj Rydzewski <mi...@ceti.pl>
>
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: How to prevent Tomcat redirect my request

Posted by Mikolaj Rydzewski <mi...@ceti.pl>.
jim ma wrote:
> I still want to know if it is possible to  stop Tomcat from redirecting. If
> yes, that will be zero code effort for our current implementation.
>   
Why is it a problem for you to use http://localhost:8080/foo/ URL?

-- 
Mikolaj Rydzewski <mi...@ceti.pl>


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


Re: How to prevent Tomcat redirect my request

Posted by jim ma <ma...@gmail.com>.
Great thanks , Kees.  I will try httpunit .

I still want to know if it is possible to  stop Tomcat from redirecting. If
yes, that will be zero code effort for our current implementation.

Thanks

Jim


On Thu, Nov 20, 2008 at 5:51 PM, Kees Jan Koster <kj...@gmail.com> wrote:

> Dear Jim,
>
>
>  I deployed a web application "foo.war" into tomcat 5.x .  When I used
>> httpclient to send post request to "http://localhost:8080/foo", I always
>> get the http 302 redirect response .  How can I prevent Tomcat to reply
>> redirect response and directly adding the slash  to my request
>> url "http://localhost:8080/foo/" to get what I want . I see web browser
>> can
>> automcatically resend the request to redirected location but httpclient
>> can not.  Is there  configurations for that ?   Could anyone shed  some
>> lights ?
>>
>
> I usually use httpunit, not httpclient. The advantage is that it does all
> the cookies, 302 responses and lord knows what else in the HTTP protocol,
> allowing me to focus on the logic instead of the protocol.
>
> Maybe that's an alternative route for you, if Tomcat cannot be stopped from
> redirecting your app.
> --
> Kees Jan
>
> http://java-monitor.com/forum/
> kjkoster@kjkoster.org
> 06-51838192
>
> Rule 1 for being in a hole: stop digging.
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: How to prevent Tomcat redirect my request

Posted by Kees Jan Koster <kj...@gmail.com>.
Dear Jim,

> I deployed a web application "foo.war" into tomcat 5.x .  When I used
> httpclient to send post request to "http://localhost:8080/foo", I  
> always
> get the http 302 redirect response .  How can I prevent Tomcat to  
> reply
> redirect response and directly adding the slash  to my request
> url "http://localhost:8080/foo/" to get what I want . I see web  
> browser can
> automcatically resend the request to redirected location but  
> httpclient
> can not.  Is there  configurations for that ?   Could anyone shed   
> some
> lights ?

I usually use httpunit, not httpclient. The advantage is that it does  
all the cookies, 302 responses and lord knows what else in the HTTP  
protocol, allowing me to focus on the logic instead of the protocol.

Maybe that's an alternative route for you, if Tomcat cannot be stopped  
from redirecting your app.
--
Kees Jan

http://java-monitor.com/forum/
kjkoster@kjkoster.org
06-51838192

Rule 1 for being in a hole: stop digging.


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