• 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

whats the difference between forward and redirect?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
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...
:roll:
[ November 16, 2007: Message edited by: Bear Bibeault ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 74
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in redirect, lost original request

in forward ,keep original request.

just so so
 
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
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:
http://faq.javaranch.com/java/RedirectForward
[ November 17, 2007: Message edited by: Ben Souther ]
 
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic