java2db

RequestDispatcher include method example (servlet chaining)



RequestDispatcher include method is useful to combined the output content of the source, destination servlets and send it to browser window as a response.

 

RequestDispatcher include method example Statements placed before and after RequestDispatcherObject.include(–) will be executed and combined their outputs with the destination resource.

 

RequestDispatcher include method example Both source and destination servlets use the same request and response objects (Or sub classes of the ServletRequestWrapper or ServletResponseWrapper classes that wrap them) .So the request data is visible and accessable in both servlets.

 

RequestDispatcher include method example Use request attributes to pass additional data from source servlet (First servlet) to destination servlet.

 

RequestDispatcher include method example Both the source and destination servlets that may reside in the same or different web application of same server.
RequestDispatcher include method example Syntax :
<RequestDispatcherObj>.include(<ServletRequestObject> , <ServletResponseObject>);

 

RequestDispatcher include method example Directory structure :

 

RequestDispatcher include method exampleExample for RequestDispatcher include method :

RequestDispatcher include method example index.jsp

RequestDispatcher include method example FirstServlet.java

RequestDispatcher include method example SecondServlet.java

RequestDispatcher include method example web.xml

RequestDispatcher include method example Execution result :

Enter the name and click send button.

The output of the source and destination servlets will combined and send to the client as a response.

 




Related Posts :
Kill or invalidate session using session id ( concurrent login )
Servlet Filter ( Request and Response filters in servlets ) example
URL rewriting in servlets ( Servlet session tracking )
Servlet Cookies ( Servlets session tracking with cookie )
Hidden form field in servlets ( Servlet session tracking )
Request attributes in servlet with an example ( Attribute scope )
Session attributes in servlet with example (Attribute scope)
ServletConext Attributes in servlet ( Attribute scope )
   [will not be published]



^ <