Friday, December 5, 2014

Servlet Redirecting and Dispatching

Response Redirect

When we need to move from one servlet to another, or from a servlet to a page, it's time to send the user somewhere else. The redirect is part of the servlet HTTP response object.


Create a "StartServlet"

 package com.swtk.web.servlets;  

 import java.io.IOException;  
 import javax.servlet.ServletException;  
 import javax.servlet.annotation.WebServlet;  
 import javax.servlet.http.HttpServlet;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  

 @WebServlet("/StartServlet")  

 public class StartServlet extends HttpServlet {  

      private static final long     serialVersionUID     = 1L;  

      public StartServlet() {  
           super();  
      }  

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
           response.sendRedirect("myservlets/Results.do");  
      }  

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}  
 }  



Create a "ResultsServlet"

 package com.swtk.web.servlets;  

 import java.io.IOException;  
 import java.io.PrintWriter;  
 import javax.servlet.ServletException;  
 import javax.servlet.annotation.WebServlet;  
 import javax.servlet.http.HttpServlet;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  

 @WebServlet(
    description = "Simple Results Servlet", 
    urlPatterns = { 
       "/ResultsServlet", 
       "/myservlets/Results.do" 
    }
 )  

 public class ResultsServlet extends HttpServlet {  

      private static final long     serialVersionUID     = 1L;  

      public ResultsServlet() {  
           super();  
      }  

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
           response.setContentType("text/html");  
           PrintWriter out = response.getWriter();  
           out.println("Redirected!");  
           out.close();  
      }  

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}  
 }  


Start on
http://localhost:8080/HelloWorldServlet/StartServlet
and automatically redirect to




The Request Dispatcher

Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or given by a particular name.


 package com.swtk.web.servlets;  

 import java.io.IOException;  
 import javax.servlet.RequestDispatcher;  
 import javax.servlet.ServletException;  
 import javax.servlet.annotation.WebServlet;  
 import javax.servlet.http.HttpServlet;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  

 @WebServlet("/ServletDispatcher")  

 public class ServletDispatcher extends HttpServlet {  

      private static final long     serialVersionUID     = 1L;  

      public ServletDispatcher() {  
           super();  
      }  

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
           RequestDispatcher dispatcher = request.getRequestDispatcher("myservlets/Results.do");  
           dispatcher.forward(request, response);  
      }  

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}  
 }  



Differences and Best Practices


When should I use
response.sendRedirect("")
vs
dispatcher.forward(request, response)


RequestDispatcher is server side forwarding:

  1. The client sends a GET request
  2. The servlet forwards the request to another servlet
  3. The second servlet processes the request and returns a response ...



SendRedirect is client side redirection:

  1. The client sends a GET request. 
  2. The server returns a redirect.  The response is empty and contains a new URL
  3. The client automatically send a new GET request (with the new URL from the redirect).
  4. The server returns a response ...




Using Fiddler


Fiddler helps you debug web applications by capturing network traffic between the Internet and test computers. Among other capabilities, the tool enables you to inspect incoming and outgoing data to monitor requests and responses.

If I open the Fiddler tool, and run the servlet with RequestDispatcher, Fiddler will show me this:


If I run the Servlet with the SendRedirect, Fiddler will show me this:

Notice the two GET statements in the SendRedirect.  The output from Fiddler corresponds to our sequence diagrams above.


References


  1. Forwarding
    1. http://www.jguru.com/faq/view.jsp?EID=206736
      1. suppose you want Servlet_A to invoke Servlet_B ...
  2. Forward vs Redirect
    1. http://www.javapractices.com/topic/TopicAction.do?Id=181
      1. It's important to understand the difference between these two cases, in particular with respect to browser reloads of web pages.
    2. http://javarevisited.blogspot.in/2011/09/sendredirect-forward-jsp-servlet.html
      1. This is a classic interview question ..
    1. http://stackoverflow.com/questions/2047122/requestdispatcher-interface-vs-sendredirect

No comments:

Post a Comment