Saturday, January 13, 2007

What happens inside JSP/Struct/JSF Redirect?

Let’s use the following servlet to demo a redirect.
public class RedirectServlet extends HttpServlet
{
public void service(HttpServletRequest req,
HttpServletResponse resp)
{
String contextPath = req.getContextPath();
String redirectStr = contextPath +
"/nextpage.jsp?param1=value1";
// Use encodeRedirectURL so that session id can be
// included in browser which doesn't support cookies.
resp.sendRedirect(resp.encodeRedirectURL(redirectStr));
}
}
Then, we use Telnet to access this servlet:
% telnet machine_hostname 8080

Manually enter:
GET /servlet/RedirectServlet HTTP/1.0

You will get the reply:
HTTP1.1 302 Moved Temporarily
Location: http://machine_hostname:8080/webapp/nextpage.jsp?param1=value1
DATE: ...
Server: ...
Connection: ...


What to observe is that,
1) Redirect in fact is a 302 response from webserver.
2) The complete URL and Port are written out fully in the Location.

This is the reason why the internal machine hostname will be shown up in browser, when we login OracleAS EM (Enterprise Manager) or perform a JSP/Struct/JSF Redirect in our web application.

Please check how to let EM and Redirect return URL as expected-ly.

No comments: