At 02:57 PM 10/4/01 -0400, Matt Feifarek wrote:
>I'd like a little clarification on how sendRedirect() actually works:
>
>1. What happens to the running servlet after sendRedirect is sent?
>
>Obviously, it doesn't do its output, but what DOES it do? We're noticing
>that if a redirect is sent during the awake() method, that method finishes
>up its business (we can see this through print statements).
>
>2. What happens next? Is it jumping straight to sleep()?
>
>3. Where is the _ideal_ place to trigger the sendRedirect?
>
>4. Is it ok to do in awake()?
>
>As an aside, I would LOVE a flowchart that shows the branching/paths of a
>servlet as it travels from awake() to sleep(). Perhaps that could be in the
>docs someday. I'd make one, if I felt that I was qualified to understand the
>mechanism; if someone can explain it to me, I'll do the graphics work.

This is what HTTPResponse.sendRedirect does:

def sendRedirect(self, url):
     self.setHeader('Status', '302 Redirect')
     self.setHeader('Location', url)
     self.setHeader('Content-type', 'text/html')
     self.write('<html> <body> This page has been redirected to <a 
href="%s">%s</a>. </body> </html>' % (url, url))

So as you can see, all it does is set some headers and write out some 
text.  It doesn't jump anywhere -- your servlet continues processing 
normally.  When the browser gets the response, it does the redirect.  Under 
some circumstances, the web server will handle the redirect internally so 
that the browser doesn't "know" that a redirect has happened; I think this 
happens when the URL you're redirecting to starts with a "/", but it might 
depend on which web server you're using.

I think it's perfectly OK to call sendRedirect() from awake().

When you call sendRedirect, you generally shouldn't write anything else 
out.  There are many ways to accomplish that, but here are two ideas:

### idea 1
class MyPage(Page):
     def writeHTML(self):
         if shouldUseRedirect():
             self.response().sendRedirect(url)
         else:
             Page.writeHTML(self)

     def writeContent(self):
         # here goes the content you want to write if you're NOT redirecting...

### idea 2
class MyPage(Page):
     def awake(self, trans):
         Page.awake(self, trans)
         if shouldUseRedirect():
             self.response.sendRedirect(url)

     def writeHTML(self):
         if self.response().hasHeader('Location'):
             return
         # here goes the content you want to write if you're NOT redirecting...

The other thing to consider is using self.application().forwardRequest() 
instead of sendRedirect().  This causes the forwarding to happen entirely 
inside of the server instead of bouncing the redirect back to the 
application.  The advantage is that forwarding is very fast.  The 
disadvantage is that the URL that the user sees in their browser is the 
original URL, not the one that you forwarded to.


--

- Geoff Talvola
   [EMAIL PROTECTED]

_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to