Servlet Tutorial

Servlet Chaining

Servlet Chaining:

If a client request is processed by group of servlets, then that servlets are known as servlet chaining or if the group of servlets process a single client request then those servlets are known as servlet chaining.

In order to process a client request by many number of servlets then we have two models, they are forward model and include model.

Forward model:

In this model when we forward a request to a group of servlets, finally we get the result of destination servlet as a response but not the result of intermediate servlets.

forward model

Include model:

If a single client request is passed to a servlet and that servlet makes use of other group of servlets to process a request by including the group of servlets into a single servlet.

include model

In the above diagram client request goes to servlet s1 and s1 internally includes s2, s3 and s4 servlets and finally result of all these servlets given to the client by a source servlet s1.

Note: One servlet can include any number of servlets where as one servlet can forward to only one servlet at a time.

Steps for DEVELOPING Servlet Chaining:

  1. Obtain an object of ServletContext by using in any of the following way:
  2. ServletContext ctx1=getServletContext (); [GenericServlet method] 
    ServletContext ctx2=config.getServletContext (); [ServletConfig method] 
    ServletContext ctx3=req.getServletContext (); [HttpServletRequest method]
    
  3. Obtain an object of RequestDispatcher. RequestDispatcher is an interface which is present in javax.servlet.* package and it is used for forwarding the request and response objects of source servlet to destination servlet or for including the destination servlet into source servlet. To obtain an object of RequestDispatcher, the ServletContext contains the following method:
  4. request dispatcher
    RequestDispatcher rd=ctx.getRequestDispatcher ("./s2");
  5. Use forward or include model to send the request and response objects. RequestDispatcher contains the following methods for forwarding or including the request and response objects.
  6. forward or include RequestDispatcher

    For example:

    rd.forward (req, res) throws ServletException, IOException 
    rd.include (req, res) throws ServletException, IOException
    

Write a java program which illustrates the concept of servlet chaining?

Answer:

web.xml:

<web-app>
    <servlet>
        <servlet-name>abc</servlet-name>
        <servlet-class>Serv1</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>pqr</servlet-name>
        <servlet-class>Serv2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>abc</servlet-name>
        <url-pattern>/s1</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>pqr</servlet-name>
        <url-pattern>/s2</url-pattern>
    </servlet-mapping>
</web-app>

Serv1.java:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Serv1 extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        pw.println("<h2>I AM FROM Serv1 BEGINNING</h2>");
        ServletContext ctx = getServletContext();
        RequestDispatcher rd = ctx.getRequestDispatcher("/s2");
        rd.include(req, res);
        pw.println("<h2>I AM FROM Serv1 ENDING</h2>");
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doGet(req, res);
    }
};

Serv2.java:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Serv2 extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        pw.println("<h2>I AM FROM Serv2</h2>");
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doGet(req, res);
    }
};

What is the difference between getRequestDispatcher (String) and getNamedRequestDispatcher (String)?

Answer:

getRequestDispatcher (String) method takes url-pattern or public-url of web.xml where as getNamedRequestDispatcher (String) method takes name of the servlet or deployer name of web.xml

Forwarding or Including request and response of one web-app to another web-app:

In order to achieve forwarding or including the request and response objects of one web application to another web application, we must ensure that both the web applications must run in the same servlet container.

  1. Obtain ServletContext object for the current web application.
  2. weblogic server

    For example:

    ServletContext cctx=getServletContext ();
  3. Obtain SerletContext object of another web application by using the following method which is present in ServletContext interface.
  4. servlet context another webapp

    For example:

    ServletContext octx=cctx.getContext ("./webapp2");
  5. Obtain RequestDispatcher object by using ServletContext object of another web application.
  6. servlet context another webapp of url pattern

    For example:

    RequestDispatcher rd=octx.getRequestDispatcher ("/s2");
  7. Use either include of forward to pass request and response objects of current web application.
  8. For example:

    rd. include (req, res); 
    rd.forward (req, res);

Deploying in same servers but from different web applications:

For example:

web.xml (webapp1):

<web-app>
    <servlet>
        <servlet-name>abc</servlet-name>
        <servlet-class>Serv1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>abc</servlet-name>
        <url-pattern>/s1</url-pattern>
    </servlet-mapping>
</web-app>

Serv1.java (webapp1):

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Serv1 extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        pw.println("<h2>I AM FROM Serv1 BEGINNING OF webapp1</h2>");
        ServletContext cctx = getServletContext();
        ServletContext octx = cctx.getContext("/webapp2");
        RequestDispatcher rd = octx.getRequestDispatcher("/s2");
        rd.include(req, res); // rd.forward (req, res);
        pw.println("<h2>I AM FROM Serv2 ENDING OF webapp1</h2>");
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doGet(req, res);
    }
};

web.xml (webapp2):

<web-app>
    <servlet>
        <servlet-name>pqr</servlet-name>
        <servlet-class>Serv2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>pqr</servlet-name>
        <url-pattern>/s2</url-pattern>
    </servlet-mapping>
</web-app>

Serv2.java (webapp2):

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Serv2 extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        pw.println("<h6>I AM FROM Serv2 OF webapp2</h6>");
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doGet(req, res);
    }
};

Forwarding request and response objects of one web application to another web application and both the web applications are running in different servlet containers:

In order to send the request and response object of one web application which is running in on servlet container to another web application which is running in another servlet container, we cannot use forward and include methods.

To achieve the above we must use a method called sendRedirect (String url) method whose prototype is The above method is present in HttpServletResponse interface, the parameter String url represents url of another web application which is running in some other servlet container.

http servlet response

The following diagram will illustrate the concept of sendRedirect method:

concept send redirect
  1. Make a request to a servlet or JSP which is running in a web application of one container http://localhost:2008/webapp1/s1 context path or document root of one web application.
  2. Servlet of web application of Tomcat will redirect the request of client to another web application of Weblogic by using the following statement: Res.sendRedirect ("http://localhost:7001/webapp2/s2"); must be written in Serv1 of webapp1
  3. Browser will send the request to another web application of another servlet container.
  4. For example:

    http://localhost:7001/webapp2/s2 which is redirected by Serv1 of webapp1.
  5. Webapp1 gives the response of Serv2 only but not Serv1 servlet.

Deploying in different servers:

For example:

web.xml (webapp1):

<web-app>
    <servlet>
        <servlet-name>abc</servlet-name>
        <servlet-class>Serv1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>abc</servlet-name>
        <url-pattern>/s1</url-pattern>
    </servlet-mapping>
</web-app>

Serv1.java (webapp1):

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Serv1 extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        pw.println("<h2>I AM FROM Serv1 OF webapp1</h2>");
        res.sendRedirect("http://localhost:7001/webapp2/s2");

    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doGet(req, res);
    }
};

Note:webapp1 must be deployed in Tomcat server.

web.xml (webapp2):

<web-app>
    <servlet>
        <servlet-name>pqr</servlet-name>
        <servlet-class>Serv2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>pqr</servlet-name>
        <url-pattern>/s2</url-pattern>
    </servlet-mapping>
</web-app>

Serv2.java (webapp2):

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Serv2 extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        pw.println("<h6>I AM FROM Serv2 OF webapp2</h6>");
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doGet(req, res);
    }
};

Note:webapp2 must be deployed in Weblogic server.

Develop the following application with the help of request dispatcher by using forward and include methods?

web app help request dispatcher

Answer:

web.xml:

<web-app>
    <servlet>
        <servlet-name>abc</servlet-name>
        <servlet-class>RecvServ</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>pqr</servlet-name>
        <servlet-class>DispServ</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>abc</servlet-name>
        <url-pattern>/receive</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>pqr</servlet-name>
        <url-pattern>/display</url-pattern>
    </servlet-mapping>
</web-app>

header.html:

<html>
    <title>Header</title>
    <body bgcolor="#D8BFD8">
    <center><hr>
        <font color="#990033" size="7">AshaKrishna Technologies<hr>
        </font>
    </center>
    </body>
</html>

login.html:

<html>
    <title>Login Page</title>
    <body bgcolor="#D87093">
    <center>
        <form name="header" action="./receive" method="post">
            <table bgcolor="#AFEEEE" border="1">
                <tr>
                    <th>Enter user name : </th>
                    <td><input type="text" name="header_euname" value=""></td>
                </tr>
                <tr>
                    <th>Enter password : </th>
                    <td><input type="password" name="header_epwd" value=""></td>
                </tr>
                <tr>
                    <td align="center"><input type="submit" name="header_login" value="Login"></td>
                    <td align="center"><input type="reset" name="header_reset" value="Reset"></td>
                </tr>
            </table>
        </form>
    </center>
    </body>
</html>
 

footer.html:

<html>
    <title>Footer</title>
    <body bgcolor="#D8BFD8">
    <center><hr>
        <font color="#990033" size="3">All copy rights&nbsp;&copy;&nbsp;reserved to BudDinu</hr>
        </font>
    </center>
    </body>
</html>
                        

RecvServ.java:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class RecvServ extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        RequestDispatcher rd = req.getRequestDispatcher("/display");
        rd.forward(req, res);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doGet(req, res);
    }
};

DispServ.java:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class DispServ extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        String uname = req.getParameter("header_euname");
        String pwd = req.getParameter("header_epwd");
        RequestDispatcher rd1 = req.getRequestDispatcher("header.html");
        rd1.include(req, res);
        pw.println("<br><br><br>");
        if (uname.equals("kvr") && pwd.equals("advanced")) {
            pw.println("<center><font color=#ffff66><h3>VALID CREDENTIALS</h3></center>");
        } else {
            pw.println("<center><font color=#ffff66><h3>INVALID CREDENTIALS</h3></center>");
        }
        pw.println("</font></br></br></br>");
        RequestDispatcher rd2 = req.getRequestDispatcher("footer.html");
        rd2.include(req, res);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doGet(req, res);
    }
};

Note:

  1. Whenever we pass a html program as a parameter to getRequestDispatcher method, that html program will be executed by browser at client side where as whenever we pass a servlet program or a JSP which will be executed by servlet container or server.
  2. RequestDispatcher interface object can also be obtained by using the following method which is present in HttpServletRequest.
http servlet request dispacher