Hi folks, Can you please tell me how is Request dispatcher used in servlets ? I mean what is its use and how is it used. I would be thankful if someone can explain me its usage and also whats the difference in Include then.
Hope to get good replies on this. Saurabh
Success is not doing extraordinary things but doing ordinary things extraordinarily well.
RequestDispatcher is an interface. 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.
It receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server.
It has two methods forward() and include().
eg from a servlet which do search string. You wabt to display the result in another Html or jsp file
write the code like
RequestDispatcher reqdispatcher = request.getRequestDispatcher(URL of the file);
In the same way you can use include() to include the values from another url to the current application.
Basically if you want communication between 2 or more resource at server side then you use RequestDispatcher.
include is equivalent to jsp:include forward is equivalent to jsp:forward
include will include the output of the resource from the calling point. The control is temporarily sent to the included resource
forward will forward the control to the resource specified. The control is permamnetly forwarded.
You cannot write after forward otherwise it will result in IllegalStateException.forward is just a method so the control does return back to calling point but after forward everyuthing is ignored. You can write after include.
I hope I covered all points,
Joined: Oct 07, 2003
Hello Rohit and Jaya, Thanks for your detailed explanation on this point and i got the things clearly now. So it means in case of include the control gets back at the calling point which is not the case with forward.
Are these same as jsp:include and jsp:forward. I mean dont they have any differences or its just that in case of servlets we have methods and in case of JSPs we have tags.