Tuesday, 3 April 2018


RequestDispatcherInterface Concept in Servlet 1.forward() 2.include() ================================================================================ //index.html //FirstServlet.java //SecondServlet.java //ThirdServlet.java //web.xml ================================================================================ //index.html <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <h1><center>RequestDispatcher Example</center></h1> <body bgcolor="#E6E6FA" > <form action="FirstServlet"> <table border="2" align="center"> <tr><td>Employee Name</td><td><input type="text" name="ename"></td></tr> <tr><td>Employee Password</td><td><input type="password" name="epass"></td></tr> <tr><td>Employee Address</td><td><input type="text" name="address"></td></tr> <tr><td>Employee Email </td><td><input type="email" name="email"></td></tr> <tr><td>Employee Salary</td><td><input type="text" name="esal"></td></tr> <tr><td><input type="submit" value="submit"></td> </table> </form> </body> </html> =============================================================================== //FirstServlet.java package govind; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String ename = request.getParameter("ename"); String epass=request.getParameter("epass"); String eadd = request.getParameter("address"); String email=request.getParameter("email"); String esalry=request.getParameter("esal"); out.println("FirstServlet Result:"); out.println("Employee Name=:"+ename); out.println("Employee Password=:"+epass); out.println("Employee Address=:"+eadd); out.println("Employee Email=:"+email); out.println("Employee Salary=:"+esalry); RequestDispatcher rd=null; //ServletContext context =null; // Ist way :-getting RequestDispatcher by ServletRequest if(ename.equalsIgnoreCase("govind")&&epass.equalsIgnoreCase("khan")) { rd = request.getRequestDispatcher("/SecondServlet"); rd.forward(request, response); } else { rd = request.getRequestDispatcher("ThirdServlet"); rd.include(request, response); } // 2nd way :-getting RequestDispatcher by ServletContext /*if(ename.equalsIgnoreCase("govind")&&epass.equalsIgnoreCase("khan")) { context = getServletContext(); rd=context.getRequestDispatcher("/SecondServlet"); rd.forward(request, response); } else { context = getServletContext(); rd=context.getRequestDispatcher("/ThirdServlet"); //rd=context.getNamedDispatcher("ThirdServlet"); rd.include(request, response); } */ } } =============================================================================== //SecondServlet.java package govind; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SecondServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String ename = request.getParameter("ename"); String epass=request.getParameter("epass"); String eadd = request.getParameter("address"); String email=request.getParameter("email"); String esalry=request.getParameter("esal"); out.println("SecondServlet Result:"); out.println("Employee Name=:"+ename); out.println("Employee Password=:"+epass); out.println("Employee Address=:"+eadd); out.println("Employee Email=:"+email); out.println("Employee Salary=:"+esalry); } } ============================================================================== //ThirdServlet.java package govind; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ThirdServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); String ename = request.getParameter("ename"); String epass=request.getParameter("epass"); String eadd = request.getParameter("address"); String email=request.getParameter("email"); String esalry=request.getParameter("esal"); out.println("ThirdServlet Result:"); out.println("Employee Name=:"+ename); out.println("Employee Password=:"+epass); out.println("Employee Address=:"+eadd); out.println("Employee Email=:"+email); out.println("Employee Salary=:"+esalry); } } ================================================================================= //web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>sessionmanagement</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>FirstServlet</display-name> <servlet-name>FirstServlet</servlet-name> <servlet-class>govind.FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/FirstServlet</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>SecondServlet</display-name> <servlet-name>SecondServlet</servlet-name> <servlet-class>govind.SecondServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SecondServlet</servlet-name> <url-pattern>/SecondServlet</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>ThirdServlet</display-name> <servlet-name>ThirdServlet</servlet-name> <servlet-class>govind.ThirdServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ThirdServlet</servlet-name> <url-pattern>/ThirdServlet</url-pattern> </servlet-mapping> </web-app> ============================================================================================= run 1: if empname=govind && pass=khan then forward method executed o/p:- SecondServlet Result: Employee Name=:govind Employee Password=:khan Employee Address=:madhubani Employee Email=:govindkhan@gmail.com Employee Salary=:20000


run 2: if empname!=govind && pass=khan then include method executed o/p:- FirstServlet Result: Employee Name=:ballabh Employee Password=:khan Employee Address=:madhubani Employee Email=:govindkhan@gmail.com Employee Salary=:20000 ThirdServlet Result: Employee Name=:ballabh Employee Password=:khan Employee Address=:madhubani Employee Email=:govindkhan@gmail.com Employee Salary=:20000


No comments:

Post a Comment