Request dispatcher in Servlet is one of the important topics here we explained it using the User login example.
In Java Servlets, a RequestDispatcher
is an interface provided by the Servlet API that allows you to forward or include a request or response from one servlet to another servlet, JSP (JavaServer Pages), or any other resource within the same web application. It is used for server-side redirection and dispatching requests.
There are two primary methods provided by the RequestDispatcher
interface:
forward(ServletRequest request, ServletResponse response)
: This method forwards the request and response objects from one servlet to another. Once the forward is performed, the control is transferred to the destination servlet or resource, and the response generated by the destination servlet is sent back to the client. This means that the client is unaware of the original request being processed by another servlet.
1 2 | RequestDispatcher dispatcher = request.getRequestDispatcher("/destinationServlet"); dispatcher.forward(request, response); |
include(ServletRequest request, ServletResponse response)
: This method includes the response of another servlet or resource within the current servlet’s response. It doesn’t transfer control to the destination servlet entirely; instead, it includes the output of the destination servlet in the current servlet’s response. This is often used when you want to include common headers, footers, or other content in multiple servlets or JSPs.
1 2 | RequestDispatcher dispatcher = request.getRequestDispatcher("/commonHeaderFooter.jsp"); dispatcher.include(request, response); |
The RequestDispatcher
is useful for creating modular and reusable components in web applications. It enables the composition of web pages by combining the output of multiple servlets or resources, allowing developers to build complex and dynamic web applications.
For the login example, we are using the user’s mail id and password.
if both are correct then we are forwarding to home.jsp page if any of the mail id or password is correct then it will be forwarded back to the index.jsp page with the appropriate error message.
Project Explorer
Following is the project explorer for the user Login example.
Pages are stored in the Web Pages folder.
The servlet is located inside the source package.
Login Page -index.jsp
This is login page contains two boxes for user-id and password.
This page also shows error message if user id and/or password are incorrect.
A scriptlet is used to show the error message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <%@page contentType="text/html" pageEncoding="UTF-8"%> <!Doctype html> <html> <head> <title>Login Page</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <h1>Login Page</h1> <div style="color: red;"> <% String message = (String) request.getAttribute("error"); if (message != null) { out.println(message); } %> </div> <form action="login" method="post"> <p> Mail Id : <input name="mailId" type="text" placeholder="Enter your mail id" /> </p> <p> Password : <input name="password" type="password" placeholder="Enter your password" /> </p> <p> <input type="submit" value="Login" /> </p> </form> </body> </html> |
Login Check – Login.java
Servlet call on /login
URL and doPost()
is get called.
Here mailId and password request parameters are extracted from the request URL and stored in mailId and password variable.
Both values are checked is pre-defined values if values are matched then it is redirected to home.jsp.
else redirected to index.jsp page.
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(“/home.jsp”);
dispatcher.forward(request, response);
here RequestDispatcher
is used to forward requests and responses to specified pages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package ebhor.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name = "Login", urlPatterns = {"/login"}) public class Login extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); String mailId = request.getParameter("mailId"); String password = request.getParameter("password"); System.out.println("Mail id "+mailId+" password "+password); System.out.println("Successfully Login"); RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/home.jsp"); dispatcher.forward(request, response); } else { System.out.println("Mail id or password is incorrect"); request.setAttribute("error", "Please enter correct username and/or password"); RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp"); dispatcher.forward(request, response); } } } |
User home page -home.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <%@page contentType="text/html" pageEncoding="UTF-8"%> <!Doctype html> <html> <head> <title>Home Page</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <h1>Welcome User</h1> <h3> Your mail id is <%=request.getParameter("mailId")%></h3> <h3> Your password is <%=request.getParameter("password")%></h3> </body> </html> |
Result
Login page
If the user id and/or password are incorrect then it will be redirected to the index.jsp page with an appropriate error message.
Login Failure
Using the correct email id and password [email protected] and 123456
Login Success

Here we discussed the Request dispatcher in Servlet User Login Example. Hope you learned it.
Read More
Sending data to servlet HTTP get method