What is the difference between Sendredirect and Request Dispatcher in java
Send Redirect | Request Dispatcher |
---|---|
sendRedirect() this method is declared in HttpServletResponse Interface | getRequestDispatcher() this method is declared in HttpServletRequest Interface. |
Using Send Redirect Client or user know where the request is coming because the request is displayed in URL | Using request dispatcher Client or User doesn't know which page is processed internally |
The request process is taken care by the web container and transfers the request using a browser, each request is processed as a new request. | The request process is taken care by web container, each request is processed without the client being informed. |
Using sendRedirect() it doesn't include any resources. | Using requestDispatcher() it includes the resources. |
The sendRedirect() method is slower because when a new request is created old request object is lost. | The forward() method is faster than sendRedirect() method. |
In the case of sendRedirect, if we want to use the same data for a new resource(JSP, Servlet) we have to store the data in session or pass data in URL. | In the case of requestDispatcher redirect using forward, and we want to use the same data in a new resource(JSP, Servlet), we can use request.setAttribute() as we have a request object available. |
sendRedirect() redirect the request to the server or outside the context Example : response.sendRedirect("www.google.com") |
getRequestDispatcher() redirect the request to another resource could be any servlet, JSP page. Example : request.getRequestDispatcher("Servlet or JSP").forward(request, response) |
Send Redirect is also called client-side redirect | Request Dispatcher is also called a server-side redirect. |
Which one is good sendRedirect or Request Dispatcher?
It depends upon your need for which method is more useful.
Difference Between Response Send Redirect & Request Dispatcher |
If you want to transfer the request to a new server or outside the context, and a request is treated as
completely new, then go for sendRedirect.
In the case of sendRedirect, passing data is not easy we have to store the data in session or pass along with the URL.
SendRedirect is not secure while sending data display in the URL.
Request Dispatcher:
If you want to forward or include the request or responses with in the same server, then go for Request Dispatcher.
When we redirect using forward, we can easily pass the data using request.setAttribute() as we have a request object available.
Post a Comment