File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes whats the difference  between forward and redirect? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "whats the difference  between forward and redirect?" Watch "whats the difference  between forward and redirect?" New topic

whats the difference between forward and redirect?

kamal kannan
Ranch Hand

Joined: Oct 06, 2006
Posts: 33
whats the difference between
1) javax.servlet.RequestDispatcher.void forward(ServletRequest request,ServletResponse response);
2)javax.servlet.HttpServletRequest.void sendRedirect(java.lang.String location); methods please explain these methods usage...
[ November 16, 2007: Message edited by: Bear Bibeault ]

SCJP 1.5 95% /SCWCD 1.5 93% /RHCE EL4 100% /WEBLOGIC 9.2 SYSTEM ADMINISTRATION 100% / Oracle Weblogic 10g Server Certified System Export 88%
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

The first performs a forward, which transfers control of the request to another resource. The second initiates a redirect, in which the browser is instructed to make a new request.

[Asking smart questions] [About Bear] [Books by Bear]
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

Forward happens on serverside, server forwards the same request to another resource. whereas redirect happens on the browser side, server sends http status code 302 to browser so browser makes new request.
Redirect requires one more round trip from browser to server.

In the case of forward the same request is forwarded to another resource so all the request parameters and request attributes will be available. in second case its a new request so old parameters and attributes will not be available.

One more difference is redirect reflects in browser address bar forward doesnt.

[Servlet tutorial] [Servlet 3.0 Cook Book]
Pranav Pal
Ranch Hand

Joined: Nov 04, 2007
Posts: 74
In redirect , Server tells the Client : I can't serve you at this point of time, you go and tell someone else to serve you, and here is the address of that someone else.

In forward , Server tells the Client : I will serve you, but I will take over the request to one of my co-workers, and th co-worker will end up serving you

Hakuna Matata!
wang jiajian

Joined: Sep 20, 2007
Posts: 9
in redirect, lost original request

in forward ,keep original request.

just so so
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

This is a common question that takes some effort to answer completely so I've started a FAQ entry for it.
You can read it here:
[ November 17, 2007: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sureshdv Babu

Joined: Sep 23, 2011
Posts: 2
Before explaining the difference between include,forward and sendRedirect a small information on the request and response objects which is created by servlet container.
Whenever there is request from the client (i.e is browser eg: Internet explorer,safari,netscape etc., ) to the server, the servlet container creates two objects
1. request 2. response these objects are created by servlet container.
Now coming to our differences:-
all the three are used for inter servlet communication in different ways.

Forward :
Consider there are 2 servlets, servlet1 and servlet2, and now you are calling the servlet2 from servlet1, as we already discuss every servlet will be having request,response objects. servlet1 contains request,response objects and here the same objects will be forwarded to servlet2, with out knowledge of the
container request will be forwarded to servlet2, and in this case for 2 servlets only one request and repsone objects will be there.
Cautions : Forward need to be call before response is committed to the client otherwise you will end up with an exception.
Limitations : You can't forward the request outside of the context so the request should with in your site.
Advantages : performance wise it will be bit faster as second call is made with out knowledge of the container and there will not be a round trip to the client to server.

sendRedirect : Here also consider same example servlet1 and servlet2 and now if you want to call servlet2 from servlet1,
in this case the request and response of the servlet1 will be discarded and you will be redirected to the client, once again
the new request will be made from client for servlet2 and container will create new req,and resp objects for servlet2.
If you see here in this scenario, 2 request and 2 response objects are created by the container even though first request,response objects
are discarded container will create total 4 objects (2 req, 2resp) and there is a round trip to the server.
Advantages: You can access the servlet which is out side of the context also, so you can make a call to the servlet which is there in the other web site.
Limitations: There is a round trip performance wise bit slow and as there is no req,ans resp objects of servlet1 you can't get the values of ser1 and ser2.

Include : Include and forward are almost similar the only difference is, in include the servlet2 will be embedded in servlet1
and literally there will be only servlet will be there as it includes the the servlet2, where as in forward it will invoke the second
servlet dynamically so it won't embed the servlet2 in servlet1.
and one more major diff is as the include will embed the contents into servlet2 no need to take care of response committed to client.

I know the post bit lengthy the reason is i tried to explain everything clearly.
I agree. Here's the link:
subject: whats the difference between forward and redirect?
It's not a secret anymore!