Request dispatcher in Servlet User Login Example

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:

  1. 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. 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.

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.

Request dispatcher in Servlet User Login Example Project Explorer

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.

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.

User home page -home.jsp

Result

Login page

Request dispatcher in Servlet User Login Example 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

Request dispatcher in Servlet User Login Example Login Failure

Using the correct email id and password [email protected] and 123456

Login Success

Request dispatcher in Servlet User Login Example 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

How to get multiple parameters in ServletContext

Servlet Annotation WebServlet Example