Searching...
Saturday, 13 February 2016

Servlet RequestDispatcher

15:22
The javax.servlet.RequestDispatcher interface class help your servlet to call another servlet, JSP file, or HTML file from inside another servlet.

There are two main method in this interfaces


1) forward(ServletRequest request, ServletResponse response)

                  —>> forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server

2) include(ServletRequest request, ServletResponse response)
                  —>> includes the content of a resource (servlet, JSP page, HTML file) in the response


Example

Servlet class

This servlet class will do the include or forward based on the action from the user interface
package com.vinod;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MyServletDispatcher
 */

public class MyServletDispatcher extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */

    public MyServletDispatcher() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
        if (request.getParameter("action") != null) {
            String action = request.getParameter("action");
            if (action.equalsIgnoreCase("include")) {
                rd.include(request, response);
            } else if (action.equalsIgnoreCase("forward")) {
                rd.forward(request, response);
            }
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}


Test jsp (Dispatcher.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form name="input" action="http://localhost:8081/MyServletDispatcher"
        method="get">
        Enter Action: <input type="text" name="action"><input
            type="submit" value="Submit">
    </form>
</body>
</html>

index.JSP
 
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

Deploy and run the Dispatcher.jsp ( In this example using jetty server, so used jetty:run maven command to start the jetty server)
 


Download complete Example

https://github.com/kkvinodkumaran/myj2ee

0 comments: