Servlet Collaboration In Java Using RequestDispatcher and HttpServletResponse
What is Servlet Collaboration?
The exchange of information among servlets of a particular Java web application is known as Servlet Collaboration. This enables passing/sharing information from one servlet to the other through method invocations.
What are the principle ways provided by Java to achieve Servlet Collaboration?
The servlet api provides two interfaces namely:
These two interfaces include the methods responsible for achieving the objective of sharing information between servlets.
Using RequestDispatcher Interface
The RequestDispatcher interface provides the option of dispatching the client’s request to another web resource, which could be an HTML page, another servlet, JSP etc. It provides the following two methods:
- public void forward(ServletRequest request, ServletResponse response)throws ServletException, java.io.IOException:
The forward() method is used to transfer the client request to another resource (HTML file, servlet, jsp etc). When this method is called, the control is transferred to the next resource called. On the other hand, the include() method is used to include the content of the calling file into the called file. After calling this method, the control remains with the calling resource, but the processed output is included into the called resource.
The following diagram explains the way it works:
- public void include(ServletRequest request, ServletResponse response)throws ServletException, java.io.IOException:
The include() method is used to include the contents of the calling resource into the called one. When this method is called, the control still remains with the calling resource. It simply includes the processed output of the calling resource into the called one.
The following diagram explains how it works:
- Example of using RequestDispatcher for Servlet Collaboration
The following example explains how to use RequestDispatcher interface to achieve Servlet Collaboration:
- If password matches:
- If password doesn’t match:
Using HttpServletResponse Interface
- The HttpServletResponse interface is entrusted with managing Http responses. To achieve servlet collaboration, it uses the following method:
public void sendRedirect(String URL)throws IOException;
- This method is used redirect response to another resource, which may be a servlet, jsp or an html file. The argument accepted by it, is a URL which can be both, absolute and relative. It works on the client side and uses the browser’s URL bar to make a request.
Example of using sendRedirect() for redirection
- The following example of a web application created using servlet takes the text written in the text field in the webpage, and directs it to the servlet. The servlet then redirects it to google, which then produces search results based on the text written.
- Search result
What is the difference between forward() method of RequestDiispatcher and sendRedirect() of HttpServletResponse?
- Although the two methods appear to do the same thing, there are still differences between the two, which are as follows:
|It works on the server side||It works on the client side|
|It sends the same request and response objects to another resource.||It always send a new request|
|It works only within the server.||It can be used within and outside the server.|