Saturday, April 28, 2012

Redirect to another Servlet or JSP

response.sendRedirect() 

Understanding Servlet redirect

response.sendRedirect sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative or absolute URL. Servlet container converts relative URL to absolute URL before sending response to 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.
This code snippet shows how to redirect to another URL from a servlet. The destination can be any URL, within the same application or out of it.

Redirect to another URL within same application.

Following snippet shows how to redirect to another JSP file.
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class RedirectServlet extends HttpServlet {
 
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException  {
 
/*
* Destination, it can be any relative or context specific path.
* if the path starts without '/' it is interpreted as relative to the current request URI.
* if the path starts with '/' it is interpreted as relative to the context.
*/
 
String destination  ="/jsp/destination.jsp";        
response.sendRedirect(response.encodeRedirectURL(destination));
 
 }
}

Redirect to another site

You can redirect to another site too.
response.sendRedirect(response.encodeRedirectURL("http://www.google.com"));

web.xml

    <servlet>
      <servlet-name>RedirectServlet</servlet-name>
      <servlet-class>RedirectServlet</servlet-class>                    
    </servlet>
 
    <servlet-mapping>
      <servlet-name>RedirectServlet</servlet-name>
      <url-pattern>/redirect</url-pattern>
    </servlet-mapping>

Redirect from JSP

Redirecting from JSP is same as above. Just put the call to response.sendRedirect() inside a scriptlet.
<%
String destination  ="/jsp/destination.jsp";        
response.sendRedirect(response.encodeRedirectURL(destination));
%>

Difference between redirect and forward

Servlet redirect and servlet forward both are used to handle the request processing to some other URL/Servlet but there is a big difference in the way it works. The main difference is
  • Servlet redirect always sends a HTTP status code 303 to client along with the redirect URL. Client then sends a new request to the URL.Thus response.sendRedirect() takes one more round trip than forward where as servlet forward just forwards the request to another resource on the server and it does not take a full round trip that is why forward is some time referred as server side redirect.
  • The another difference is you can redirect the request to some other URL on some another site but you can not forward the request to some other URL on different site.
  • Servlet forward will forward the existing request to another JSP or Servlet, so all the request parameters and attributes will be available to destination servlet. However with redirect, browser sends new request to specified URL, so old request parameters and attributes will not be available to destination resource.
  • Browser is completely unaware of servlet forward and hence the URL in browser address bar will remain unchanged. Where as the URL in browser address bar will change to new URL when servlet redirect is used.

No comments:

Post a Comment