Servlet RequestDispatcher forward vs include

In this post, we will learn the difference between forward and include methods available in RequestDispatcher.

In servlet, we can use the RequestDispatcher to dispatch the request from one servlet to another. And it is very important to know to implement it correctly.

Hence, we will be looking at a simple example to demonstrate the uses of these methods.

Use case

We will define a simple use case. We will have the following files:

  1. login.html
  2. LoginServlet.java
  3. SuccessServlet.java
  4. web.xml

When a user sends the username and password values from login.html file it goes to LoginServlet and without doing any validation it sends the request directly to SuccessServlet. While sending requests from LoginServlet to SuccessServlet we will see the different outputs based on the code we’ve written below.

login.html

<html>
    <body>
        <form action="LoginServlet" method="post">
            Username: <input type="text" name="username"/></br></br>
            Password: <input type="password" name="password"/></br>
            <input type="submit" value="Login"/></br>
        </form>
    </body>
</html>

LoginServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginController extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String un = request.getParameter("username");
        String pw = request.getParameter("password");
        out.print("Printing from LoginServlet<p/>");
        RequestDispatcher rd = request.getRequestDispatcher("./SuccessServlet");
        rd.include(request, response);
    }

}

SuccessServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SuccessServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String un = request.getParameter("username");
        out.print("<h1>Welcome: " + un + "</h1>");
    }

}

web.xml

<web-app>
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>LoginServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>SuccessServlet</servlet-name>
        <servlet-class>SuccessServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>SuccessServlet</servlet-name>
        <url-pattern>/SuccessServlet</url-pattern>
    </servlet-mapping>
</web-app>

In the LoginServlet.java above, we have called the rd.include(request, response) method and sending a request to SuccessServlet we will see the output Printing from LoginServlet.

Output using include

Servlet RequestDispatcher include method
Servlet RequestDispatcher forward vs include 3

Now, change the method in LoginServlet from rd.include(request, response) to rd.forward(request, response) the value Printing from LoginServlet will not be printed like below:

Servlet RequestDispatcher forward method
Servlet RequestDispatcher forward vs include 4

Conclusion

In this post, we learned when we have to send a request to another servlet along with the content of the current servlet then use the include method otherwise only forward.


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments