StudentGuide Web Component Development With Java ...

milklivereddeepInternet και Εφαρμογές Web

13 Νοε 2013 (πριν από 3 χρόνια και 8 μήνες)

559 εμφανίσεις

Sun Microsystems, Inc.
UBRM05-104
500 Eldorado Blvd.
Broomfield, CO 80021
U.S.A.
Revision A.1
Student Guide
Web Component
Development With Javaª
Technology
SL-314
Please
Recycle
Copyright 2002 SunMicrosystems,Inc.,901 SanAntonio Road,Palo Alto,California 94303,U.S.A.All rights reserved.
This product or document is protectedby copyright anddistributedunder licenses restricting its use,copying,distribution,
anddecompilation.No part of this product or document may be reproducedinany formby any means without prior
writtenauthorizationof Sunandits licensors,if any.
Third-party software,including font technology,is copyrightedandlicensedfromSunsuppliers.
Sun,SunMicrosystems,the SunLogo,the Duke Logo,EJB,Enterprise JavaBeans,JavaBeans,Javadoc,JDK,Java Namingand
Directory Interface,JavaScript,Java Servlet,Java 2 Platform,Enterprise Edition,Java 2 Platform,StandardEdition,Java 2
SDK,JDBC,J2EE,J2SE,JSP,JVM,Solaris,andSunEnterprise are trademarks or registeredtrademarks of SunMicrosystems,
Inc.inthe U.S.andother countries.
Netscape andNetscape Navigator is a trademark or registeredtrademark of Netscape Communications Corporation.
INTERACTIVEUNIXis a registeredtrademark of INTERACTIVESystems Corporation.UNIXis a registeredtrademark in
the UnitedStates andother countries,exclusively licensedthroughX/OpenCompany,Ltd.
RESTRICTEDRIGHTS:Use,duplication,or disclosure by the U.S.Government is subject to restrictions of FAR52.227-
14(g)(2)(6/87) andFAR52.227-19(6/87),or DFAR252.227-7015 (b)(6/95) andDFAR227.7202-3(a).
DOCUMENTATIONIS PROVIDED“AS IS” ANDALLEXPRESS ORIMPLIEDCONDITIONS,REPRESENTATIONS,AND
WARRANTIES,INCLUDINGANYIMPLIEDWARRANTYOF MERCHANTABILITY,FITNESS FORAPARTICULAR
PURPOSEORNON-INFRINGEMENT,AREDISCLAIMED,EXCEPTTOTHEEXTENTTHATSUCHDISCLAIMERS ARE
HELDTOBELEGALLYINVALID.
THIS MANUAL IS DESIGNED TO SUPPORT AN INSTRUCTOR-LED TRAINING
(ILT) COURSE AND IS INTENDED TO BE USED FOR REFERENCE PURPOSES IN
CONJUNCTION WITH THE ILT COURSE.THE MANUAL IS NOT A STANDALONE
TRAINING TOOL.USE OF THE MANUAL FOR SELF-STUDY WITHOUT CLASS
ATTENDANCE IS NOT RECOMMENDED.
Please
Recycle
Copyright 2002 SunMicrosystems Inc.,901 SanAntonio Road,Palo Alto,California 94303,Etats-Unis.Tous droits réservés.
Ce produit ou document est protégé par un copyright et distribué avec des licences qui en restreignent l’utilisation,la copie,
la distribution,et la décompilation.Aucune partie de ce produit ou document ne peut être reproduite sous aucune forme,
par quelque moyen que ce soit, sans l’autorisation préalable et écrite de Sun et de ses bailleurs de licence, s’il y en a.
Le logiciel détenu par des tiers,et qui comprend la technologie relative aux polices de caractères,est protégé par un
copyright et licencié par des fournisseurs de Sun.
Sun,Sun Microsystems,le logo Sun,le logo Duke,EJB,Enterprise JavaBeans,JavaBeans,Javadoc,JDK,Java Naming and
Directory Interface,JavaScript,Java Servlet,Java 2 Platform,Enterprise Edition,Java 2 Platform,Standard Edition,Java 2
SDK,JDBC,J2EE,J2SE,JSP,JVM,Solaris,et Sun Enterprise sont des marques de fabrique ou des marques déposées de Sun
Microsystems, Inc. aux Etats-Unis et dans d’autres pays.
Netscape et Netscape Navigator est une marque de Netscape Communications Corporationaux Etats-Unis et dans d’autres
pays.inthe UnitedStates andother countries.
INTERACTIVEest une marque de INTERACTIVESystems Corporation.UNIXest une marque enregistree aux Etats-Unis et
dans d’autres pays et licenciée exclusivement par X/OpenCompany Ltd.
LA DOCUMENTATION EST FOURNIE “EN L’ETAT” ET TOUTES AUTRES CONDITIONS,DECLARATIONS ET
GARANTIES EXPRESSES OU TACITES SONT FORMELLEMENT EXCLUES,DANS LA MESURE AUTORISEE PAR LA
LOI APPLICABLE,Y COMPRIS NOTAMMENT TOUTE GARANTIE IMPLICITE RELATIVE A LA QUALITE
MARCHANDE, A L’APTITUDE A UNE UTILISATION PARTICULIERE OU A L’ABSENCE DE CONTREFAÇON.
v
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Table of Contents
About This Course...........................................................................xix
Course Goal....................................................................................... xix
Learning Objectives............................................................................xx
Module-by-Module Overview.........................................................xxi
Topics Not Covered..........................................................................xxii
How Prepared Are You?.................................................................xxiii
How to Learn From This Course.................................................. xxiii
Introductions....................................................................................xxiv
How to Use Course Materials.........................................................xxv
Conventions......................................................................................xxvi
Icons..........................................................................................xxvi
Typographical Conventions.................................................xxvii
Additional Conventions.......................................................xxviii
Introduction to Web Application Technologies.............................1-1
Objectives........................................................................................... 1-1
Relevance.............................................................................................1-2
Additional Resources....................................................................... 1-2
Internet Services.................................................................................1-3
The Internet Is a Network of Networks.................................1-3
Networking Protocol Stack......................................................1-5
Client-Server Architecture.......................................................1-7
Hypertext Transfer Protocol.............................................................1-8
Web Browsers and Web Servers.............................................1-8
HTTP Client-Server Architecture...........................................1-9
The Structure of a Web Site...................................................1-10
Web Applications.............................................................................1-12
CGI Programs on the Web Server.........................................1-12
Execution of CGI Programs...................................................1-13
Advantages and Disadvantages of CGI Programs............1-14
Java Servlets......................................................................................1-15
Servlets on the Web Server....................................................1-15
Execution of Java Servlets......................................................1-16
Advantages and Disadvantages of Java Servlets.............. 1-18
vi Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Template Pages.................................................................................1-19
Other Template Page Technologies......................................1-20
JavaServer Pages Technology................................................1-21
Advantages and Disadvantages of JavaServer Pages....... 1-22
The Model 2 Architecture......................................................1-22
The J2EE Platform............................................................................1-24
An Example of J2EE Architecture........................................ 1-25
Job Roles...................................................................................1-26
Web Application Migration...................................................1-26
Summary...........................................................................................1-28
Developing a Simple Servlet...........................................................2-1
Objectives........................................................................................... 2-1
Relevance.............................................................................................2-2
Generic Internet Services..................................................................2-3
The NetServer Architecture.....................................................2-3
The Generic Servlets API........................................................ 2-5
The Generic HelloServlet Class..........................................2-6
HTTP Servlets.....................................................................................2-7
Hypertext Transfer Protocol....................................................2-7
HTTP GET Method...................................................................2-8
HTTP Request............................................................................2-8
The HTTPServletRequest API..............................................2-9
HTTP Response...................................................................... 2-11
The HTTPServletResponse API............................................2-12
Web Container Architecture...........................................................2-14
The Web Container.................................................................2-14
Sequence Diagram of a HTTP GET Request.......................2-15
Request and Response Process............................................ 2-16
The HTTP Servlet API........................................................... 2-19
The HTTP HelloServlet Class.......................................... 2-20
Deploying a Servlet..........................................................................2-21
Installing, Configuring, and Running the Web
Container...............................................................................2-21
Deploying the Servlet to the Web Container......................2-21
Activating the Servlet in a Web Browser.............................2-22
Summary...........................................................................................2-23
Certification Exam Notes................................................................2-24
Developing a Simple Servlet That Uses HTML Forms..................3-1
Objectives........................................................................................... 3-1
Relevance.............................................................................................3-2
Additional Resources....................................................................... 3-2
HTML Forms......................................................................................3-3
The FORM Tag............................................................................ 3-4
HTML Form Components...................................................... 3-5
Input Tags................................................................................. 3-6
vii
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Textfield Component................................................................3-6
Submit Button Components....................................................3-7
Reset Button Component.........................................................3-8
Checkbox Component............................................................. 3-9
Radio Button Component..................................................... 3-10
Password Component........................................................... 3-11
Hidden Field Component......................................................3-11
The SELECT Tag...................................................................... 3-12
The TEXTAREA Tag...................................................................3-13
Form Data in the HTTP Request....................................................3-14
HTTP GET Method Request................................................. 3-15
HTTP POST Method Request............................................... 3-16
To GET or to POST?............................................................... 3-18
How Servlets Access Form Data....................................................3-19
The Servlet API........................................................................3-19
The FormBasedHello Servlet............................................... 3-20
Summary...........................................................................................3-22
Certification Exam Notes................................................................3-23
Developing a Web Application Using a Deployment
Descriptor.......................................................................................4-1
Objectives........................................................................................... 4-1
Problems With Simple Servlets........................................................4-2
Problems With Deploying in One Place................................4-2
Multiple Web Applications.................................................... 4-3
Web Application Context Name.............................................4-4
Problems With Servlet Naming..............................................4-5
Solutions to Servlet Naming Problems................................. 4-6
Problems Using Common Services........................................4-7
Developing a Web Application Using a Deployment
Descriptor.........................................................................................4-8
The Deployment Descriptor....................................................4-8
A Development Environment...............................................4-10
The Deployment Environment............................................ 4-11
The Web Archive (WAR) File Format................................. 4-16
Summary...........................................................................................4-17
Certification Exam Notes................................................................4-18
Configuring Servlets........................................................................5-1
Objectives........................................................................................... 5-1
Relevance.............................................................................................5-2
Servlet Life Cycle Overview.............................................................5-3
The init Life Cycle Method...................................................5-3
The service Life Cycle Method.............................................5-4
The destroy Life Cycle Method.............................................5-4
Servlet Configuration........................................................................5-5
The ServletConfig API.........................................................5-5
Initialization Parameters..........................................................5-6
Summary.............................................................................................5-9
viii Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Certification Exam Notes................................................................5-10
Sharing Resources Using the Servlet Context.............................6-1
Objectives........................................................................................... 6-1
Relevance.............................................................................................6-2
The Web Application.........................................................................6-3
Duke’s Store Web Application................................................6-3
The ServletContext API.......................................................6-4
Context Initialization Parameters.......................................... 6-5
Access to File Resources.......................................................... 6-6
Writing to the Web Application Log File..............................6-6
Accessing Shared Runtime Attributes.................................. 6-7
The Web Application Life Cycle......................................................6-8
Duke’s Store Example............................................................. 6-9
Configuring Servlet Context Listeners.................................6-11
Summary...........................................................................................6-12
Certification Exam Notes................................................................6-13
Developing Web Applications Using the MVC Pattern.................7-1
Objectives........................................................................................... 7-1
Activities of a Web Application.......................................................7-2
The Soccer League Example.............................................................7-3
Page Flow of the Soccer League Example............................ 7-4
Architecture of the Soccer League Example..........................7-6
Activity Diagram of the Soccer League Example.................7-6
Discussion of the Simple Web Application....................................7-8
Model-View-Controller for a Web Application.............................7-9
Sequence Diagram of MVC in the Web Tier.......................7-10
Soccer League Application: The Domain Model................7-11
Soccer League Application: The Services Model................7-11
Soccer League Application: The Big Picture.......................7-13
Soccer League Application: The Controller........................7-14
Soccer League Application: The Views................................7-15
The Request Scope................................................................. 7-16
Summary...........................................................................................7-17
Certification Exam Notes................................................................7-18
Developing Web Applications Using Session Management........8-1
Objectives........................................................................................... 8-1
Relevance.............................................................................................8-2
Additional Resources....................................................................... 8-2
HTTP and Session Management......................................................8-3
Sessions in a Web Container...................................................8-3
Web Application Design Using Session Management.................8-4
Example: Registration Use Case.............................................8-4
Example: Multiple Views for Registration............................8-5
Example: Enter League Form................................................. 8-6
ix
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Web Application Development Using Session
Management....................................................................................8-8
The Session API.........................................................................8-8
Retrieving the Session Object..................................................8-9
Storing Session Attributes......................................................8-10
Accessing Session Attributes.................................................8-12
Destroying the Session...........................................................8-15
Session Management Using Cookies............................................8-17
The Cookie API.......................................................................8-17
Using Cookies..........................................................................8-18
Using Cookies for Session Management.............................8-19
Session Management Using URL-Rewriting...............................8-21
Implications of Using URL-Rewriting.................................8-22
Guidelines for Working With Sessions.........................................8-23
Summary...........................................................................................8-24
Certification Exam Notes................................................................8-25
Handling Errors in Web Applications.............................................9-1
Objectives........................................................................................... 9-1
Additional Resources........................................................................9-2
The Types of Web Application Errors............................................9-3
HTTP Error Codes....................................................................9-3
Generic HTTP Error Page....................................................... 9-4
Servlet Exceptions.....................................................................9-5
Generic Servlet Error Page...................................................... 9-6
Using Custom Error Pages...............................................................9-7
Creating Error Pages.................................................................9-7
Declaring HTTP Error Pages.................................................. 9-8
Example HTTP Error Page......................................................9-8
Declaring Servlet Exception Error Pages...............................9-9
Example Servlet Error Page.................................................. 9-10
Developing an Error Handling Servlet.........................................9-11
Programmatic Exception Handling...............................................9-14
Exception Handling Servlet Declarations...........................9-16
Trade-offs for Declarative Exception Handling................ 9-17
Trade-offs for Programmatic Exception Handling........... 9-18
Logging Exceptions.........................................................................9-19
Summary...........................................................................................9-20
Certification Exam Notes................................................................9-21
Configuring Web Application Security.........................................10-1
Objectives......................................................................................... 10-1
Relevance...........................................................................................10-2
Additional Resources..................................................................... 10-2
Web Security Issues.........................................................................10-3
Authentication.........................................................................10-3
Authorization..........................................................................10-4
x Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Maintaining Data Integrity................................................... 10-5
Access Tracking.......................................................................10-5
Dealing With Malicious Code...............................................10-6
Dealing With Web Attacks....................................................10-6
Declarative Authorization..............................................................10-7
Web Resource Collection.......................................................10-7
Declaring Security Roles........................................................10-8
Security Realms.......................................................................10-9
Declarative Authentication...........................................................10-10
BASIC Authentication......................................................... 10-11
Summary.........................................................................................10-15
Certification Exam Notes..............................................................10-16
Understanding Web Application Concurrency Issues...............11-1
Objectives......................................................................................... 11-1
Additional Resources......................................................................11-2
The Need for Servlet Concurrency Management........................11-3
Concurrency Management Example................................... 11-4
Attributes and Scope.......................................................................11-8
Local Variables....................................................................... 11-9
Instance Variables................................................................ 11-10
Class Variables..................................................................... 11-11
Request Scope....................................................................... 11-12
Session Scope........................................................................ 11-13
Application Scope................................................................ 11-14
The Single Threaded Model.........................................................11-15
The SingleThreadModel Interface....................................11-15
How the Web Container Might Implement the
Single Threaded Model.................................................... 11-16
STM and Concurrency Management................................ 11-17
Recommended Approaches to Concurrency
Management................................................................................11-19
Summary.........................................................................................11-20
Certification Exam Notes..............................................................11-21
Integrating Web Applications With Databases...........................12-1
Objectives......................................................................................... 12-1
Relevance...........................................................................................12-2
Additional Resources..................................................................... 12-2
Database Overview..........................................................................12-3
The JDBC API......................................................................... 12-4
Designing a Web Application That Integrates With a
Database.........................................................................................12-5
Domain Objects.......................................................................12-5
Database Tables.......................................................................12-6
Data Access Object Pattern....................................................12-7
Advantages of the DAO Pattern.......................................... 12-9
xi
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Developing a Web Application That Uses a Connection
Pool................................................................................................12-10
Connection Pool....................................................................12-10
Storing the Connection Pool in a Global Name
Space................................................................................... 12-12
Accessing the Connection Pool.......................................... 12-13
Initializing the Connection Pool........................................ 12-16
Developing a Web Application That Uses a Data Source........12-17
Summary.........................................................................................12-18
Certification Exam Notes..............................................................12-19
Developing JSPª Pages...............................................................13-1
Objectives......................................................................................... 13-1
Relevance...........................................................................................13-2
Additional Resources..................................................................... 13-2
JavaServer Page Technology..........................................................13-3
How a JSP Page Is Processed.................................................13-5
Developing and Deploying JSP Pages.................................13-7
JSP Scripting Elements....................................................................13-8
Comments............................................................................... 13-9
Directive Tag......................................................................... 13-10
Declaration Tag.................................................................... 13-11
Scriptlet Tag.......................................................................... 13-12
Expression Tag......................................................................13-13
Implicit Variables................................................................. 13-14
The page Directive.........................................................................13-15
JSP Page Exception Handling.......................................................13-17
Declaring an Error Page...................................................... 13-18
Developing an Error Page................................................... 13-19
Behind the Scenes...........................................................................13-20
Debugging a JSP Page......................................................... 13-23
Summary.........................................................................................13-26
Certification Exam Notes..............................................................13-27
Developing Web Applications Using the Model 1
Architecture..................................................................................14-1
Objectives......................................................................................... 14-1
Additional Resources......................................................................14-2
Designing With Model 1 Architecture..........................................14-3
Guest Book Form.................................................................... 14-4
Guest Book Components...................................................... 14-5
Guest Book Page Flow........................................................... 14-6
What Is a JavaBeans Component?....................................... 14-7
The GuestBookService JavaBeans Component............... 14-8
Developing With Model 1 Architecture.......................................14-9
The Guest Book HTML Form................................................14-9
JSP Standard Actions............................................................14-10
xii Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Creating a JavaBeans Component in a JSP Page..............14-10
Initializing the JavaBean Component................................14-11
Control Logic in the Guest Book JSP Page........................14-13
Accessing a JavaBeans Component in a JSP Page............14-14
Beans and Scope....................................................................14-15
Review of the jsp:useBean Action...................................14-15
Summary.........................................................................................14-17
Certification Exam Notes..............................................................14-18
Developing Web Applications Using the Model 2
Architecture.................................................................................15-1
Objectives......................................................................................... 15-1
Relevance...........................................................................................15-2
Designing With Model 2 Architecture..........................................15-3
The Soccer League Example Using Model 2
Architecture.........................................................................15-4
Sequence Diagram of Model 2 Architecture...................... 15-6
Developing With Model 2 Architecture.......................................15-8
Controller Details................................................................... 15-9
Request Dispatchers............................................................ 15-14
View Details.......................................................................... 15-15
Summary.........................................................................................15-16
Certification Exam Notes..............................................................15-17
Building Reusable Web Presentation Components...................16-1
Objectives......................................................................................... 16-1
Complex Page Layouts....................................................................16-2
What Does a Fragment Look Like?......................................16-3
Organizing Your Presentation Fragments...........................16-4
Including JSP Page Fragments.......................................................16-5
Using the include Directive.................................................16-5
Using the jsp:include Standard Action...........................16-6
Using the jsp:param Standard Action................................16-7
Summary...........................................................................................16-9
Certification Exam Notes..............................................................16-10
Developing JSP Pages Using Custom Tags...............................17-1
Objectives......................................................................................... 17-1
Relevance...........................................................................................17-2
Additional Resources..................................................................... 17-2
Job Roles Revisited...........................................................................17-3
Introducing Custom Tag Libraries................................................17-4
Contrasting Custom Tags and Scriptlet Code....................17-4
Developing JSP Pages Using Custom Tags.........................17-5
What Is a Custom Tag Library?............................................17-6
Custom Tag Syntax Rules......................................................17-6
Example Tag Library: Soccer League...................................17-8
xiii
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Developing JSP Pages Using a Custom Tag Library................17-10
Using an Empty Custom Tag..............................................17-11
Using a Conditional Custom Tag...................................... 17-12
Using an Iterative Custom Tag.......................................... 17-13
Summary.........................................................................................17-14
Certification Exam Notes..............................................................17-15
Developing a Simple Custom Tag.................................................18-1
Objectives......................................................................................... 18-1
Overview of Tag Handlers.............................................................18-2
Fundamental Tag Handler API.............................................18-2
Tag Handler Life Cycle..........................................................18-3
Tag Library Relationships......................................................18-6
Developing a Tag Handler Class...................................................18-8
The getReqParam Tag............................................................18-8
The getReqParam Tag Handler Class..................................18-9
Configuring the Tag Library Descriptor.....................................18-11
Tag Declaration Element..................................................... 18-12
Custom Tag Body Content................................................. 18-13
Custom Tag Attributes.........................................................18-13
Custom Tag That Includes the Body...........................................18-14
The heading Tag.................................................................. 18-15
The heading Tag Handler Class........................................ 18-16
The heading Tag Descriptor.............................................. 18-18
Summary.........................................................................................18-19
Certification Exam Notes..............................................................18-20
Developing Advanced Custom Tags............................................19-1
Objectives......................................................................................... 19-1
Writing a Conditional Custom Tag...............................................19-2
Example: The checkStatus Tag..........................................19-2
The checkStatus Tag Handler............................................19-3
The checkStatus Tag Life Cycle.........................................19-4
Writing an Iterator Custom Tag.....................................................19-5
Iteration Tag API.................................................................... 19-6
Iteration Tag Life Cycle..........................................................19-7
Example: The iterateOverErrors Tag.............................19-9
The iterateOverErrors Tag Handler.............................19-10
Using the Page Scope to Communicate............................ 19-13
Summary.........................................................................................19-15
Certification Exam Notes..............................................................19-16
Integrating Web Applications With Enterprise JavaBeans
Components....................................................................................20-1
Objectives......................................................................................... 20-1
Relevance...........................................................................................20-2
Additional Resources......................................................................20-3
Distributing the Business Logic.....................................................20-4
Java 2 Platform, Enterprise Edition......................................20-5
xiv Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Enterprise JavaBeans Technology....................................... 20-6
Integrating the Web Tier With the EJB Tier.................................20-7
Enterprise JavaBeans Interfaces........................................... 20-8
Java Naming and Directory Service.................................... 20-9
Creating the Business Delegate....................................................20-10
Declaring the Business Delegate Class..............................20-11
Finding the Home interface Using JNDI.......................... 20-12
Delegating Business Methods to the EJB Tier.................. 20-13
Using the Business Delegate in a Servlet Controller.................20-14
Using a Delegate in a Servlet.............................................. 20-15
Summary.........................................................................................20-17
Certification Exam Notes..............................................................20-18
Quick Reference for HTML..............................................................A-1
Objectives.......................................................................................... A-1
Additional Resources.......................................................................A-2
HTML and Markup Languages......................................................A-3
Definition...................................................................................A-3
Types of Markup......................................................................A-3
Simple Example........................................................................A-4
Basic Structure of HTML..................................................................A-5
Tag Syntax.................................................................................A-5
Comments.................................................................................A-5
Spaces, Tabs, and Newlines Within Text..............................A-6
Character and Entity References............................................A-6
Links and Media Tags......................................................................A-7
The HREF Attribute and the A Element..................................A-7
The IMG Element and the SRC Attribute.............................. A-8
The APPLET Element................................................................A-8
The OBJECT Element................................................................A-9
Text Structure and Highlighting...................................................A-10
Text Structure Tags................................................................A-10
Text Highlighting.................................................................. A-13
HTML Forms...................................................................................A-14
The FORM Tag..........................................................................A-14
HTML Form Components................................................... A-15
Input Tags.............................................................................. A-16
Text Fields.............................................................................. A-17
Submit Buttons...................................................................... A-18
Reset Button............................................................................A-19
Checkboxes............................................................................ A-20
Radio Buttons........................................................................ A-21
Password................................................................................ A-22
Hidden Fields.........................................................................A-22
The SELECT Tag..................................................................... A-23
The TEXTAREA Tag..................................................................A-24
xv
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Table Elements................................................................................A-25
Advanced HTML............................................................................A-28
The JavaScript™ Language..................................................A-28
CSS.......................................................................................... A-30
Frames...............................................................................................A-33
Quick Reference for HTTP..............................................................B-1
Objectives........................................................................................... B-1
Additional Resources........................................................................B-2
Introduction to HTTP........................................................................B-3
Definition....................................................................................B-3
Structure of Requests.........................................................................B-4
HTTP Methods......................................................................... B-5
Request Headers...................................................................... B-6
Structure of Responses......................................................................B-8
Response Headers.................................................................... B-9
Status Codes.............................................................................B-10
CGI.....................................................................................................B-12
Set of Environment Variables................................................B-12
Data Formatting......................................................................B-14
Quick Reference for the Tomcat Server........................................C-1
Objectives.......................................................................................... C-1
Additional Resources.......................................................................C-2
Definition of the Tomcat Server......................................................C-3
Installation Instructions.................................................................. C-3
Starting and Stopping the Tomcat Server Execution...................C-4
Starting the Tomcat Server.....................................................C-4
Stopping the Tomcat Server.................................................. C-5
Configuration....................................................................................C-6
Logging and Log Files....................................................................C-10
Quick Reference for the Ant Tool..................................................D-1
Objectives.......................................................................................... D-1
Additional Resources.......................................................................D-2
Introduction to Ant...........................................................................D-3
Build File Structure...........................................................................D-4
Projects.......................................................................................D-4
Targets.......................................................................................D-4
Tasks..........................................................................................D-5
Properties..................................................................................D-5
Ant Special Features........................................................................ D-6
Patterns......................................................................................D-6
The fileset Element..............................................................D-6
Filtering.....................................................................................D-7
Basic Built-in Ant Tasks...................................................................D-8
The copy Task...........................................................................D-8
xvi Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
The delete Task......................................................................D-9
The mkdir Task......................................................................D-10
The echo Task.........................................................................D-10
The javac Task......................................................................D-11
The javadoc Task..................................................................D-11
The jar Task...........................................................................D-12
Complete Ant Build File.......................................................D-13
Executing Ant..................................................................................D-15
Install Instructions..........................................................................D-16
System Requirements............................................................D-16
Environment Setup for Ant..................................................D-16
Quick Reference for XML................................................................E-1
Objectives........................................................................................... E-1
Additional Resources........................................................................E-2
Introduction to XML..........................................................................E-3
Simple Example.........................................................................E-3
Basic Syntax........................................................................................E-4
Well-Formed XML Documents...............................................E-4
Validity and DTDs....................................................................E-5
DTD-specific Information....................................................... E-7
Schemas...............................................................................................E-9
Quick Reference for UML................................................................F-1
Additional Resources....................................................................... F-1
What Is UML?.....................................................................................F-2
Modeling in UML............................................................................. F-2
User View...................................................................................F-3
Structural View..........................................................................F-3
Behavioral View........................................................................F-3
Implementation View...............................................................F-3
Environment View....................................................................F-4
General Elements.............................................................................. F-4
Packages.....................................................................................F-5
Stereotypes.................................................................................F-6
Annotation.................................................................................F-7
Constraints.................................................................................F-7
Tagged Values...........................................................................F-8
Use Case Diagrams............................................................................F-9
Class Diagrams.................................................................................F-10
Class Nodes..............................................................................F-10
Inheritance................................................................................F-13
Interface Implementation.......................................................F-13
Association, Roles, and Multiplicity....................................F-14
Aggregation and Composition.............................................F-15
Association Classes.................................................................F-16
Other Association Elements..................................................F-18
xvii
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Object Diagrams...............................................................................F-19
Sequence Diagrams..........................................................................F-20
Collaboration Diagrams..................................................................F-22
State Diagrams..................................................................................F-23
Transitions............................................................................... F-24
Activity Diagrams............................................................................F-25
Component Diagrams.....................................................................F-27
Deployment Diagrams....................................................................F-29
Preface-xix
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Preface
About ThisCourse
Course Goal
The Web Component Development With Java™Technology course provides
students with the skills to analyze,design,develop,test,and deploy a
Web application.This course describes how to create dynamic Web
content using Java™technology servlets and JavaServer Pages™(JSP™)
technology,including custom tag library development.The course
describes how to construct small to medium scale Web applications and
deploy them onto the Tomcat server,which is the reference
implementation for the servlet and JSP specifications.The course also
summarizes best practices for integrating the Web tier with other tiers,
such as a database server and an Enterprise JavaBeans™components
(EJB™) server.
This course is designed for developers who use the Java programming
language to create components,such as servlets and customtags,for Web
applications.
Learning Objectives
Preface-xx Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Learning Objectives
When you have completed this course,you should be able to do the
following:

Develop a Web application using Java servlets

Develop a robust Web application using the Model-View-Controller
(MVC) software pattern,session management,exception handling,
declarative security,and proper concurrency control

Develop a Web application using JavaServer Pages technology

Develop a custom tag library

Develop a Web application that integrates with an n-tiered
architecture using a relational database management system
(RDBMS) or EJB server
Module-by-Module Overview
About This Course Preface-xxi
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Module-by-Module Overview
Java Servlet Application Strategies
Design and Development of N-Tier Web Applications
Introduction to Web
Application Technologies
Developing a
Simple Servlet
Developing a
Simple Servlet That
Uses HTML Forms
ConÞguring
Servlets
Sharing Resources
Using the
Servlet Context
JSPª Application Strategies
Building Reusable
Web Presentation
Components
Developing Custom JSP Tag Libraries
Developing a
Simple Custom Tag
Developing
Advanced Custom Tags
Handling Errors
in Web
ConÞguring Web
Application Security
Understanding
Web Application
Concurrency Issues
Integrating
Web Applications
With Databases
Developing
JSPª Pages
Developing
Web Applications
Using the
Model 1 Architecture
Developing
Web Applications
Using the
Model 2 Architecture
Developing
JSP Pages Using
Custom Tags
Integrating
Web Applications
With Enterprise JavaBeans
Java 2 Platform, Enterprise Edition
Components
Developing Web
Applications Using
the MVC Pattern
Developing Web
Applications Using
Session Management
Applications
Developing a Web
Application Using a
Deployment Descriptor
Topics Not Covered
Preface-xxii Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Topics Not Covered
This course does not cover the following topics.Many of these topics are
covered in other courses offered by Sun Educational Services:

Java technology programming – Covered in SL-275:The Java™
Programming Language

Object-oriented design and analysis – Covered in OO-226:
Object-Oriented Analysis and Design for Java Technology (UML)

Java 2 Platform,Enterprise Edition – Covered in SEM-SL-345:Java™
2 Platform,Enterprise Edition:Technology Overview Seminar

Enterprise JavaBeans – Covered in SL-351:Enterprise JavaBeans™
Programming
Refer to the Sun Educational Services catalog for specific information and
registration.
HowPrepared Are You?
About This Course Preface-xxiii
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
HowPrepared Are You?
To be sure you are prepared to take this course,can you answer yes to the
following questions?

Can you create Java technology applications?

Can you read and use a Java technology application programming
interface (API)?

Can you analyze and design a software system using a modeling
language like Unified Modeling Language (UML)?

Can you develop applications using a component/container
framework?
Howto Learn FromThis Course
To get the most out of the course,you should:

Ask questions

Participate in the discussions and exercises

Use the online documentation for Java™2,Standard Edition
(J2SE™),servlet,and JSP APIs

Read the servlet and JSP specifications
Introductions
Preface-xxiv Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Introductions
Now that you have been introduced to the course,introduce yourself to
the other students and the instructor,addressing the items shown on the
overhead.
Howto Use Course Materials
About This Course Preface-xxv
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Howto Use Course Materials
To enable you to succeed in this course,these course materials use a
learning module that is composed of the following components:

Goals – You should be able to accomplish the goals after finishing
this course and meeting all of its objectives.

Objectives – You should be able to accomplish the objectives after
completing a portion of instructional content.Objectives support
goals and can support other higher-level objectives.

Lecture – The instructor will present information specific to the
objective of the module.This information should help you learn the
knowledge and skills necessary to succeed with the activities.

Activities – The activities take on various forms,such as an exercise,
self-check,discussion,and demonstration.Activities help facilitate
mastery of an objective.

Visual aids – The instructor might use several visual aids to convey
a concept,such as a process,in a visual form.Visual aids commonly
contain graphics,animation,and video.
Conventions
Preface-xxvi Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Conventions
The following conventions are used in this course to represent various
training elements and alternative learning resources.
Icons
Additional resources – Indicates other references that provide additional
information on the topics described in the module.
1
2
3
Demonstration - Indicates a demonstration of the current topic is
recommended at this time.
?
!
Discussion – Indicates a small-group or class discussion on the current
topic is recommended at this time.
Note – Indicates additional information that can help students but is not
crucial to their understanding of the concept being described.Students
should be able to understand the concept or complete the task without
this information.Examples of notational information include keyword
shortcuts and minor system adjustments.
Caution – Indicates that there is a risk of personal injury from a
nonelectrical hazard,or risk of irreversible damage to data,software,or
the operating system.A caution indicates that the possibility of a hazard
(as opposed to certainty) might happen,depending on the action of the
user.
Conventions
About This Course Preface-xxvii
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
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 thejavax.servlet interface is imported to allow access to its life
cycle methods (Line 2).
Courier italicis used for variables and command-line placeholders
that are replaced with a real name or value;for example:
To delete a file,use the rm filenamecommand.
Courier italic boldis used to represent variables whose values are to
be entered by the student as part of an activity;for example:
Type chmod a+rwx filenameto grant read,write,and execute
rights for filename to world,group,and users.
Palatino italic is used for book titles,new words or terms,or words that
you want to emphasize;for example:
Read Chapter 6 in the User’s Guide.
These are called class options.
Conventions
Preface-xxviii Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Additional Conventions
Java programming language examples use the following additional
conventions:

Method names are not followed with parentheses unless a formal or
actual parameter list is shown;for example:
“The doIt method...” refers to any method called doIt.
“The doIt() method...” refers to a method called doIt that takes
no arguments.

Line breaks occur only where there are separations (commas),
conjunctions (operators),or white space in the code.Broken code is
indented four spaces under the starting code.

If a command used in the Solaris™Operating Environment is
different froma command used in the Microsoft Windows platform,
both commands are shown;for example:
If working in the Solaris Operating Environment
$CD SERVER_ROOT/BIN
If working in Microsoft Windows
C:\>CD SERVER_ROOT\BIN
1-1
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Module1
IntroductiontoWebApplication
Technologies
Objectives
This module presents Web application basics:the history of browsers and
Web servers and what they do.It also presents the main Web application
technologies and provides you with standards by which to evaluate the
advantages and disadvantages of each.
Upon completion of this module,you should be able to:

Describe Internet services

Describe the World Wide Web

Distinguish between Web applications and Web sites

Describe Java servlet technology and list three benefits of this
technology compared with traditional Common Gateway Interface
(CGI) scripting

Describe JavaServer Pages technology and list three benefits of JSP
pages technology over rival template page technologies

Describe the Java™2 Platform,Enterprise Edition (J2EE™)
Relevance
1-2 Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Relevance
?
!
Discussion – The following questions are relevant to understanding what
technologies are available for developing Web applications and the
limitations of those technologies:

What Web applications have you developed?

Were there goals you could not achieve because of the technology
you used?
Additional Resources
The following references provide additional details on the topics that are
presented briefly in this module and described in more detail later in the
course:

Java Servlets Specification.[Online].Available:
http://java.sun.com/products/servlet/

JavaServer Pages Specification.[Online].Available:
http://java.sun.com/products/jsp/

Java™2 Platform,Enterprise Edition Blueprints.[Online].Available:
http://java.sun.com/j2ee/blueprints/

Tomcat download page.[Online].Available:
http://jakarta.apache.org/tomcat

HTTP RFC#2616 [Online].Available:
http://www.ietf.org/rfc/rfc2616.txt

Shishir Gundavarama,CGI Programming on the World Wide Web,
Sebastopol:O’Reilly & Associates,Inc.,1996.
Internet Services
Introduction to Web Application Technologies 1-3
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Internet Services
It is important to have a fundamental understanding of the components
of the Internet:the clients,the servers,and the networks used to connect
them.
The Internet Is a Network of Networks
In the early days of computer networking,local area networks (LANs)
were created to share resources among members of a particular
institution.These networks were constrained to short distances.This is
illustrated in Figure 1-1.
Figure 1-1 Pre-Internet Networking Environment
￿￿￿
Internet Services
1-4 Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Soon different institutions started connecting their networks to other
networks as well.This enabled increased collaboration among academics
and scientists across the world.The resulting “network of networks” is
shown in Figure 1-2.
Figure 1-2 Simplified Illustration of the Internet
Connectivity is just one element of what makes the Internet work.
Another element is the language of communication between computers.
This is called a protocol.
￿￿￿￿￿￿￿￿
￿￿￿
￿￿￿
￿￿￿
￿￿￿
Internet Services
Introduction to Web Application Technologies 1-5
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Networking Protocol Stack
Servers and clients that interact on a network are connected by several
layers of hardware and software working together.These layers are
shown in Figure 1-3.
Figure 1-3 Network Layers of Physical and Software Elements
Note – Figure 1-3 is based on the International Organization for
Standardization (ISO) seven-layer network stack,but leaves out several
features that are unimportant to this discussion.This is often referred to as
the “4 layer” model.
To retrieve a file from a remote server,a user would use a File Transfer
Protocol (FTP) program.This required several commands entered into the
FTP program to log in to the remote server,navigate to the necessary
directory,and then retrieve the file.This is done using a command line
interface.
FTP works by sending messages between the client and the remote server.
These messages may be brief text commands or files of any size and type.
FTP is built on top of the Transmission Control Protocol (TCP),which
guarantees that the message is transmitted across the network without
any errors.
TCP is built on top of the Internet Protocol (IP),which provides a set of
low-level services.TCP can send a message of any size,but IP uses a
packet mechanism that sends data in a fixed size.IP does not guarantee
delivery of the packet.
￿￿￿￿ ￿￿￿￿￿￿￿￿ ￿￿￿￿￿￿￿￿ ￿￿￿￿￿
￿￿￿￿ ￿￿ ￿￿￿ ￿￿￿￿￿￿￿￿￿￿￿ ￿￿￿￿￿
￿￿￿￿￿￿￿￿￿￿￿￿ ￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿ ￿￿￿￿￿ ￿￿￿￿ ￿￿ ￿￿￿ ￿￿
￿￿￿ ￿￿￿￿￿￿￿￿ ￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿ ￿￿￿￿￿￿￿￿ ￿￿￿￿
￿￿￿ ￿￿￿￿￿
￿￿￿
￿￿￿
￿￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿ ￿￿￿￿￿
Internet Services
1-6 Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
IP is built on top of a variety of physical devices.These devices include
the cables connecting computers,routers,switches,and so on.
FTP is just one application of an Internet service.There are scores of
standard services,including email,telnet,time,system status,and so on.
Another example protocol stack is shown in Figure 1-4.
Figure 1-4 Another Networking Layer Example
You can also create proprietary protocols built on top of TCP.
￿￿￿￿￿￿￿￿￿￿￿￿ ￿￿￿￿￿￿￿￿￿￿
￿￿￿￿￿ ￿￿￿ ￿￿ ￿￿￿
￿￿￿￿￿￿￿￿￿ ￿￿￿￿￿￿￿￿ ￿￿￿￿￿￿￿￿￿
￿￿ ￿￿￿￿￿￿￿￿￿￿￿ ￿￿￿￿￿ ￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿￿￿￿
￿￿￿￿
￿￿￿
￿￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿ ￿￿￿￿￿
Internet Services
Introduction to Web Application Technologies 1-7
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Client-Server Architecture
In traditional Internet services,there is a client host and a server host.The
client makes requests of the server and the server responds to those
requests.TCP/IP is the most common transmission protocol stack.The
fundamental client-server architecture is shown in Figure 1-5.
Figure 1-5 Fundamental Client-Server Architecture
￿￿￿￿ ￿￿￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿ ￿￿￿￿￿￿
￿￿￿￿￿￿
￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿ ￿￿￿￿￿￿￿￿￿￿
Hypertext Transfer Protocol
1-8 Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Hypertext Transfer Protocol
The Hypertext Transfer Protocol (HTTP) is similar to FTP because it is a
protocol to transfer files fromthe server to the client.HTTP was created in
conjunction with the related Hypertext Markup Language (HTML) standard.
There is one fundamental difference between FTP and HTTP:HTTP
supports only one request per connection.This means that the client
connects to the server to retrieve one file and then disconnects.This
mechanism allows more users to connect to a given server over a period
of time.
HTML is a document display language that allows users to link fromone
document to another.For example,a paper on charmed quarks stored at
European Organization for Nuclear Research (CERN) in Switzerland
could include a “See also” link to a paper on strange quarks stored on a
computer in Fargo,North Dakota.
HTML also permits images and other media objects to be embedded in an
HTML document.The media objects are stored in files on a server.HTTP
also retrieves these files.HTTP can therefore be used to transmit any file
that conforms to the Multipurpose Internet Mail Extensions (MIME)
specification.
Web Browsers and Web Servers
To view an HTML document with rich media content,a graphical user
interface (GUI) was built on top of the client-side HTTP.This GUI is called
a Web browser.The server-side HTTP component is called a Web server.
Several companies have developed Web browsers and Web servers;some
have developed both.The first Web server was a process called httpd;the
first widely used browser was Mosaic,created by National Center for
Supercomputing Applications (NCSA).
Hypertext Transfer Protocol
Introduction to Web Application Technologies 1-9
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Mosaic became extremely popular,and various companies developed
their own Web browsers.The early development of Web browsers and
Web servers is illustrated in Figure 1-6.
Figure 1-6 Early Web Browser and Web Server Vendors
HTTP Client-Server Architecture
For every exchange over the Web using HTTP,there is a request and a
response.This is illustrated in Figure 1-7.
Figure 1-7 HTTP Client-Server Architecture
￿￿￿￿￿￿￿￿ ￿￿￿￿￿￿￿￿￿
￿￿￿￿
￿￿￿ ￿￿￿￿￿￿ ￿￿￿￿￿￿
httpd
￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿ ￿￿￿￿￿￿
￿￿￿￿￿￿
httpd
￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿ ￿￿￿￿￿￿￿￿
￿￿￿
￿￿
￿￿
￿￿￿￿ ￿￿￿￿￿￿￿￿￿￿￿
￿￿￿ ￿￿￿￿￿￿
￿￿￿￿￿￿
￿￿￿￿￿￿￿
￿￿￿￿￿￿
￿￿￿￿￿￿￿
￿￿￿￿￿￿
￿￿￿￿￿￿￿
￿￿￿￿￿￿￿
httpd
￿￿￿￿￿￿￿￿
￿￿￿￿￿￿ ￿￿￿￿￿￿￿￿ ￿
￿￿￿￿￿￿ ￿￿￿￿￿￿￿￿￿ ￿
Hypertext Transfer Protocol
1-10 Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
The Web browser sends a single request to the server.The Web server
determines which file is being requested and sends the data in that file
back as the response.The browser interprets the response and represents
the content on the screen.
The request information consists of the file or other resource that the user
wants and information about the browser.The response information
contains the requested file and other information.The request is typically
in plain text;the response can be plain text or part plain text,part binary
data.(Graphics,for example,must be sent in binary form.)
The Structure of a Web Site
A Web site is a collection of HTML pages and other media files,which
contain all the content that is visible to the user on a given Web server.
These files are stored on the server and might include a complex directory
hierarchy.The Web site is composed of that directory hierarchy.An
example of a Web site is shown in Figure 1-8.
Figure 1-8 An Example Web Site Directory Structure
Note – The index.html file is a special file used when the user requests a
Uniform Resource Locator (URL) that ends in a slash character (/).The
Web server presents the user with a directory listing for that URL unless
an index.html file exists in that directory.If that is the case,then the Web
server sends the index.html file as the response to the original URL.
￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿
Hypertext Transfer Protocol
Introduction to Web Application Technologies 1-11
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
UniformResource Locator
A URL is a canonical name that locates a specific resource on the Internet.
It consists of:
protocol://host:port/path/file
For example:
http://www.soccer.org:80/league/Spring2001.html
The pathelement includes the complete directory structure path to find
the file.The port number is used to identify the TCP port that is used by
the protocol on the server.If the port number is the standard port for the
given protocol,then that number can be ignored in the URL.For example,
port 80 is the default HTTP port:
http://www.soccer.org/league/Spring2001.html
Web Applications
1-12 Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Web Applications
Very early in the development of HTML,the designers created a
mechanism to permit a user to invoke a program on the Web server.This
was called the Common Gateway Interface (CGI).When a Web site includes
CGI processing,this is called a Web application.
CGI Programs on the Web Server
Usually,the browser needs to send data to the CGI programon the server.
The CGI specification defines how the data is packaged and sent in the
HTTP request to the server.This data is usually typed into the Web
browser in an HTML form.
The URL determines which CGI program to execute.This might be a
script or an executable file.The CGI program parses the CGI data in the
request,processes the data,and generates a response (usually an HTML
page).The CGI response is sent back to the Web server,which wraps the
response in an HTTP response.The HTTP response is sent back to the
Web browser.An example Web application architecture that uses CGI
programs is illustrated in Figure 1-9.
Figure 1-9 Web Server Architecture With CGI Programs
User workstation
Web server
<HTML>
</HTML>
<HTML>
</HTML>
<HTML>
</HTML>
CGI scrip
t
Browser
httpd
<<HTTP>>
<<HTTP request> >
<<HTTP response> >
Database
Web Applications
Introduction to Web Application Technologies 1-13
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Execution of CGI Programs
At runtime,a CGI program is launched by the Web server as a separate
operating system (OS) shell.The shell includes an OS environment and
process to execute the code of the CGI program,which resides within the
server’s file system.The runtime performance of one CGI request is
shown in Figure 1-10.
Figure 1-10 Running a Single Instance of a CGI Program
However,each new CGI request launches a new operating system shell
on the server.The runtime performance of multiple CGI requests is shown
in Figure 1-11.
Figure 1-11 Running Multiple Instances of a CGI Program
￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿ ￿￿￿￿
httpd
￿￿￿
￿￿￿￿￿￿￿
￿￿￿
￿￿￿￿￿
￿￿￿￿￿￿￿
￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿ ￿￿￿￿
httpd
￿￿￿￿￿￿￿
￿￿￿￿￿￿￿
￿￿￿
￿￿￿￿￿￿￿
￿￿￿
￿￿￿￿￿
￿￿￿
￿￿￿￿￿
￿￿￿
￿￿￿￿￿
￿￿￿￿￿￿￿
Web Applications
1-14 Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Advantages and Disadvantages of CGI Programs
CGI programs have four advantages:

