Servlet Chaining using request dispatcher

Introduction

 
This is a process to make available requests of users to multiply servlets. The user provides request to the first servlet present in the chain. The first servlet uses the form fields present in the request. The first servlet can transfer the request to another servlet with the execution control. The second servlet can use the request, as it is available to itself. This process can be repeated for any number of servlets. The last servlet present in the chain provides a response to the user. All servlet present before the last servlet remains invisible to the user.
 

Including Response

 
This is a process to provide a response to the user by including the response of other pages into the response of the first servlet. The user provides the request of the first servlet, the first servlet can transfer the request with execution control to another servlet or page. The second servlet or page can use the request available from the first servlet. The second servlet provides response and execution control to the first servlet. The first servlet provides response to the user by including the response of the second page or servlet. This process can be repeated for any number of servlets. The user fills like getting the response of only one page.
 
Steps to follow for forward and include
  1. Create an object of javax.servlet.servletContext by using getServletContext() method of generic servlet.
  2. Create an object of javax.servlet.requestDispather by using getRequestDispatcher() of servlet context.This method accepts name and path of the forward and include file.
  3. Use forward () method of RequestDispatcher to forward the request or use include () of RequestDispatcher to include the response.
Ex:-
  1. < html > < head > < title > Login < /title></head > < body bgcolor = "orange" > < br > < p > < h3 > < center > Please enter your user name and password < /center></h3 > < /p><br><br><form action="./valid  
  2. "method="  
  3. post "><center>Username</center><center><input type="  
  4. text "name="  
  5. t1 "></center><center>Password</center><center><input type="  
  6. password "name="  
  7. p1 "></center><center><input type="  
  8. submit "name="  
  9. Submit "value="  
  10. Submit "></center></form></body></html>  
valid.java file
  1. import javax.servlet.*;  
  2. import javax.servlet.http.*;  
  3. import java.io.*;  
  4. import java.sql.*;  
  5. public class valid extends HttpServlet {  
  6.  public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {  
  7.   String s1 = req.getParameter("t1");  
  8.   String s2 = req.getParameter("p1");  
  9.   try {  
  10.    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  11.    Connection con = DriverManager.getConnection("jdbc:odbc:dsn1""system""pintu");  
  12.    Statement stmt = con.createStatement();  
  13.    ResultSet rs = stmt.executeQuery("select * from  userinfo where name='" + s1 + "' and pass='" + s2 + "'");  
  14.    ServletContext sc = getServletContext();  
  15.    RequestDispatcher rd1 = sc.getRequestDispatcher("/inbox");  
  16.    RequestDispatcher rd2 = sc.getRequestDispatcher("/error");  
  17.    if (rs.next())  
  18.     rd1.forward(req, res);  
  19.    else  
  20.     rd2.forward(req, res);  
  21.   } catch (Exception e) {  
  22.    System.out.println(e);  
  23.   }  
  24.  }  
inbox.java file
  1. import javax.servlet.http.*;  
  2. import javax.servlet.*;  
  3. import java.io.*;  
  4. public class inbox extends HttpServlet {  
  5.  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {  
  6.   String str = req.getParameter("t1");  
  7.   PrintWriter out = res.getWriter();  
  8.   out.println("<html><body>");  
  9.   out.println("<h1>Welcome:: " + str + "</h1>");  
  10.   out.println("</body></html>");  
  11.  }  
  12. }  
error.java file
  1. import javax.servlet.http.*;  
  2. import javax.servlet.*;  
  3. import java.io.*;  
  4. public class error extends HttpServlet {  
  5.  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {  
  6.   PrintWriter out = res.getWriter();  
  7.   out.println("<html><body>");  
  8.   out.println("<h1 align='center'>Invalid uid and password</h1>");  
  9.   ServletContext sc = getServletContext();  
  10.   RequestDispatcher rd1 = sc.getRequestDispatcher("/login.html");  
  11.   rd1.include(req, res);  
  12.   out.println("</body></html>");  
  13.  }  
  14. }