• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forward, include and sendRequest

 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain the difference between the forward, include and sendRequest methods.
 
vinod bonde
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forward does move the control to the url specified
It happen on the server only

sendredirect :- The server sends an http error message to the browser saying this resource has been moved to some other location and then the browser is redirected to the specified url

include :
- static include
contents from the given url and copied to the jsp before comilation
- dynamic include
output of the specified url are placed where the include is mentioned
 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What sort of redirect would change the page but not the URL?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The requestDispatcher.forward method takes place on the server and would therefore, not change the URL in the browser's window.
 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a method that works that way but only requires a single string? Like .forward("foo.jsp?bar=baz");
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, but you could certainly write one.
I usually do and put it in either my Servlet superclass or action superclass.
 
sureshreddy puli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As part of Servlet Api RequestDispatcher interface is provided RequestDispatcher object is responcible for dispatching(hand over) the request to a resource.
As part of RequestDispatcher there are two methods.
1.include(request,response)
2.forward(request,response)
include:-->Nothing but executing multiple servlets with in same scope.That is ,suppose you write your code like following...

public class SrvOne{ | public class SrvTwo{
.....service(req1,res1){ ____req1,res1______
............ / | ..service(req1,res1){
............ / | ..................
here your include(req1,res1) | ..................
............ <----------------------}//returns req1,res1 to SrvOne
............ (req1,res1) |
|
}//end of service(req1,res1) |
//-->req1,res1 killed |
} | }
Here two servlet's service()s are executing at a time under same scope.i.e under same request,response objects.Finally server sends two servlet's out put to client.

Note:-->Servlet chaining is achieved through include().

forward:-->Whenever you are forwarding your page to another page nothing but calling the forwarded page's service() by passing same request and response objects.Here . When forward is executed the o/p generated by the first servlet is discarded.The second is sent to the browser.

redirect:-->When sendRedirect() executes the web container generates Header which is not visible to us. After receiving this info the browser again send request to the resource.i.e the old request and response objects are kiled and new request and response created.I.e making another request.
 
sureshreddy puli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class SrvOne{

..service(req1,res1){
..................
here your include(req1,res1)- -|
............ <--------------|--|
| |
}//end of service(req1,res1) | |
//-->req1,res1 killed here | |
} | |
| |
| |
| |(req1,res1)
public class SrvTwo{ (req1,res1) | |
/\<---------------| |
/ \ |
.....service(req1,res1){ |
................ |
................ |
|
}//end of SrvTwo service()------)
}
 
sureshreddy puli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i am trying to explain this bu using diagrams. But i couldn't.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sureshreddy,

If you put your diagrams inside code brackets, it will maintain the formatting.
 
sureshreddy puli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[public class SrvOne{

..service(req1,res1){
..................
here your include(req1,res1)- -|
............ <--------------|--|
| |
}//end of service(req1,res1) | |
//-->req1,res1 killed here | |
} | |
| |
| |
| |(req1,res1)
public class SrvTwo{ (req1,res1) | |
/\<---------------| |
/ \ |
.....service(req1,res1){ |
................ |
................ |
|
}//end of SrvTwo service()------)
}]
 
sureshreddy puli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me "what is code brackets"
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The UBB code buttons at the bottom of the screen. hit the CODE buttonand the brackets will be generated automatically. Alternatively, just type the brackets below:
[ October 06, 2005: Message edited by: Paul Bourdeaux ]
 
sureshreddy puli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
sureshreddy puli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hope you get my points
 
Eddy Lee Sin Ti
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forward and Include enables server-side navigation and composition of response.

You use Forward method when you have a control flow on server such as page1 doing some data processing and then forward the control to page2 to display it.

You use Include method when you wanna compose a response by combining a few subresponses. For example: You have a page1 which need contents from page2 and page3. When you called page2 from page1, the control will temporary transfer to page2 and executed it and the response and "included" into page1. After than the execution continues in page1.

The forward and include and transparent to client, in the sense that thr browser still displaying the same url in it's address bar.

For sendRedirect, the server will set the http header (http code 301 - permanent redirect, 307 - temporary redirect, usually depends on container) and the redirect will reflect on client browser.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic