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.
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.
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.
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.
ServletContext ctx1=getServletContext (); [GenericServlet method] ServletContext ctx2=config.getServletContext (); [ServletConfig method] ServletContext ctx3=req.getServletContext (); [HttpServletRequest method]
RequestDispatcher rd=ctx.getRequestDispatcher ("./s2");
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-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>
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); } };
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.
For example:
ServletContext cctx=getServletContext ();
For example:
ServletContext octx=cctx.getContext ("./webapp2");
For example:
RequestDispatcher rd=octx.getRequestDispatcher ("/s2");
For example:
rd. include (req, res); rd.forward (req, res);
Deploying in same servers but from different web applications:
For example:
<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>
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-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>
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.
The following diagram will illustrate the concept of sendRedirect method:
For example:
http://localhost:7001/webapp2/s2 which is redirected by Serv1 of webapp1.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>
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-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>
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?
Answer:
<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>
<html> <title>Header</title> <body bgcolor="#D8BFD8"> <center><hr> <font color="#990033" size="7">AshaKrishna Technologies<hr> </font> </center> </body> </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>
<html> <title>Footer</title> <body bgcolor="#D8BFD8"> <center><hr> <font color="#990033" size="3">All copy rights © reserved to BudDinu</hr> </font> </center> </body> </html>
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); } };
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: