Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Forward and Include method in Servlet

 
sharma anurag
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all my JavaRanch Friends this is Anurag ...

I want to know how Forward and Include method in RequestDispatcher work??
and please send me the code also if it is possible for you.

Thanks in advance.!!!
[ September 11, 2007: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64606
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sharma anurag:
and please send me the code also if it is possible for you.


That's not quite how things work here on the Ranch. You'll get all the help you need to write your own code. What specific questions do you have about dispatching? Are you having problems with some existing code?
 
srinivas.b
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sharma, its very simple...

see forward -- request goes to another resource from current position ,you cant print anything after this statement.if you do so it throws an exception

where as include : you are processing another resource with in your page .. you can print anything after this statement.no problems

i think you understand clearly...
 
S Venkatesh
Ranch Hand
Posts: 464
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) forward: When you use a forward, you can forward your request to any other resource that is managed by your web container. This means that you can forward your request to any other servlet or a JSP and continue to process the request. All the attributes present in that request is available for you for you to continue processing. And once the request is dispatched, it is committed. You cannot continue processing further.

....
RequestDispatcher view = request.getRequestDispatcher("\Servlet1");
view.forward(request,response);
// Don't do anything here. Its committed as you have forwarded to another resource.


2) include: Its like taking the help of another container managed resource to do some processing. You send your request to the other resource, perform the requested operation in another resource and get back to the same resource and continue processing further.

....
RequestDispatcher view = request.getRequestDispatcher("\Servlet1");
view.include(request,response);
// you can continue processing the request from here
.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic