Servlet Development Guide : PART III Programming : Chapter 8 Creating Responses : Redirecting the Client
Redirecting the Client
The response object provides a function for returning an HTTP redirect. The sendRedirect() function takes a std::string containing a URL, and sets the response to an HTTP Temporary Redirect directing the client to the URL. The code sample below redirects a client to the Rogue Wave Web site:
 
response.sendRedirect("http://www.roguewave.com/");
The Servlet Specification allows the servlet container to transmit the response to the client as soon as the servlet calls sendRedirect(). Therefore, the servlet should not alter the response after calling sendRedirect(), since the client may not receive the changes.
The sendRedirect() function does not rewrite the URL to include session information. If the servlet uses URL rewriting to track sessions, the servlet should rewrite the URL before calling sendRedirect(). The sample below forwards the client to another servlet in the same context while preserving session information by calling encodeRedirectURL() on the response:
 
std::string loginURL =
response.encodeRedirectURL("/loginServlet/");
response.sendRedirect(loginURL);
Note that the sendRedirect() function sends an HTTP redirect message to the client.