Programs can be written in a variety of languages,although they are
primarily written in Perl.

A buggy CGI program will not crash the Web server.

Programs are easy for a Web designer to reference.When the script is
written,the designer can reference it in one line in a Web page.

Because CGI programs execute in their own OS shell,these programs
do not have concurrency conflicts with other HTTP requests
executing the same CGI program.

All service providers support CGI programs.
CGI program also have distinct disadvantages:

The response time of CGI programs is very high,because CGI
programs execute in their own OS shell.The creation of an OS shell
is a heavyweight activity for the OS.

CGI is not scalable.If the number of people accessing the Web
application increases from 50 to 5000,for example,CGI cannot be
adapted to handle the load.There is a limit on the number of
separate operating system processes a computer can run.

The languages for CGI are not always secure,or object oriented.

The CGI script has to generate an HTML response,so the CGI code
is mingled with HTML.This is not good separation of presentation
and business logic.

Scripting languages are often platform dependent.
Because of these disadvantages,developers need other CGI solutions.
Servlets is the Java technology solution used to process CGI data.
Java Servlets
Introduction to Web Application Technologies 1-15
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Java Servlets
Sun Microsystems developed servlets as an advance over traditional CGI
technology.A Java servlet is a Java technology program that,like a CGI
program,runs on the server.The types of tasks that you can run with
servlets are similar to those you can run with CGI;however,the
underlying executing architecture is different.
As with CGI scripts,you can write servlets that can understand HTTP
requests,generate the response dynamically (possibly querying databases
to fulfill the request),and then send a response containing an HTML page
or document to the browser.
Servlets on the Web Server
Unlike CGI programs,servlets run within a component container
architecture.This container is called the Web container (the new term for
the servlet engine).The Web container is a Java™virtual machine
(JVM™) that supplies an implementation of the servlet application
programming interface (API).Servlet instances are components that are
managed by the Web container to respond to HTTP requests.This
architecture is shown in Figure 1-12.
Figure 1-12 Web Server Architecture With Java Servlets
￿￿￿￿ ￿￿￿￿￿￿￿￿￿￿￿
￿￿￿ ￿￿￿￿￿￿
￿￿￿￿￿￿
￿￿￿￿￿￿￿
￿￿￿￿￿￿
￿￿￿￿￿￿￿
￿￿￿￿￿￿
￿￿￿￿￿￿￿
￿￿￿ ￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿
httpd
￿￿￿￿￿￿￿￿
￿￿￿￿￿￿ ￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿ ￿￿￿￿￿￿￿￿￿￿
￿￿￿￿￿￿￿
￿￿￿￿￿￿￿￿
Java Servlets
1-16 Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Note – In some architectures,the Web container acts as a standalone
HTTP service;in other architectures,the HTTP service forwards requests
to be processed by the Web container.
Execution of Java Servlets
The basic processing steps for Java servlets are quite similar to the ones
for CGI.However,the servlet runs as a thread in the Web container
instead of in a separate OS process.The Web container itself is an OS
process,but it runs as a service and is available continuously as opposed
to a CGI script in which a new OS process (and shell) is created for each
request.Aservlet executes as a thread within the Web container’s process.
This is illustrated in Figure 1-13.
Figure 1-13 Running a Single Instance of a Servlet
￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿ ￿￿￿￿
￿￿￿
httpd
￿￿￿￿￿￿￿
￿￿￿￿￿￿
￿￿￿￿￿￿￿
Java Servlets
Introduction to Web Application Technologies 1-17
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
When the number of requests for a servlet rises,no additional instances of
the servlet or operating system processes are created.Each request is
processed concurrently using one Java thread per request.The effect of
additional clients requesting the same servlet is shown in Figure 1-14.
Figure 1-14 Running Multiple Instances of a Servlet
￿￿￿￿￿￿
￿￿￿￿￿￿￿￿￿ ￿￿￿￿
￿￿￿
httpd
￿￿￿￿￿￿￿
￿￿￿￿￿￿￿
￿￿￿￿￿￿￿
￿￿￿￿￿￿
￿￿￿￿￿￿
￿￿￿￿￿￿
￿￿￿￿￿￿￿
Java Servlets
1-18 Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Advantages and Disadvantages of Java Servlets
Servlets have the following advantages:

Each request is run in a separate thread,so servlet request processing
is significantly faster than traditional CGI processing.

Servlets are scalable.Many more requests can be executed because
the Web container uses a thread rather than an operating system
process,which is a limited system resource.

Servlets are robust and object oriented.You have all the capabilities
of the Java programming language when you write the servlet,
instead of the capabilities of Perl or whatever language you use to
write the CGI script.

Servlets can only be written in the Java programming language,
which makes themeasy to write if you know the Java programming
language.However,using servlets to generate pages with dynamic
content requires application development expertise.

Servlets are platform independent,because they are written in the
Java programming language.

Servlets have access to logging capabilities.Most CGI programs do
not.

The Web container provides additional services to the servlets,such
as error handling and security.
Servlets have the following disadvantages:

Servlets often contain both business logic and presentation logic.
Presentation logic is anything that controls how the application
presents information to the user.Generating the HTML response
within the servlet code is presentation logic.Business logic is
anything that manipulates data in order to accomplish something,
such as storing data.

Servlets must handle concurrency issues.
Mixing presentation and business logic means that whenever a Web page
changes (which can be monthly or weekly for many applications) the
servlets must be rewritten,recompiled,and redeployed.
This disadvantage led to the development of template pages,including
JavaServer Pages technology.
Template Pages
Introduction to Web Application Technologies 1-19
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Template Pages
JSP pages are just one way of implementing the concept of HTML pages
with embedded code,or template pages.There are three mainstream
technologies available for creating HTML with embedded code:PHP from
Apache,Active Server Pages (ASP) from Microsoft,and JSP from Sun
Microsystems.PHP and ASP,however,work only with proprietary Web
servers.
With JSP pages,Java technology code fragments are embedded in an
HTML-like file.This code is executed at runtime to create dynamic
content.An example JSP template page is shown in Code 1-1.
Code 1-1 An Example JSP Template Page
01 <HTML>
2
3 <HEAD>
4 <TITLE>Example JSP Page</TITLE>
5 </HEAD>
6
7 <BODY BGCOLOR=ÕwhiteÕ>
8
9 <B>Table of numbers squared:</B>
10
11 <TABLE BORDER=Õ1Õ CELLSPACING=Õ0Õ CELLPADDING=Õ5Õ>
12 <TR><TH>number</TH><TH>squared</TH></TR>
13 <% for ( int i=0; i<10; i++ ) { %>
14 <TR><TD><%= i %></TD><TD><%= (i * i) %></TD></TR>
15 <% } %>
16 </TABLE>
17
18 </BODY>
19
20 </HTML>
Template Pages
1-20 Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
This example page generates an HTML table with the numbers 0 through
9 in the first column and the corresponding squares in the second column.
A screen shot from the generated page appears in Figure 1-15.
Figure 1-15 Output of Example Template Page
Other Template Page Technologies
The following three code examples demonstrate the same “for loop” in
three template page technologies:PHP,ASP,and JSP,respectively.This is
shown to demonstrate how similar these template page technologies are.
Code 1-2 PHP (PHP Hypertext Preprocessor)
<? for ( $i=0; $i<10; $i++ ) { ?>
<TR><TD><? echo $i ?></TD><TD><? echo ($i * $i) ?></TD></TR>
<? } ?>
Code 1-3 ASP (Active Server Pages)
<% FOR I = 0 TO 10 %>
<TR><TD><%= I %></TD><TD><%= (I * I) %></TD></TR>
<% NEXT %>
Template Pages
Introduction to Web Application Technologies 1-21
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Code 1-4 JSP (JavaServer Pages)
<% for ( int i=0; i<10; i++ ) { %>
<TR><TD><%= i %></TD><TD><%= (i * i) %></TD></TR>
<% } %>
JavaServer Pages Technology
All template page technologies have the same fundamental structure:an
HTML page that a Web designer can easily create,with special tags,
which indicate to the Web server that code needs to be executed at
request-time.This course focuses only on JSP pages.
JSP pages are the opposite of servlets.Instead of Java technology code that
contains HTML,template pages are HTML that contains Java technology
code.JSP pages are converted by the Web container into a servlet instance.
That servlet then processes each request to that JSP page.This feature of
JSP pages is an advantage over other template page technologies because
JSP pages are compiled into Java technology byte code whereas ASP (or
PHP) pages are interpreted on each HTTP request.
The JSP page runs as a servlet;everything that you can do in a servlet you
can do in a JSP page.The main difference is that a JSP page should focus
on the presentation logic of the Web application
Template Pages
1-22 Web Component Development With Javaª Technology
Copyright 2002 Sun Microsystems,Inc.All Rights Reserved.Enterprise Services,Revision A.1
Advantages and Disadvantages of JavaServer Pages
Because JSP pages are translated into Java servlets,JSP technology has all
of the advantages of servlets:

Web applications using JSP pages have high performance and
scalability because threads are used rather than operating system’s
shells or processes.

JSP technology is built on Java technology,so it is platform
independent.

JSP scripting elements can be written in the Java language so that JSP
pages can take advantage of the object-oriented language and all of
its APIs.
JSP technology has the following disadvantages:

Often JSP pages contain both presentation logic and business logic.

JSP pages must also consider concurrency issues.

JSP pages are difficult to debug.
A properly designed Web application should use servlets and JSP pages
together to achieve separation of concerns.
The Model 2 Architecture
Using servlets and JSP pages together facilitates proper separation of
concerns using a variation on the Model-View-Controller (MVC) design
pattern.A Web application designed using the Model 2 architecture has the
following features:

Aservlet acts as the Controller,which verifies formdata,updates the
Model with the formdata,and selects the next View as the response.

A JSP page acts as the View,which renders the HTML response,
retrieving data from the Model necessary to generate the response,
and provides HTML forms to permit user interaction.

Java technology classes act as the Model,which implements the
business logic of the Web application.
Template Pages
Introduction to Web Application Technologies 1-23