Upload
kedem
View
30
Download
0
Embed Size (px)
DESCRIPTION
RequestDispatcher in JSP – EEMag Sample. Employee Management Version 0.1. Objectives. Using RequestDispatcher in JSP
Citation preview
1
RequestDispatcher in JSP – EEMag Sample
Employee ManagementVersion 0.1
2
Objectives
Using RequestDispatcher in JSP <jsp:forward page=“…"/>
Get parameters of the form in JSP request.getParameter("List")
3
Home page – menu.jsp
Go to Add pageGo to List page
4
List page – list.jsp
5
Add page – add.jsp
6
View page – view.jsp
7
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>
8
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 %>"/>
9
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>
10
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>
11
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>
12
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>
13
Pack the web application into war file
1. Open the window command line by going to menu Start | Run. Then execute “cmd” comand.
2. Change the working directory into the folder that contains the web application
3. Execute the comand:jar -cvf eemag01.war .
(Note: run “jar” without any argument to view helps)