Web Component Development with Servlet and JSP Technologies, Java EE 6 Student Guide - Volume 1 SL-314-EE5 REV C.1 D61810GC10 Edition 1.0 D62412
Copyright 2008, 2009, 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, Inc. Disclaimer This training manual may include references to materials, offerings, or products that were previously offered by Sun Microsystems, Inc. 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. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. UNIX is a registered trademark licensed through X/Open Company, Ltd.
This page intentionally left blank.
This page intentionally left blank.
Table of Contents About This Course...Preface-xiii Course Goals... Preface-xiii Course Map...Preface-xiv Topics Not Covered...Preface-xv How Prepared Are You?...Preface-xvi How to Learn From This Course... Preface-xvi Introductions...Preface-xvii How to Use Course Materials... Preface-xviii Conventions...Preface-xix Icons...Preface-xix Typographical Conventions... Preface-xx Additional Conventions...Preface-xxi Introduction to Web Application Technologies...1-1 Objectives... 1-1 Relevance... 1-2 Additional Resources... 1-3 Web Application Technologies... 1-4 HTTP Client-Server Architecture... 1-5 Web Site Structure... 1-5 Java Platform, Enterprise Edition 5 (Java EE 5)... 1-7 Java EE 5 SDK... 1-7 Web Sites and Web Applications... 1-9 Execution of CGI Programs... 1-9 Advantages and Disadvantages of CGI Programs... 1-12 Execution of Java Servlets... 1-12 Advantages and Disadvantages of Java Servlets... 1-14 Using Separate Processes or Using Threads... 1-15 Java Servlets... 1-16 JavaServer Pages Technology... 1-17 Concerns When Using Servlets and JSP Technology... 1-19 Web Application Three-Tier Architecture... 1-20 Model-View-Controller (MVC) Architecture in a Web Application... 1-22 v
Model 2 Architecture... 1-23 Model 2 Frameworks... 1-25 Java EE Containers... 1-25 Java EE Architecture Example... 1-27 Job Roles... 1-28 Web Application Migration... 1-29 Summary... 1-31 Developing a View Component... 2-1 Objectives... 2-1 Relevance... 2-2 Additional Resources... 2-3 Designing a View Component... 2-4 Types of View Components... 2-4 Soccer League Case Study... 2-5 List Leagues Analysis Model... 2-6 List Leagues Page Flow... 2-7 HTTP Revisited... 2-12 Hypertext Transfer Protocol... 2-12 HTTP GET Method... 2-14 HTTP Request... 2-14 HTTP Request Headers... 2-15 HTTP Response... 2-16 HTTP Response Headers... 2-17 Web Container Architecture... 2-18 Request and Response Process... 2-19 Sequence Diagram of an HTTP GET Request... 2-23 Developing a Simple HTTP Servlet... 2-24 List Leagues Architecture Model... 2-24 The HttpServlet API... 2-25 The ListLeaguesServlet Class... 2-29 Configuring and Deploying a Servlet... 2-33 Soccer League Web Application Structure... 2-33 Configuring a Servlet Definition... 2-35 Configuring a Servlet Mapping... 2-36 Complete Deployment Descriptor... 2-37 Web Application Context Root... 2-38 Sun Java System Application Server Deployment... 2-38 WAR Files for Deployment... 2-40 Activating the Servlet in a Web Browser... 2-41 Certification Exam Notes... 2-43 Summary... 2-44 Developing a Controller Component... 3-1 Objectives... 3-1 Relevance... 3-2 Additional Resources... 3-3 vi Web Component Development With Servlet and JSP Technologies
Designing Controller Components... 3-4 Types of Controller Components... 3-4 Add a New League Analysis Model... 3-5 Add a New League Page Flow... 3-7 Form Verification... 3-11 Soccer League Web Structure... 3-12 Creating an HTML Form... 3-14 Add a New League Web Form... 3-14 The form Tag... 3-15 Textfield Component... 3-16 Drop-Down List Component... 3-17 Submit Button... 3-18 Complete HTML Form... 3-19 How Form Data Is Sent in an HTTP Request... 3-20 Form Data in the HTTP Request... 3-20 HTTP GET Method Request... 3-21 HTTP POST Method Request... 3-22 HTTP GET and POST Methods... 3-23 Developing a Controller Servlet... 3-24 Add a New League Use Case Revisited... 3-24 Servlet API to Retrieve Form Parameters... 3-26 Developing the AddLeagueServlet Servlet... 3-27 Dispatching From the Controller to a View... 3-31 Add a New League Use Case (Stage 2)... 3-31 Request Scope... 3-33 Using a Request Dispatcher... 3-33 Developing the AddLeagueServlet Code... 3-34 Developing the SuccessServlet Code... 3-37 Certification Exam Notes... 3-41 Summary... 3-42 Developing Dynamic Forms...4-1 Objectives... 4-1 Relevance... 4-2 Additional Resources... 4-3 Servlet Life Cycle Overview... 4-4 Servlet Class Loading... 4-5 One Instance Per Servlet Definition... 4-6 The init Life Cycle Method... 4-7 The service Life Cycle Method... 4-8 The destroy Life Cycle Method... 4-9 Using Initialization Parameter to Customize the Add a New League Form... 4-10 Add League Architecture Model (Stage 1)... 4-11 The AddLeagueFormServlet Code... 4-12 Configuring Initialization Parameters... 4-13 vii
The ServletConfig API... 4-14 The AddLeagueFormServlet Code... 4-16 Handling Errors in a Web Form... 4-17 Add League Analysis Model (Stage 2)... 4-17 Error Handling Screen Shots... 4-18 Add League Architecture Model (Stage 2)... 4-20 Soccer League Web Application Structure... 4-21 The AddLeagueServlet Code... 4-22 The AddLeagueFormServlet Code... 4-23 Repopulating Web Forms... 4-26 Repopulating a Text Field... 4-28 Repopulating a Drop-Down List... 4-29 Certification Exam Notes... 4-30 Summary... 4-31 Sharing Application Resources Using the Servlet Context... 5-1 Objectives... 5-1 Relevance... 5-2 Additional Resources... 5-3 Purpose of the Servlet Context... 5-4 Soccer League Demonstration... 5-4 Servlet Context... 5-6 Developing a Servlet Context Listener... 5-10 League List Initialization Example... 5-10 Web Application Life Cycle... 5-10 Certification Exam Notes... 5-18 Summary... 5-19 Designing the Business Tier... 6-1 Objectives... 6-1 Relevance... 6-2 Additional Resources... 6-3 Describing the Analysis Model... 6-4 Register For a League Use Case Example... 6-5 Domain Entities... 6-9 Player Entity Example... 6-10 Designing a Service Component... 6-13 Entity Service... 6-13 Façade Service... 6-20 Certification Exam Notes... 6-25 Summary... 6-26 Developing Web Applications Using Struts... 7-1 Objectives... 7-1 Relevance... 7-2 Additional Resources... 7-3 Struts Framework... 7-4 viii Web Component Development With Servlet and JSP Technologies
Model-View-Controller Pattern... 7-5 Front Controller Pattern... 7-7 Struts MVC Framework... 7-8 Developing a Struts Action Class... 7-10 Struts Action Class API... 7-10 Creating an Action Class... 7-11 Configuring the Struts Action Mappings... 7-15 Configuring the Infrastructure Controller... 7-15 Configuring Action Mappings... 7-17 Installing the Struts Library Files... 7-19 Certification Exam Notes... 7-21 Summary... 7-22 Developing Web Applications Using Session Management...8-1 Objectives... 8-1 Relevance... 8-2 Additional Resources... 8-3 HTTP and Session Management... 8-4 Web Container Sessions... 8-4 Designing Web Applications to Use Session Management... 8-5 Registration Use Case Example... 8-5 Registration Use Case Analysis Model... 8-6 Using Session Management in a Web Application... 8-7 Session API... 8-8 Storing Session Attributes... 8-9 Accessing Session Attributes... 8-10 Destroying the Session... 8-11 Using Cookies for Session Management... 8-13 Cookie API... 8-14 Using Cookies Example... 8-15 Performing Session Management Using Cookies... 8-15 Using URL-Rewriting for Session Management... 8-17 URL-Rewriting Implications... 8-18 Certification Exam Notes... 8-19 Summary... 8-20 Using Filters in Web Applications...9-1 Objectives... 9-1 Relevance... 9-2 Additional Resources... 9-3 Web Container Request Cycle... 9-4 Web Container Request Processing... 9-4 Applying Filters to an Incoming Request... 9-5 Applying Filters to a Dispatched Request... 9-7 Filter API... 9-8 Developing a Filter Class... 9-9 The PerformanceFilter Class... 9-9 ix
The init Method... 9-10 The dofilter Method... 9-11 The destroy Method... 9-12 Configuring the Filter... 9-13 Declaring a Filter in the web.xml File... 9-13 Declaring a Filter Mapping in the web.xml File... 9-13 Certification Exam Notes... 9-17 Summary... 9-18 Integrating Web Applications With Databases... 10-1 Objectives... 10-1 Relevance... 10-2 Additional Resources... 10-3 Mapping Sample Data Structures into Database Entities... 10-4 Designing a Web Application... 10-5 Domain Objects... 10-6 Database Tables... 10-7 Data Access Object (DAO) Pattern... 10-9 DAO Pattern Advantages... 10-10 JDBC API... 10-11 Developing a Web Application Using a Database... 10-12 Traditional Approaches to Database Connections... 10-12 Using a DataSource and the JNDI API... 10-13 Configuring a DataSource and the JNDI API... 10-21 Summary... 10-23 Developing JSP Pages... 11-1 Objectives... 11-1 Relevance... 11-2 Additional Resources... 11-3 JavaServer Pages Technology... 11-4 How a JSP Page Is Processed... 11-7 Developing and Deploying JSP Pages... 11-14 Writing JSP Scripting Elements... 11-15 Comments... 11-16 Directive Tag... 11-17 Declaration Tag... 11-17 Scriptlet Tag... 11-18 Expression Tag... 11-20 Implicit Variables... 11-21 Using the page Directive... 11-22 Using Standard Tags... 11-24 Other Standard Tags... 11-31 Using Expression Language (EL) Elements... 11-33 Syntax Overview... 11-33 Bean Access Using EL... 11-34 Unified Expression Language... 11-36 x Web Component Development With Servlet and JSP Technologies
Arithmetic Operators... 11-37 Comparisons and Logical Operators... 11-38 Configuring the JSP Environment... 11-39 Certification Exam Notes... 11-40 Summary... 11-42 Developing JSP Pages Using Custom Tags...12-1 Objectives... 12-1 Relevance... 12-2 Additional Resources... 12-3 The Java EE Job Roles Involved in Web Application Development... 12-4 Designing JSP Pages with Custom Tag Libraries... 12-5 Contrasting Custom Tags and Scriptlet Code... 12-5 Developing JSP Pages Using Custom Tags... 12-7 Custom Tag Library Overview... 12-8 Custom Tag Syntax Rules... 12-9 JSTL Sample Tags... 12-11 Using a Custom Tag Library in JSP Pages... 12-17 Using an Empty Custom Tag... 12-18 Using a Conditional Custom Tag... 12-18 Using an Iterative Custom Tag... 12-19 JSTL Tags... 12-21 Certification Exam Notes... 12-27 Summary... 12-28 Developing Web Applications Using Struts Action Forms...13-1 Objectives... 13-1 Relevance... 13-2 Additional Resources... 13-3 Struts Application Components... 13-4 Model Elements Review... 13-5 Control Elements Review... 13-6 View Elements Review... 13-7 Developing an ActionForm Class... 13-8 The Add a New League Form... 13-8 The AddLeagueForm Class... 13-9 Data Conversion... 13-11 Writing the validate Method... 13-11 Struts ActionError Class... 13-14 How the Controller Uses the Form Bean... 13-15 Developing the JSP Code for a View Form... 13-18 Struts html Tag Library Overview... 13-18 Writing the JSP Code for a View Form... 13-19 Configuring the View Forms... 13-22 Configuring the Form Beans... 13-22 Configuring the View Aspects of the Actions... 13-22 xi
Certification Exam Notes... 13-24 Summary... 13-25 Building Reusable Web Presentation Components... 14-1 Objectives... 14-1 Relevance... 14-2 Additional Resources... 14-3 Complex Page Layouts... 14-4 Presentation Segment Overview... 14-5 Organizing Presentation Segments... 14-6 Including JSP Page Segments... 14-7 Using the include Directive... 14-7 Using the jsp:include Standard Action... 14-8 Using the jsp:param Standard Action... 14-9 Developing Layouts Using Struts Tiles... 14-12 Layout JSP File... 14-12 Tiles Layout... 14-15 Content Body... 14-16 Certification Exam Notes... 14-17 Summary... 14-18 Introducing JavaServer Faces Technology...A-1 Objectives... A-1 Relevance... A-2 Additional Resources... A-3 JavaServer Faces Technology Overview... A-4 Introducing JavaServer Faces Technology... A-4 Components of a JavaServer Faces Technology-Based Web Application... A-6 Comparison of JavaServer Faces Technology With Struts Framework... A-10 Key JavaServer Faces Concepts... A-11 UI Component Model... A-12 UI Component Classes... A-12 DVD Library Sample Project: Use Cases... A-14 Quick Reference for HTML...B-1 Objectives... B-1 Additional Resources...B-2 HTML and Markup Languages...B-3 Definition...B-3 Types of Markup...B-3 Simple Example...B-4 Creating an HTML Document...B-5 Tag Syntax...B-5 Comments... B-6 Spaces, Tabs, and New Lines Within Text...B-6 xii Web Component Development With Servlet and JSP Technologies
Character and Entity References...B-6 Creating Links and Media Tags...B-8 The A Element and the HREF Attribute...B-8 The IMG Element and the SRC Attribute... B-9 The APPLET Element...B-9 The OBJECT Element...B-10 Text Structure and Highlighting...B-11 Text Structure Tags...B-11 Text Highlighting... B-14 HTML Forms...B-15 The FORM Tag...B-15 HTML Form Components... B-17 Input Tags... B-18 Text Fields... B-19 Submit Buttons... B-20 Reset Button...B-21 Checkboxes... B-22 Radio Buttons... B-23 Password... B-24 Hidden Fields...B-24 The SELECT Tag... B-25 The TEXTAREA Tag...B-26 Creating HTML Tables...B-27 Advanced HTML...B-30 JavaScript Programming Language...B-30 CSS... B-32 Frames... B-36 Quick Reference for HTTP... C-1 Objectives... C-1 Additional Resources... C-2 HTTP Overview... C-3 Definition... C-3 Request Structure... C-4 HTTP Methods... C-5 Request Headers... C-6 Response Structure... C-8 Response Headers... C-9 Status Codes... C-10 CGI Overview... C-12 Environment Variables Set... C-12 Data Formatting... C-14 Quick Reference for the Ant Tool... D-1 Objectives... D-1 Additional Resources... D-2 Ant Overview... D-3 xiii
Build File Structure... D-4 Projects... D-4 Targets... D-5 Tasks... D-5 Properties... D-6 Ant Special Features... D-7 Patterns... D-7 The fileset Element... D-8 Filtering... D-8 The filter Task... D-9 Basic Built-in Ant Tasks... D-10 The copy Task... D-10 The delete Task... D-12 The mkdir Task... D-13 The echo Task... D-13 The javac Task... D-14 The javadoc Task... D-15 The jar Task... D-16 Complete Ant Build File... D-17 Executing Ant... D-20 Installing Ant... D-21 System Requirements... D-21 Ant Environment Setup... D-21 Quick Reference for UML...E-1 Objectives... E-1 Additional Resources...E-2 UML Fundamentals...E-3 General Elements...E-6 Packages...E-6 Stereotypes...E-8 Annotation...E-8 Constraints...E-9 Tagged Values...E-9 Use Case Diagrams...E-10 Class Diagrams...E-11 Class Nodes...E-11 Inheritance...E-14 Interface Implementation... E-15 Association, Roles, and Multiplicity... E-16 Aggregation and Composition...E-17 Association Classes...E-18 Other Association Elements...E-21 Object Diagrams...E-22 Collaboration Diagrams...E-24 Sequence Diagrams...E-26 xiv Web Component Development With Servlet and JSP Technologies
Statechart Diagrams...E-29 Transitions... E-30 Activity Diagrams...E-31 Component Diagrams...E-35 Deployment Diagrams...E-37 Quick Reference for XML... F-1 Objectives... F-1 Additional Resources...F-2 Introduction to XML...F-3 Simple Example...F-3 Basic Syntax...F-4 Well-Formed XML Documents...F-4 Validity and DTDs...F-5 DTD-specific Information... F-7 Schemas...F-9 xv