13
1 RequestDispatcher in JSP – EEMag Sample Employee Management Version 0.1

RequestDispatcher in JSP – EEMag Sample

  • 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

Page 1: RequestDispatcher in JSP – EEMag Sample

1

RequestDispatcher in JSP – EEMag Sample

Employee ManagementVersion 0.1

Page 2: RequestDispatcher in JSP – EEMag Sample

2

Objectives

Using RequestDispatcher in JSP <jsp:forward page=“…"/>

Get parameters of the form in JSP request.getParameter("List")

Page 3: RequestDispatcher in JSP – EEMag Sample

3

Home page – menu.jsp

Go to Add pageGo to List page

Page 4: RequestDispatcher in JSP – EEMag Sample

4

List page – list.jsp

Page 5: RequestDispatcher in JSP – EEMag Sample

5

Add page – add.jsp

Page 6: RequestDispatcher in JSP – EEMag Sample

6

View page – view.jsp

Page 7: RequestDispatcher in JSP – EEMag Sample

7

Source code: menu.jsp

<html> <body> <form action="process.jsp"> <input type="Submit" name="List" value="List"> &nbsp;&nbsp; <input type="Submit" name="Add" value="Add"> &nbsp;&nbsp; </form> </body></html>

Page 8: RequestDispatcher in JSP – EEMag Sample

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 %>"/>

Page 9: RequestDispatcher in JSP – EEMag Sample

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>

Page 10: RequestDispatcher in JSP – EEMag Sample

10

Source code: add.jsp (1)

<html> <body> <a href="menu.jsp">Go to Menu</a> <form action="add.jsp"> Name*:&nbsp;&nbsp;<input type="input" name="fullName" value=""> <br/> Sex:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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>

Page 11: RequestDispatcher in JSP – EEMag Sample

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>

Page 12: RequestDispatcher in JSP – EEMag Sample

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>

Page 13: RequestDispatcher in JSP – EEMag Sample

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)