• 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

forward, include and sendRequest

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain the difference between the forward, include and sendRequest methods.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What sort of redirect would change the page but not the URL?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i am trying to explain this bu using diagrams. But i couldn't.
 
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell me "what is code brackets"
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
sureshreddy puli
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hope you get my points
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic