Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Request Dispatcher

 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jaya lakshmi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are exactly same
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic