tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andy Eastham" <a...@barllama.demon.co.uk>
Subject RE: Using sendRedirect puts me into an infinite loop...
Date Wed, 16 Jul 2003 10:49:18 GMT
Mike,

The documentation is always your friend :-)

The docs say that relative urls are interpreted relative to the CURRENT
REQUEST URI, so the behaviour is as advertised:

*****************************

sendRedirect
public void sendRedirect(java.lang.String location)
                  throws java.io.IOExceptionSends a temporary redirect
response to the client using the specified redirect location URL. This
method can accept relative URLs; the servlet container must convert the
relative URL to an absolute URL before sending the response to the client.
If the location is relative without a leading '/' the container interprets
it as relative to the current request URI. If the location is relative with
a leading '/' the container interprets it as relative to the servlet
container root.
If the response has already been committed, this method throws an
IllegalStateException. After using this method, the response should be
considered to be committed and should not be written to.

Parameters:
location - the redirect location URL
Throws:
java.io.IOException - If an input or output exception occurs
IllegalStateException - If the response was committed

********************************

You need to hack it with starting the url off with /SHOW or get the
application name from the ServletContext, or read the start of the current
URI, or get it from a config file or somewhere.

All the best,

Andy

> -----Original Message-----
> From: Mike Curwen [mailto:mikec@gb-im.com]
> Sent: 15 July 2003 23:26
> To: tomcat-user@jakarta.apache.org
> Subject: Using sendRedirect puts me into an infinite loop...
>
>
>
>
> Sorry for posting this here, but I either have a pretty big
> misunderstanding of something (most likely) or I've discovered a bug????
>
> I have Apache Web Server set up thusly:
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> <VirtualHost xxx.xxx.xxx.xxx>
>       DocumentRoot /home/webhome/myapp/
>       ServerName www.myApp.com
>       ErrorLog /var/log/myapp/error_log
>       CustomLog /var/log/myapp/access_log combined
> </VirtualHost>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Then in Tomcat server.xml:
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> <Context path="/SHOW" docBase="/home/webhome/myapp/"
> defaultSessionTimeout="60">
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> And finally in the web.xml for the application in question:
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>         <servlet>
>                 <servlet-name>Trans</servlet-name>
>                 <servlet-class>com.acme.Translator</servlet-class>
>                 <load-on-startup>0</load-on-startup>
>         </servlet>
> .
> .
> .
>         <servlet-mapping>
>           <servlet-name>Trans</servlet-name>
>           <url-pattern>/</url-pattern>
>         </servlet-mapping>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> So I think you can see with this setup, I have something like this:
>
> http://www.myapp.com/SHOW/1/Mike.Curwen
> http://www.myapp.com/SHOW/2/Other.Person
> http://www.myapp.com/SHOW/admin
> http://www.myapp.com/SHOW/reports
>
> The /admin and /reports requests will map to other 'known' servlets in
> web.xml  (and these work properly).
>
> Anything 'not' recognized will be picked up by my Translating servlet.
> (the default servlet) It expects certain formats.. meaning
> #/FirstName.LastName
>
> Never mind the robustness of that.. here is the code within the
> translator (the code of interest)
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> public void handleRequest( HttpServletRequest request,
> HttpServletResponse response) throws Exception {
>
> 	HttpSession session = request.getSession(true);
>
> 	logger.debug("URI: " + request.getRequestURI());
> 	logger.debug("URL: " + request.getRequestURL());
> 	logger.debug("PATH: " + request.getPathInfo());
>
>
> 	String what_was_requested = request.getRequestURI();
>
> 	String newURL=("foo?");
>
> 	// large block of code snipped ... it translates the
> 	// 'encoded' request into a normalized querystring
> 	// for the 'foo' servlet.
>
> 	newURL = response.encodeRedirectURL(newURL);
> 	logger.debug("SHOW:: " + request.getRequestURI() + " translates
> to: " + newURL);
> 	// redirect (our tracking filter won't pick up internally
> forwarded requests until servlet2.4)
> 	response.sendRedirect(newURL);
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>
> So I'm translating from a #/FirstName.LastName format into a queryString
> that all other parts of my application can use.
>
> Here is what I get when I make a request for
> http://www.myapp.com/SHOW/1/Mike.Curwen
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> DEBUG com.acme.Translator.handleRequest() - URI: /SHOW/1/Mike.Curwen
> (44348ms)
> DEBUG com.acme.Translator.handleRequest() - URL:
> http://www.myapp.com/SHOW/1/Mike.Curwen (44349ms)
> DEBUG com.acme.Translator.handleRequest() - PATH: null (44350ms)
> DEBUG com.acme.Translator.handleRequest() - pid 1 - 1 CACHED (44357ms)
> DEBUG com.acme.Translator.handleRequest() - SHOW:: /SHOW/1/Mike.Curwen
> translates to: foo?cid=1&pid=1&uid=1&src=browse&pgid=1 (44363ms)
> DEBUG com.acme.Translator.handleRequest() - URI: /SHOW/1/foo (44367ms)
> DEBUG com.acme.Translator.handleRequest() - URL:
> http://www.myapp.com/SHOW/1/foo (44368ms)
> DEBUG com.acme.Translator.handleRequest() - PATH: null (44368ms)
> DEBUG com.acme.Translator.handleRequest() - SHOW:: /SHOW/1/foo
> translates to: foo?cid=1&pid=1&uid=0&src=browse&pgid=1 (44373ms)
> DEBUG com.acme.Translator.handleRequest() - URI: /SHOW/1/foo (44376ms)
> DEBUG com.acme.Translator.handleRequest() - URL:
> http://www.myapp.com/SHOW/1/foo (44377ms)
> DEBUG com.acme.Translator.handleRequest() - PATH: null (44378ms)
> DEBUG com.acme.Translator.handleRequest() - SHOW:: /SHOW/1/foo
> translates to: foo?cid=1&pid=1&uid=0&src=browse&pgid=1 (44382ms)
> DEBUG com.acme.Translator.handleRequest() - URI: /SHOW/1/foo (44386ms)
> DEBUG com.acme.Translator.handleRequest() - URL:
> http://www.myapp.com/SHOW/1/foo (44387ms)
> DEBUG com.acme.Translator.handleRequest() - PATH: null (44388ms)
> DEBUG com.acme.Translator.handleRequest() - SHOW:: /SHOW/1/foo
> translates to: foo?cid=1&pid=1&uid=0&src=browse&pgid=1 (44392ms)
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> .
> .
> etc, etc, etc, until you kill Tomcat
>
> If I start my newURL (the translated URL) with "/foo?", then this will
> tell the container to translate the redirect relative to the container
> root. I don't want that, I want the application root. So I leave it as
> "foo?".  (I've tried it both ways: /foo? gives me a 404, and I'm missing
> the "/SHOW" token. But if I leave it off.... I get an infinite loop.
>
> It's almost as though somehow the container is determining that
> http://www.myapp.com/SHOW/1/Mike.Curwen
> translates to /SHOW/1  as an application 'root', instead of the expected
> (?) /SHOW/.
>
> So then of course,
> http://www.myapp.com/SHOW/1/foo?etcetc
> is 'unrecognized' and I go through the Translator again.
>
> The 2nd (and subsequent) calls to handleRequest() will attempt to parse
> foo?etcetc as a FirstName.LastName, and right now I have it defaulting
> to a test user (uid=0) when it can't find one.
>
> Am I right in thinking "The container is responsible for turning
> relative URLs into absolute URLs".  I sorta recall reading it somewhere.
> In this case though, it is not translating them correctly.
>
> Might this be a bug in JK ? In Tomcat? Or is this the expected
> behaviour?
>
>
> detail:
> Apache/2.0.45 (Unix) mod_jk/1.2.3
> Tomcat 4.1.24
> JDK 1.4.1
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>



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


Mime
View raw message