Web Component Development With Servlet and JSPTM
Technologies Activity Guide - Windows SL-314-EE6 D65271GC11 Edition 1.1 July 2010 D68407
Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
Disclaimer This document contains proprietary information, is provided under a license agreement containing restrictions on use and disclosure, and is protected by copyright and other intellectual property laws. You may copy and print this document solely for your own use in an Oracle training course. The document may not be modified or altered in any way. Except as expressly permitted in your license agreement or allowed by law, you may not use, share, download, upload, copy, print, display, perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization of Oracle. The information contained in this document is subject to change without notice. If you find any problems in the document, please report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not warranted to be error-free. Sun microsystems Disclaimer This training manual may include references to materials, offerings, or products that were previously offered by Sun microsystems . Certain materials, offerings, services, or products may no longer be offered or provided. Oracle and its affiliates cannot be held responsible for any such references should they appear in the text provided. Restricted Rights Notice If this documentation is delivered to the U.S. Government or anyone using the documentation on behalf of the U.S. Government, the following notice is applicable: U.S. GOVERNMENT RIGHTS The U.S. Government’s rights to use, modify, reproduce, release, perform, display, or disclose these training materials are restricted by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract. Trademark Notice Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
Â
This page intentionally left blank. Â
Â
This page intentionally left blank. Â
Â
Table of Contents About This Workbook ............................................................Preface-i Course Goal ................................................................................ Preface-i Conventions ................................................................................ Preface-ii Icons...................................................................................Preface-ii Typographical Conventions ............................................... Preface-ii Additional Conventions ................................................... Preface-iv Introduction to Java Servlets ..........................................................1-1 Objectives ............................................................................................ 1-1 Exercise 1.............................................................................................. 1-2 Task – Create a New Project........................................................ 1-2 Task – Create a servlet................................................................. 1-2 Task – Examine the Servlet ......................................................... 1-3 Task – Create the New Servlet Code ........................................... 1-3 Task – Run the New Servlet ........................................................ 1-3 Task – Provide an Index Page...................................................... 1-4 Exercise Summary ................................................................................ 1-5 Introduction to Java Server Pages .................................................2-1 Objectives ............................................................................................ 2-1 Exercise 1.............................................................................................. 2-2 Task – Create an HTMLForm ..................................................... 2-2 Task – Create a JSP ..................................................................... 2-2 Task – Run the Application ......................................................... 2-3 Exercise Summary ................................................................................ 2-4 Implementing an MVC Design.........................................................3-1 Objectives ............................................................................................ 3-1 Exercise 1.............................................................................................. 3-2 Task – Design a JavaBeans Compliant Model. ........................... 3-2 Task – Implement the Model ....................................................... 3-3 Task – Create an HTML Form to Submit the Request ................ 3-3 Task – Create a View Component Using a JSP........................... 3-4 Task – Create the Controller........................................................ 3-4 v Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision D
Task – Run the Application ......................................................... 3-5 Exercise Summary
................................................................................ 3-6 The Servlet’s Environment.............................................................. 4-1 Objectives ............................................................................................ 4-1 Exercise 1.............................................................................................. 4-2 Task – Create index.jsp and a View JSP ................................ 4-2 Task – Create the Controller Servlet............................................ 4-2 Exercise Summary ................................................................................ 4-4 Container Facilities For Servlets & JSPs....................................... 5-1 Objective.............................................................................................. 5-1 Exercise 1.............................................................................................. 5-2 Task – Investigate Projects Using web.xml............................... 5-2 Task – Code the Servlet to Use the Configuration Information .. 5-3 Task– Prepare the Index Page and Run the Application ............. 5-4 Exercise Summary ................................................................................ 5-6 More View Facilities......................................................................... 6-1 Objectives ............................................................................................ 6-1 Exercise 1.............................................................................................. 6-2 Task – List HTTP Headers .......................................................... 6-2 Exercise 2.............................................................................................. 6-3 Task.............................................................................................. 6-3 Exercise Summary ................................................................................ 6-4 Developing JSP Pages .................................................................... 7-1 Objectives ............................................................................................ 7-1 Exercise 1.............................................................................................. 7-2 Task – Investigate JSP Translation.............................................. 7-2 Exercise 2.............................................................................................. 7-3 Task – Investigate the jsp:useBean Tag.................................. 7-3 Task – Retrieve Session Information Using JSP Tags ................ 7-4 Exercise Summary ................................................................................ 7-5 Developing JSP Pages Using Custom Tags ................................. 8-1 Objective.............................................................................................. 8-1 Exercise 1.............................................................................................. 8-2 Task – Investigate the c:out Core Tag. ..................................... 8-2 Task – Investigate the c:remove Core Tag................................ 8-2 Exercise Summary ................................................................................ 8-4 More Controller Facilities................................................................ 9-1 Objectives ............................................................................................ 9-1 Exercise 1.............................................................................................. 9-2 Task – Witness the Failure........................................................... 9-2 Task – Examine the Cause of the Failure
.................................... 9-3 Exercise 2.............................................................................................. 9-4 Task – Examine the Starting Point. ............................................. 9-4 vi Web Component Development With Servlet and JSPTM Technologies Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision D
Task – Install a Login Mechanism in the Application................. 9-4 Task – Install a Security
Filter in the Application ....................... 9-5 Exercise Summary ................................................................................ 9-7 More Options for the Model...........................................................10-1 Objective............................................................................................ 10-1 Exercise 1............................................................................................ 10-2 Task – Create a New Project and an Entity. .............................. 10-2 Task – Create a controller.......................................................... 10-2 Task – Create JSPs..................................................................... 10-3 Task – Test the Application ....................................................... 10-3 Task – Optional: Add Edit, Delete, and Find Operations.......... 10-3 Exercise Summary .............................................................................. 10-4 Asynchronous Servlets and Clients.............................................11-1 Objectives .......................................................................................... 11-1 Exercise 1............................................................................................ 11-2 Task – Create a New Project and Asynchronous Servlet .......... 11-2 Task – Create an Asynchronous Client...................................... 11-3 Task – Test the Application. ...................................................... 11-3 Exercise Summary .............................................................................. 11-5 Implementing Security ...................................................................12-1 Objectives .......................................................................................... 12-1 Exercise 1............................................................................................ 12-2 Task – Verify the Project Prior to Adding Security................... 12-2 Task – Configure Users in the GlassFish Server ....................... 12-2 Task – Assign Users to Roles .................................................... 12-2 Task – MandateLogin Before Access........................................ 12-3 Task – Run the Application and Verify Login Requirement..... 12-3 Task – Pick Up the Username from the Environment ............... 12-4 Task – Modify the Application to Use Form-based login ......... 12-4 Task – Provide a Logout Mechanism ........................................ 12-4 Exercise Summary .............................................................................. 12-5 vii Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision D
viii Web Component Development With Servlet and JSPTM Technologies Copyright 2009 Sun Microsystems, Inc. All Rights
Reserved. Sun Services, Revision D
Lab Preface
About This Workbook Course Goal Upon completion of this lab workbook, you should be able to: •
Write servlets using the JavaTM programming language (Java servlets) •
Create robust web applications using Struts, session management, filters, and database integration •
Write pages created with the JavaServer PagesTM technology (JSPTM pages) •
Create easy-to-maintain JSP pages using the Expression Language, JSP Standard Tag Library (JSTL), and the Struts Tiles framework •
Create robust web applications that integrate Struts and JSP pages This workbook presents the lab exercises for each module of the Student Guide. Lab Preface-i Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision D
Conventions
Conventions The following conventions are used in this course to represent various training elements and alternative learning resources.
Icons Note – Indicates additional information that can help you, but is not crucial to your understanding of the concepts being described.
Typographical Conventions Courier is used for the names of commands, files, directories, programming code, and on-screen computer output; for example: Use ls -al to list all files. system% You have mail. Courier is also used to indicate programming constructs, such as class names, methods, and keywords; for example: The getServletInfo method is used to get author information. The java.awt.Dialog class contains Dialog constructor. Courier bold is used for characters and numbers that you type; for example: To list the files in this directory, type: # ls Courier bold is also used for each line of programming code that is referenced in a textual description; for example: 1 import java.io.*; 2 import javax.servlet.*; 3 import javax.servlet.http.*; Notice the javax.servlet interface is imported to allow access to its life cycle methods (Line 2). Lab Preface-ii Web Component Development With Servlet and JSPTM Technologies Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision D