Tải bản đầy đủ (.pdf) (426 trang)

Tài liệu Beginning JSP, JSF and Tomcat Java Web Development docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (10.79 MB, 426 trang )


Beginning JSP, JSF and
Tomcat
Java Web Development











Giulio Zambon

www.it-ebooks.info
ii
Beginning JSP, JSF and Tomcat
Copyright © 2012 by Giulio Zambon
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is
concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting,
reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval,
electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter
developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or
material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use
by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of
the Copyright Law of the Publisher's location, in its current version, and permission for use must always be obtained
from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations
are liable to prosecution under the respective Copyright Law.


ISBN 978-1-4302-4623-7
ISBN 978-1-4302-4624-4 (eBook)
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion
and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary
rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither
the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may
be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
President and Publisher: Paul Manning
Lead Editor: Steve Anglin
Developmental Editor: Douglas Pundick, Ralph Moore
Technical Reviewer: Boris Minkin, Manuel Joran Elera
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel, Jonathan
Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff
Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt
Wade, Tom Welsh
Coordinating Editors: Katie Sullivan
Copy Editor: Michael Sandlin
Compositor: Bytheway Publishing Services
Indexer: SPi Global
Artist: SPi Global
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail , or visit
www.springeronline.com.
For information on translations, please e-mail , or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook

versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook
Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available to readers at
www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-
code.
www.it-ebooks.info
iii
Contents at a Glance

About the Author xiv

About the Technical Reviewers xv

Chapter 1: Introducing JSP and Tomcat 1

Chapter 2: JSP Elements 19

Chapter 3: JSP Application Architectures 49

Chapter 4: JSP in Action 79

Chapter 5: XML and JSP 121

Chapter 6: Databases 159

Chapter 7: JavaServer Faces 2.2 189

Chapter 8: JSF and eshop 231

Chapter 9: Tomcat 259


Chapter 10: eshop* 281

Appendix A: The Web Page 317

Appendix B: SQL Practical Introduction 379

Appendix C: Abbreviations and Acronyms 405

Index 409
www.it-ebooks.info
iv
Contents

About the Author xiv

About the Technical Reviewers xv

Chapter 1: Introducing JSP and Tomcat 1
Installing Java 3
Java Test 5
Installing Tomcat 6
Simple Tomcat Test 8
What Is JSP? 9
Viewing a JSP Page 10
Hello World! 12
Listing the HTML-Request Parameters 16
Summary 17

Chapter 2: JSP Elements 19

Introduction 19
Scripting Elements and Java 20
Scriptlets 20
Expressions 20
Declarations 21
Data Types and Variables 21
Objects and Arrays 23
Operators, Assignments, and Comparisons 24
www.it-ebooks.info
#/.4%.43
v
Selections 25

Iterations 26
Implicit Objects 27
The application Object 27
The config Object 30
The exception Object 31
The out Object 32
The pageContext Object 34
The request Object 34
The response Object 43
The session Object 43
Directive Elements 44
The page Directive 44
The include Directive 47
The taglib Directive 47
Summary 47

Chapter 3: JSP Application Architectures 49

The Model 1 Architecture 49
The Model 2 Architecture 50
The E-bookshop Home Page 52
The E-bookshop Servlet 54
More on E-bookshop 57
E-bookshop’s Folder Structure 60
Eclipse 63
Creating a New Web Project 67
Importing a WAR file 69
Eclipse Occasional Bugs 70
www.it-ebooks.info
#/.4%.43
vi
A Better Online Bookshop 70
Objects and Operations 71
The Customer Interface 72
The E-shop Architecture 73
The Model 73
The Controller 74
The View 76
Summary 77

Chapter 4: JSP in Action 79
JSP Standard Actions 79
Actions: forward, include, and param 79
Action: useBean 82
Actions: setProperty and getProperty 84
Action: text 87
Actions: element, attribute, and body 87
Actions: plugin, params, and fallback 88

Comments and Escape Characters 90
JSP’s Tag Extension Mechanism 90
Bodyless Custom Actions 91
Bodied Custom Actions 95
Tag Files 98
JSTL and EL 103
JSP Expression Language 103
JSP Standard Tag Library 107
The Core Library 109
The i18n Library: Writing Multi-Lingual Applications 112
Summary 119
www.it-ebooks.info
#/.4%.43
vii

Chapter 5: XML and JSP 121
The XML Document 122
Defining Your Own XML Documents 123
XML DTDs 124
XML Schemas 124
Validation 132
JSTL-XML and XSL 139
XPath 139
An XPath Example 143
x:parse 145
XSLT: Transformation from One XML Format to Another 146
XSLT: Transformation from XML to HTML 147
XSL Transformation: Browser Side vs. Server Side 148
x:transform and x:param 152
JSP in XML Syntax 153

Summary 157

Chapter 6: Databases 159
MySQL 159
MySQL Test 161
MySQL/Tomcat Test 165
Database Basics 168
SQL Scripts 170
Java API 171
Connecting to the Database 172
Accessing Data 173
Transactions 176
DB Access in E-shop 176
www.it-ebooks.info
#/.4%.43
viii
What about the XML Syntax? 180
Possible Alternatives to MySQL 184
Summary 187

Chapter 7: JavaServer Faces 2.2 189
The simplef Application 189
An Alternative to <managed-bean> 195
The simplefx and simpleh Applications 195
The JSF Life Cycle 197
Event Handling 199
The JSF Tag Libraries 199
The html Library 200
The core Library 205
The facelet Library 215

The composite Library 224
Summary 229

Chapter 8: JSF and eshop 231
eshopf 231
The Top Menu 232
The Left Menu (part 1) 233
The Shop Manager 235
The Left Menu (part 2) 236
The Checkout Page 237
web.xml 238
Using and Creating Converters 240
Writing the Converter in Java 241
Registering the Converter with the Application 243
Using the Converter 243
www.it-ebooks.info
#/.4%.43
ix
Using and Creating Validators 243
Built-In Validators 244
Application-Level Validation 245
Custom Validators 246
Validation Methods in Backing Beans 247
Creating Custom Components 248
Component 249
Renderer 251
Tag 253
Inline Renderer 256
faces-config.xml 257
Summary 257


Chapter 9: Tomcat 259
Tomcat’s Architecture and server.xml 259
Context 260
Connector 261
Host 261
Engine 262
Service 262
Server 262
Listener 263
Global Naming Resources 263
Realm 263
Cluster 263
Valve 264
Loader and Manager 264
Directory Structure 264
conf 265
www.it-ebooks.info
#/.4%.43
x
lib 265

logs 266
webapps 266
work 266
Logging the Requests 267
Tomcat on Port 80 269
Creating a Virtual Host 269
HTTPS 271
Application Deployment 276

Summary 279

Chapter 10: eshop* 281
The eshop Application 281
What Happens When the Application Starts 283
Handling Requests for Book Selection and Book Search 286
Displaying the Book Details 287
Managing the Shopping Cart 288
Accepting an Order 289
Providing the Payment Details 299
The eshopx Application 300
Style Sheet 301
web.xml 302
JSP Documents 303
Custom Tags and TLD 306
The eshopf Application 308
web.xml and context.xml 309
Style Sheet 310
JSP Documents 312
www.it-ebooks.info
#/.4%.43
xi
Java Modules 313

Summary 315

Appendix A: The Web Page 317
The WWW Network 317
URLs, Hosts, and Paths 320
XHTML vs HTML 322

XHTML/HTML Elements 324
HTML5 327
HTML Documents 329
Standard Attributes 331
Core Attributes 331
Language Attributes 332
Keyboard Attributes 333
Event Attributes 333
Object Event Attributes 333
Form Event Attributes 334
Keyboard Event Attributes 334
Mouse Event Attributes 335
Tables 335
Table Structure 337
Table Width 337
Table Borders 337
Row and Cell Alignment 339
Columns 342
Column Groups 343
Table Header, Body, and Footer 344
Input Forms 345
www.it-ebooks.info
#/.4%.43
xii
Buttons and Images 348

Lists 350
Image Maps 351
Splitting an Image with a Table 351
Using an Image Map with a Table or a List 353

Using an Image Map with Areas 356
The Bottom Line 357
Cascading Style Sheets 357
Style Syntax 358
Placing Styles 359
HTML Elements div and span 360
Using a Style Sheet to Implement Tabs 361
JavaScript 364
Placing JavaScript Inside a Web Page 364
Responding to Events 365
Checking and Correcting Dates 365
Animation: Ticker Tape 370
Animation: Bouncing Balls 373

Appendix B: SQL Practical Introduction 379
SQL Terminology 379
Transactions 380
Conventions 382
Statements 382
The WHERE Condition 384
Data Types 385
SELECT 388
CREATE DATABASE 394
www.it-ebooks.info
#/.4%.43
xiii
CREATE TABLE 395

CREATE INDEX 397
CREATE VIEW 398

INSERT 398
DROP 399
DELETE 399
ALTER TABLE 399
UPDATE 400
SET TRANSACTION and START TRANSACTION 400
COMMIT and ROLLBACK 401
Reserved SQL Keywords 401

Appendix C: Abbreviations and Acronyms 405

Index 409

www.it-ebooks.info
xiv
About the Author


Giulio Zambon’s first love was physics, but he decided to dedicate himself to
software development more than 30 years ago: back when computers were still
made of transistors and core memories, programs were punched on cards, and
Fortran only had arithmetic IFs. Over the years, he learned a dozen computer
languages and worked with all sorts of operating systems. His specific interests
were in telecom and real-time systems, and he managed several projects to their
successful completion.
In 2001 Giulio founded his own company offering computer telephony
integration (CTI) services, and he used JSP and Tomcat exclusively to develop the
web side of the service platform. Back in Australia after many years in Europe, he
now dedicates himself to writing software to generate and solve numeric puzzles.
His web site, is written in JSP on his dedicated server, which, unsurprisingly,

runs Tomcat!

www.it-ebooks.info
xv
About the Technical Reviewers


Boris Minkin is a senior technical architect at a major financial corporation. He
has more than 20 years of experience working in various areas of information
technology and financial services. Boris obtained his master’s degree in
information systems at Stevens Institute of Technology, New Jersey. His
professional interests are in Internet technology, service-oriented architecture,
enterprise application architecture, multi-platform distributed applications,
cloud, distributed caching, Java, grid, and high performance computing. You can
contact Boris at





Manuel Jordan Elera is an autodidactic developer and researcher who enjoys
learning new technologies for his own experiments and creating new
integrations. Manuel won the 2010 Springy Award-Community Champion. In his
limited free time, he reads the Bible and composes music on his guitar. Manuel is
a senior member in the Spring Community Forums known as dr_pompeii and a
technical reviewer for important books about Spring Source projects, all
published by Apress. Read more and contact him through his blog at
and follow him on his Twitter account,
@dr_pompei.
www.it-ebooks.info

-->

×