sendRedirect vs RequestDispatcher.foreward():

Many people have doubt in this particular question. Take one simple example

Suppose you are a teacher. You have a student. The student asks you one question but you do not know the answer. But you know one person(X) who knows the answer.Then you have two choices now either you go to the ‘X’ and tell him to solve the question of your student or you send your student to ‘X’ to get the answer.

The RequestDispatcher.foreward() is the same as first case. And sendRedireect() same as second case.

Now see the difference:

1) sendRedirect() makes the client to do work but foreward() makes something else on the server to do work.

2) In case of sendRedirect() a new request and response is generated but in case of foreward() same request,response is used.

3) The URL in the browser is changed to the new address in case of sendRedirect() but in case of foreward() location bar remains same.

4) Foreward() can be used with HTTP and non-HTTP clients where as sendRedirect() can be used for only HTTP clients.

No comments:

Post a Comment