Employee Management System using RequestDispatcher in JSP
130 likes | 229 Vues
Learn to create an Employee Management System in JSP using RequestDispatcher to navigate between pages and manage employee data. Source code snippets and web XML configuration included.
Employee Management System using RequestDispatcher in JSP
E N D
Presentation Transcript
RequestDispatcher in JSP – EEMag Sample Employee Management Version 0.1
Objectives • Using RequestDispatcher in JSP • <jsp:forward page=“…"/> • Get parameters of the form in JSP • request.getParameter("List")
Home page – menu.jsp Go to Add page Go to List page
Source code: menu.jsp <html> <body> <form action="process.jsp"> <input type="Submit" name="List" value="List"> <input type="Submit" name="Add" value="Add"> </form> </body> </html>
Source code: process.jsp <%! String nextPage; %> <% if (request.getParameter("List") != null) { System.out.println("Clicked List button"); nextPage = "list.jsp"; } else if (request.getParameter("Add") != null ) { System.out.println("Clicked List button"); nextPage = "add.jsp"; } %> <jsp:forward page="<%= nextPage %>"/>
Source code: list.jsp <html> <body> <a href="menu.jsp">Go to Menu</a> <br/> <h1>Listing Employes:</h1> <table border="1" cellspacing="0"> <tr> <th>Full name</th> <th>Sex</th> <th>Adress</th> </tr> <tr> <td>NNNN</td> <td>NNNN</td> <td>NNNN</td> </tr> </table> </body> </html>
Source code: add.jsp (1) <html> <body> <a href="menu.jsp">Go to Menu</a> <form action="add.jsp"> Name*: <input type="input" name="fullName" value=""> <br/> Sex: <select name="sex"> <option></option> <option name="male">Male</option> <option name="female">Female</option> </select> <br/> Address:<textarea name="address" cols="10" rows="3"></textarea> <br/><br/> <input type="Submit" name="Create" value="Create"> </form> </body>
Source code: add.jsp (2) <%! boolean createSuccess; %> <% // Clicked on Add button createSuccess = false; if ((request.getParameter("Create") != null) && (!"".equals(request.getParameter("fullName")))) { String fullName = request.getParameter("fullName"); String sex = request.getParameter("sex"); String address = request.getParameter("address"); // Logging for debug System.out.println("eemag.add: fullName = " + fullName); System.out.println("eemag.add: sex = " + sex); System.out.println("eemag.add: address = " + address); createSuccess = true; } %> <% if (createSuccess) { %> <%-- Forward to the view page --%> <jsp:forward page="view.jsp"/> <% } %> </html>
Web Application Descriptor – web.xml <?xml version="1.0"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Employee Management</display-name> <welcome-file-list> <welcome-file>menu.jsp</welcome-file> </welcome-file-list> </web-app>
Pack the web application into war file • Open the window command line by going to menu Start | Run. Then execute “cmd” comand. • Change the working directory into the folder that contains the web application • Execute the comand: jar -cvf eemag01.war . (Note: run “jar” without any argument to view helps)