Servlet Request Dispatching
Delegating the request from one servlet to another or transfer of excution from one servlet to another is known as request dispatching in servlet.
In another ways we can define it as interservlet communication,which means one servlet calling another servlet.This concept can be used in cases where some piece of code is needed to be written in different servlets.Thus, in such cases we can write that code in a single servlet and keep on calling that code from different servlets.
There are three ways in which request delegation can be done. These ways are as follows :
1.Include
2.Forward
3.Redirect
1. Include : This method suggests that the response from the another servlet be included in the response of the first servlet. This is equivalent to copy pasting the response of one servlet into another.In case of inclusion,the response which the client receives is the append of both including and the included servlets.However,for the client it appears that the including servlet only has responded.Following line of code is used to include a servlet response into another servlet :
RequestDispatcher rd = request.getRequestDispatcher("url-pattern of the servlet"); rd.include(request,response);
Here RequestDispatcher object is needed .We can see that request and response objects of the first servlet are passed to the servlet which we wish to include through include method so that request remains the same and the included servlet can write to the response of the including servlet and it appears that including servlet itself has processed the request.
2. Forward : It works same as include except for the difference that the reponse from the first servlet is flushed out and the reponse from the second servlet only is visible.Example code is :
RequestDispatcher rd = request.getRequestDispatcher("url-pattern of the servlet"); rd.forward(request,response);
Here also it appears to the client that the first servlet only has processed the request.
3. Redirect : In the case of redirect the first servlet issues a new request to a another servlet to which redirection has happened.In this case client sees a new request in the browser url bar and can realize that the response comes from the second servlet.Example code is :
response.sendRedirect("url-pattern of the servlet");
Here we can see that response object is used.This done for the reason that the issuing of the new request at the client side,in the browser url bar has to be done by the first servlet.
Number of View :4205
Related posts:
- How Container Does The Cookie Work in J2EE Define Cookie Cookies Explained Session In Servlet What Is Cookie How Apache Tomcat Handles Cookies Cookies Explained How Cookies Work Cookie Guide
- how to include one servlet into another Servlet Essentials Servlet Include
- how to forward from one servlet to another Servlet Essentials Servlet Forward
- How To Redirect From One Servlet To Another Servlet Basics Servlet Redirect
- Difference Between Parameters And Attributes In Servlets/JSP Parameter vs. Attribute
Tags: forward, include, j2ee, java, redirect, request, request dispatcher, response, servlet
This entry was posted on Friday, May 11th, 2012 at 3:11 am and is filed under Java, OPPS, software, Tech-Tips, Tips & Tricks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.