Jsp and Servlet Tutorials - Difference between RequestDispatcher and SendRedirect

Welcome to fourth part of jsp-servlet tutorial series, so far we have discussed Request-Response model of Servlet, Servlet Architecture and Difference between doGet() and doPost(). In this particular blog we will cover difference between RequestDispatcher and SendRedirect that we will cover Difference between ServletContext and ServletConfig.


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.

Now the question arises that :

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.

Difference between ServletContext and ServletConfig

Earlier we have discussed that in init() method of servlet, servlet got the access of two object servletContext and servletConfig, now lets discuss these two in more details. To better understand these two objects and difference between them lets take a look at the following picture
All the servlets in a web application have access to a single Servlet Context object, whereas each of the servlet have its own servlet config object.

Servlet Context

Servlet context is basically a java object that resides there on application level, there is always a single servlet context for a application. To get values of init parameters on application level we uses getServletContext(). In web.xml it can be specified under <web-app> tags and not under <servlet> tags.

Servlet Config

There can be a number of servlet config objects in an application, its specified as one saperate object for each servlet and jsp. To get init parameters of perticular servlet we uses getServletConfig(). In web.xml it can be specified under <servlet> tags and not under <web-app> tags.

So far we have discussed request, response and their implementations, now its time to get into something about parameters and attributes. Attributes and parameters are something in which we can store the data and retrieve that data when required. Now the question arises what is the difference between attributes and parameters ?.

The return type of Attributes is Object and return type for parameters is a String.

In a high level abstraction we have three three types of attributes and two types of parameters in a web application.

Types of attributes:

1 - Context or Application : Application level attributes or so called application variable can be accessed throughout the whole application. 
2 - Request : The request variable can be accessed within a request scope. Its done by setAttribute(String,object) and retrived using getAttribute(String). It takes an key in form of a string and any type of java object can be stored and retrieved associated to that key.
3 - Session : A session variable or session attribute can be accesed and use in the application within a session or we can say until the session is live.

Parameters : Parameters are of two types, Context parameters and Servlet Init parameters, the return type for parameters is always a string. Here one thing to be noted is that we can access init parameters but we can not set a value to these init parameters in our servlet code, The value to init parameters can be given in web.xml only, init parameters can be accessed using getInitParameter() method.

In this particular blog we went through Difference between RequestDispatcher and SendRedirect and what is difference between ServletContext and ServletConfig than we get to know something about parameters and attributes in servlet and their scope and usage. In our next and blog of jsp-servlet tutorials series we will discuss other useful feature and implementation of Servlet and Jsp's.

About The Author

Nagesh Chauhan

Nagesh Chauhan has 8+ years of software design and development experience in variety of technologies like - Core Java, Java 8 (Streams, Lambda), J2EE (Servlet, JSP), Spring Framework (MVC, IOC, JDBC, SECURITY etc), Spring Boot and Microservices, Kafla, Redis, Cassandra and Spark.