×
☰ See All Chapters

RequestDispatcher and sendRedirect

Sometimes in the application we have to pass the request other resource based on certain condition. Or sometimes we have to include the response from other resource based on certain condition. For example, if user requests for home page and if the session has expired then we have to redirect him to login page. If any exception comes we have to redirect to servlet which handles the exception and so on. Using RequestDispatcher we can dispatch the request to another resource like JSP, servlet, HTML etc.… Also we can include the content of another resource. RequestDispatcher is an interface in javax.servlet package. There are two methods defined in the RequestDispatcher interface.

    1. public void forward(ServletRequest request, ServletResponse response):  Forwards a request from one resource (like servlet, JSP) to another resource (servlet, JSP file) on the server. 

    2. public void include(ServletRequest request,ServletResponse response): Includes one resource (like servlet, JSP) into another resource (servlet, JSP file) on the server. 

forward()

include()

Request will not come back to the calling resource.

Request will come back to the calling resource.

It should be last statement in servlet.

Can be called anywhere.

Can be forwarded to only one resource.

More resources can be included.

RequestDispatcher Example

Project directory structure

requestdispatcher-and-sendredirect-0
 

home.html

<!DOCTYPE html>

<html>

<body>

        <form action="../loginServlet" method="post">

                Name:<input type="text" name="userName" /><br /> <br /> Password:<input

                        type="password" name="userPass" /><br /> <input type="submit"

                        value="login" />

        </form>

</body>

</html>

 

LoginServlet.java

package com.java4coding;

 

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class LoginServlet extends HttpServlet {

 

        public void doPost(HttpServletRequest request, HttpServletResponse response)

                        throws ServletException, IOException {

 

                response.setContentType("text/html");

                PrintWriter out = response.getWriter();

 

                String name = request.getParameter("userName");

                String pass = request.getParameter("userPass");

 

                if (name.equalsIgnoreCase("admin") && pass.equalsIgnoreCase("admin")) {

                        RequestDispatcher rd = request.getRequestDispatcher("adminServlet");

                        rd.forward(request, response);

                } else if (name.equalsIgnoreCase("user") && pass.equalsIgnoreCase("user")){

                        RequestDispatcher rd = request.getRequestDispatcher("userServlet");

                        rd.forward(request, response);

                } else {

                        out.print("You entered invalid credentials.");

                        RequestDispatcher rd = request.getRequestDispatcher("html/home.html");

                        rd.include(request, response);

                }

        }

}

AdminServlet.java

package com.java4coding;

 

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class AdminServlet extends HttpServlet {

 

        public void doPost(HttpServletRequest request, HttpServletResponse response)

                        throws ServletException, IOException {

                response.setContentType("text/html");

                PrintWriter out = response.getWriter();

                out.print("Welcome To Admin");

        }

}

 

UserServlet.java

package com.java4coding;

 

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class UserServlet extends HttpServlet {

 

        public void doPost(HttpServletRequest request, HttpServletResponse response)

                        throws ServletException, IOException {

                response.setContentType("text/html");

                PrintWriter out = response.getWriter();

                out.print("Welcome To User");

        }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xmlns:web="https://xmlns.jcp.org/xml/ns/javaee"

        xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

        <servlet>

                <servlet-name>loginServlet</servlet-name>

                <servlet-class>com.java4coding.LoginServlet</servlet-class>

        </servlet>

        <servlet-mapping>

                <servlet-name>loginServlet</servlet-name>

                <url-pattern>/loginServlet</url-pattern>

        </servlet-mapping>

        <servlet>

                <servlet-name>adminServlet</servlet-name>

                <servlet-class>com.java4coding.AdminServlet</servlet-class>

        </servlet>

        <servlet-mapping>

                <servlet-name>adminServlet</servlet-name>

                <url-pattern>/adminServlet</url-pattern>

        </servlet-mapping>

        <servlet>

                <servlet-name>userServlet</servlet-name>

                <servlet-class>com.java4coding.UserServlet</servlet-class>

        </servlet>

        <servlet-mapping>

                <servlet-name>userServlet</servlet-name>

                <url-pattern>/userServlet</url-pattern>

        </servlet-mapping>

</web-app>

 

Output

requestdispatcher-and-sendredirect-1
 

sendRedirect() Example

 

public class SendRedirectServlet extends HttpServlet {

        public void doGet(HttpServletRequest req, HttpServletResponse res)

                        throws ServletException, IOException {

                res.sendRedirect("www.google.com");  

        }

}

 


All Chapters
Author