java2db

RequestDispatcher forward method example (servlet chaining)



RequestDispatcher forward method is useful to forward request from a servlet to another servlet/Jsp/Html/File.

 

RequestDispatcher forward method example The output of the requesting/calling servlet will be discarded and the output of the requested/called servlet goes to the browser window as a response.

 

RequestDispatcher forward method example Statements placed before and after reqDispatchert.forward(–) will be executed but their output will be discarded.

 

RequestDispatcher forward method example Forwaed should be called before the response has been committed to the client.It throws an IllegalStateException , if the response already has been committed.

 

RequestDispatcher forward 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 forward method example Use request attributes to pass additional data from source servlet (First servlet) to destination servlet.

 

RequestDispatcher forward method example Both the source and destination servlets that may reside in the same or different web application of same server.

 

RequestDispatcher forward method example This method is good to configure error servlet.
RequestDispatcher forward method example Syntax :
<RequestDispatcherObj>.forward(<ServletRequestObject> , <ServletResponseObject>)

 

RequestDispatcher forward method example  Directory Structure :

 

RequestDispatcher forward method example Example for RequestDispatcher forward method:

RequestDispatcher forward method example index.jsp

RequestDispatcher forward method example FirstServlet.java

RequestDispatcher forward method example SecondServlet.java

RequestDispatcher forward method example web.xml

RequestDispatcher forward method example Execution Result :

Enter a name and click the send button.

The output comes from the SecondServlet.java , it discarded the FirstServlet.java output.




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]



^ <