RequestDispacher and sendRedirect in servlet by R4R Team

Response:

Now lets discuss something about response, in normal conditions a request comes in for a servlet and servlet sends back the respose to the client. But in some cases assume that if the servlet does not know how to serve the request, now either the servlet knows someone that is able to serve the request or the servlet does not have any information about the request at all.

Whenever a request came to the server and the server is not able to serve it, if the servlet knows someone else who can serve the request, than it redirects the request to that other servlet. If the servlet does not have any information regarding the request it get, a Error page is rendered.

When a servlet is not able to serve a request but it knows that some other server can serve the request it sends the request to that servlet and response is sent back to the client, this process can be done by two different approaches.

Send Redirect:

Lets assume that the end user(client) makes a request to some abc servlet, now abc servlet is not able to serve but it knows that an another servlet xyz can serve the request. The abc servlet tells the browser that i am not able to serve the request but xyz can, make a request to xyz to get response. Now browser sends a redirect to xyz servlet and xyz sends back the response to the client. However the end user does not know about this two step process it assumes the request is being served by abc servlet itself.

Request Dispatcher:

In case of request dispatcher when a client makes a request to abc servlet, abc forward the request to xyz servlet without telling the browser anything and the response in send back to client by xyz servlet. In request dispatcher the browser does not need to send a redirect to xyz again.

When to use a Request Dispatcher and when to use a Send Redirect :

We can use request dispatcher only when the other servlet to which the request is being forwarded lies in the same application. In the example explained above, if abc and xyz are not in the same application than we can not use request dispatcher. On the other hand Send Redirect can be used in both the cases if the two servlets resides in a same application or in different applications.

sendRedirect Method:

sendRedirect() method redirect the response to another resource. sendRedirect() method actually makes the client(browser) to create a new request to get to the resource. The client can see the new url in the browser.

sendRedirect() accept relative URL, so it can go for resources inside or outside the server.

sendRedirect and request dispatcher:

The main difference between a redirect and a request dispatch is that, redirect makes the client(browser) create a new request to get to the resource, the user can see the new URL while request dispatch get the resource in same request and URL does not changes.

sendRedirect works on response object while request dispatch work on request object.

Example

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

try {

response.sendRedirect("http://www.r4r.in");

}finally {

out.close();

}}}

Leave a Comment: