[ You are here: XTF -> Programming -> Redirecting to Another URL ]

Redirecting to Another URL

You may encounter scenarios where the best thing to do isn't to generate the output for a given query or document request, but rather to redirect the user's browser to a different URL. While this can be done in the <meta> tag of the output HTML document, that method is not very clean. In particular, it prevents the "Back" button in the user's browser from working properly. If they hit the "Back" button, the browser simply encounters the redirect again, effectively blocking the user from getting back to where they came from.

The preferable method for redirecting is to generate an HTTP 302 status code. But how can a servlet do that? By using an XTF extension built just for this purpose. Here's an example of redirecting to a new URL:
<redirect:send url="http://another_URL_here"
               xmlns:redirect="java:/org.cdlib.xtf.saxonExt.Redirect"
               xsl:extension-element-prefixes="redirect"/>
This needs to be placed fairly early in the result or document formatter, before any other output. The redirect needs to be the first output; even having an HTML header before it will cause an exception to be thrown. Once the redirect instruction is executed, further stylesheet processing is aborted and the user is immediately taken to the new URL. For more information, see the XTF Tag Reference section on Redirect.