• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Request Dispatcher

 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are exactly same
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic