Two Laptop Bag*
The moose likes Servlets and the fly likes forward, include and sendRequest Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "forward, include and sendRequest" Watch "forward, include and sendRequest" New topic
Author

forward, include and sendRequest

Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Please explain the difference between the forward, include and sendRequest methods.


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
vinod bonde
Greenhorn

Joined: Oct 03, 2005
Posts: 5
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


Bond with the Rest
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
What sort of redirect would change the page but not the URL?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The requestDispatcher.forward method takes place on the server and would therefore, not change the URL in the browser's window.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Is there a method that works that way but only requires a single string? Like .forward("foo.jsp?bar=baz");
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

No, but you could certainly write one.
I usually do and put it in either my Servlet superclass or action superclass.
sureshreddy puli
Greenhorn

Joined: Sep 28, 2005
Posts: 29
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

Joined: Sep 28, 2005
Posts: 29
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

Joined: Sep 28, 2005
Posts: 29
Hi i am trying to explain this bu using diagrams. But i couldn't.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
sureshreddy,

If you put your diagrams inside code brackets, it will maintain the formatting.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
sureshreddy puli
Greenhorn

Joined: Sep 28, 2005
Posts: 29
[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

Joined: Sep 28, 2005
Posts: 29
Please tell me "what is code brackets"
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
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

Joined: Sep 28, 2005
Posts: 29
sureshreddy puli
Greenhorn

Joined: Sep 28, 2005
Posts: 29
hope you get my points
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135
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.


SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="http://eddyleesinti.blogspot.com" target="_blank" rel="nofollow">http://eddyleesinti.blogspot.com</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forward, include and sendRequest
 
Similar Threads
SERVLET
forward and include attributes??
AJAX and Java problem
forwarded to jsp displays content in previous jsp
Request Dispatcher.