Saturday, October 16, 2010

RequestDispatcher Vs sendRedirect

RequestDispatcher Vs sendRedirect
==================================
RequestDispatcher
-------------------------
RequestDispatcher is used to dispatch the request URL to execute another servlet in servlet chaining. In this process first servlet directly dispatch the request to execute another servlet by dispatching URL. request.include() or request.forwared() methods are used to dispatch the request for another servlet execution. In this process, client or browser not able to aware for this request dispatching. All servlets are in part of servlet chaining are executed in the same server by request dispatching. In this process we cannot communicate with multiple servers. As servlet is dispatching the request directly means without involving client this servlet chaining process is faster than sendRedirec.
RequestDispatcher rd=new RequestDispatcher();
rd.forword(/servletTwo.jsp);
or
RequestDispatcher rd=new RequestDispatcher();
rd.include(/servletTwo.jsp);

Advantage:
i. Dispatch the request URL to execute another servlet for the single request by the user.
ii. All servlets are executing in the same server so this process.
iii. As client is not involved to dispatching the request for another servlet, this servlet chaining process is faster than rendRedirect.

Limitation:
In this process we cannot send the request for another servlet that is executed in the other server means there is the limitation to communicate with multiple servers.



sendRedirect
-------------------
response.sendRedirect() is used to send the request to the execute another servlet in servlet chaining. First servlet send the response along with the send redirect URL to the client and client again send that request to the server back to execute another servlet in servlet chaining. So client is award for this process. As request redirect by the browser this servlet chaining process is slower than RequestDispatcher process. In this case, multiple servlets are executed with the different request send by the client or browser. request.getAttributes() and request.setAttributes() are not available for the second request. To pass the value or object to the second servlet by passing the query string with the request.

HttpServletResponse response;
response.setRedirect(/servletTwo.jap);

Advantage:
i. response.sendRedirect() the URL by client or browser to execute another servlet that may be executed in the same server or any other server. So, we can communicate with the multiple servlet.
ii. Different request URL sending to execute different servlet by the client or browser.

Limitation:
sendRedirect() is involved the client to redirect the request URL to execute another servlet, means first servlet send the response to the client and client again redirect the URL to the container to execute the servlet; takes more time to redirecting the request URL for the servlet chaining.



No comments:

Post a Comment