aspose file tools*
The moose likes Servlets and the fly likes Request Dispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Request Dispatcher" Watch "Request Dispatcher" New topic
Author

Request Dispatcher

Saurabh Agrawal
Ranch Hand

Joined: Oct 07, 2003
Posts: 244
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.
jaya lakshmi
Greenhorn

Joined: Mar 04, 2005
Posts: 10
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);

reqdispatcher.forward(request.response);

In the same way you can use include() to include the values from another url to the current application.

Hope this will help you.

jaya
Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
Hi Saurabh

Here is the explaination of RequestDispatcher

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,

Regards
Rohit
Saurabh Agrawal
Ranch Hand

Joined: Oct 07, 2003
Posts: 244
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.

Thanks anyways,
Saurabh
Rohit Bhagwat
Ranch Hand

Joined: Dec 19, 2004
Posts: 205
They are exactly same
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Request Dispatcher