O'Reilly - JavaServer Pages 2nd Edition.pdf

(3090 KB) Pobierz
JavaServer Pages™, 2nd Edition
3132401.001.png
JavaServer Pages™, 2nd Edition
Hans Bergsten
Publisher: O'Reilly
August 2002
ISBN: 0-596-00317-X, 684 pages
Filled with useful examples and the depth, clarity, and attention to detail that made the first
edition so popular with web developers, JavaServer Pages , 2nd Edition is completely revised
and updated to cover the substantial changes in the 1.2 version of the JSP specifications, and
includes coverage of the new JSTL Tag libraries-an eagerly anticipated standard set of JSP
elements for the tasks needed in most JSP applications, as well as thorough coverage of
Custom Tag Libraries.
Copyright © 2002, 2001 O'Reilly & Associates, Inc. All rights reserved.
Printed in the United States of America.
Published by O'Reilly & Associates, Inc., 1005 Gravenstein Highway North, Sebastopol,
CA 95472.
O'Reilly & Associates books may be purchased for educational, business, or sales
promotional use. Online editions are also available for most titles (http://safari.oreilly.com).
For more information contact our corporate/institutional sales department: 800-998-9938 or
corporate@oreilly.com.
Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered
trademarks of O'Reilly & Associates, Inc. Java™ and all Java-based trademarks and logos are
trademarks or registered trademarks of Sun Microsystems, Inc., in the United States and other
countries. O'Reilly &x Associates, Inc., is independent of Sun Microsystems. Openwave,
the Openwave logo, and UP.SDK are trademarks of Openwave Systems Inc. All rights
reserved.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in this book, and O'Reilly
& Associates, Inc. was aware of a trademark claim, the designations have been printed in caps
or initial caps. The association between the image of a grey wolf and the topic of JavaServer
Pages is a trademark of O'Reilly & Associates, Inc.
While every precaution has been taken in the preparation of this book, the publisher and
the author assume no responsibility for errors or omissions, or for damages resulting from the
use of the information contained herein.
Table of Contents
Table of Contents
Preface ................................................................................................................................... 1
What's in the Book ............................................................................................................. 2
Readers of the First Edition................................................................................................ 2
Audience............................................................................................................................. 3
Organization ....................................................................................................................... 4
About the Examples ........................................................................................................... 8
Conventions Used in This Book......................................................................................... 8
How to Contact Us ............................................................................................................. 9
Acknowledgments for First Edition ................................................................................... 9
Acknowledgments for Second Edition............................................................................. 10
Part I: JSP Application Basics .............................................................................................. 12
Chapter 1. Introducing JavaServer Pages ....................................................................... 13
1.1 What Is JavaServer Pages?......................................................................................... 13
1.2 Why Use JSP? ............................................................................................................ 14
1.2.1 Embedding Dynamic Elements in HTML Pages ................................................ 14
1.2.2 Compilation ......................................................................................................... 15
1.2.3 Using the Right Person for Each Task ................................................................ 15
1.2.4 Integration with Enterprise Java APIs................................................................. 16
1.2.5 Other Solutions.................................................................................................... 16
1.2.6 The JSP Advantage ............................................................................................. 18
1.3 What You Need to Get Started................................................................................... 18
Chapter 2. HTTP and Servlet Basics................................................................................ 20
2.1 The HTTP Request/Response Model ......................................................................... 20
2.1.1 Requests in Detail................................................................................................ 21
2.1.2 Responses in Detail ............................................................................................. 23
2.1.3 Request Parameters ............................................................................................. 25
2.1.4 Request Methods ................................................................................................. 25
2.2 Servlets ....................................................................................................................... 27
2.2.1 Advantages over Other Server-Side Technologies ............................................. 27
2.2.2 Servlet Containers ............................................................................................... 29
2.2.3 Servlet Contexts and Web Applications.............................................................. 30
Chapter 3. JSP Overview................................................................................................... 32
3.1 The Problem with Servlets ......................................................................................... 32
3.2 The Anatomy of a JSP Page ....................................................................................... 34
3.3 JSP Processing............................................................................................................ 34
3.3.1 JSP Elements ....................................................................................................... 35
3.4 JSP Application Design with MVC ........................................................................... 37
Chapter 4. Setting Up the JSP Environment ................................................................... 39
4.1 Installing the Java Software Development Kit........................................................... 39
4.2 Installing the Tomcat Server ...................................................................................... 40
4.2.1 Windows Platforms ............................................................................................. 41
4.2.2 Unix Platforms (Including Linux and Mac OS X) .............................................. 43
4.3 Testing Tomcat........................................................................................................... 43
4.4 Installing the Book Examples .................................................................................... 45
4.5 Example Web Application Overview ........................................................................ 47
i
Table of Contents
Part II: JSP Application Development................................................................................. 49
Chapter 5. Generating Dynamic Content ........................................................................ 50
5.1 Creating a JSP Page.................................................................................................... 50
5.2 Installing a JSP Page .................................................................................................. 51
5.3 Running a JSP Page.................................................................................................... 52
5.4 Using JSP Directive Elements.................................................................................... 53
5.4.1 JSP Comments..................................................................................................... 54
5.5 Using Template Text .................................................................................................. 55
5.6 Using JSP Action Elements........................................................................................ 55
5.6.1 JSP Standard Tag Library ................................................................................... 57
Chapter 6. Using JavaBeans Components in JSP Pages ................................................ 60
6.1 What Is a Bean?.......................................................................................................... 60
6.2 Declaring a Bean in a JSP Page ................................................................................. 62
6.3 Reading Bean Properties ............................................................................................ 62
6.3.1 Using the <jsp:getProperty> Action.................................................................... 63
6.3.2 Using the JSTL Expression Language ................................................................ 64
6.3.3 Including Images with JSP .................................................................................. 65
6.4 Setting Bean Properties .............................................................................................. 65
6.4.1 Automatic Type Conversions.............................................................................. 67
Chapter 7. Using Custom Tag Libraries and the JSP Standard Tag Library ............. 68
7.1 What Is a Custom Tag Library? ................................................................................. 68
7.2 Installing a Custom Tag Library ................................................................................ 70
7.3 Declaring a Custom Tag Library................................................................................ 70
7.3.1 Identifying a Custom Tag Library in a JSP 1.1 Container .................................. 72
7.4 Using Actions from a Tag Library ............................................................................. 73
7.4.1 Setting Action Attribute Values .......................................................................... 74
7.4.2 The JSP Standard Tag Library ............................................................................ 75
7.4.3 Using Beans or Custom Actions ......................................................................... 78
Chapter 8. Processing Input and Output ......................................................................... 80
8.1 Reading Request Parameter Values ........................................................................... 80
8.1.1 Accessing Parameter Values with JSTL Actions ................................................ 82
8.1.2 Accessing Other Request Data ............................................................................ 86
8.1.3 Capturing Parameter Values Using a Bean ......................................................... 89
8.2 Validating User Input ................................................................................................. 91
8.2.1 Validating User Input Using JSTL Actions ........................................................ 92
8.2.2 Validating User Input Using a Bean ................................................................... 97
8.3 Formatting HTML Output........................................................................................ 101
Chapter 9. Error Handling and Debugging................................................................... 103
9.1 Dealing with Syntax Errors ...................................................................................... 103
9.1.1 Element Syntax Errors ...................................................................................... 103
9.1.2 JSTL Expression Language Syntax Errors........................................................ 107
9.2 Debugging a JSP Application .................................................................................. 110
9.3 Dealing with Runtime Errors ................................................................................... 115
9.3.1 Catching Exceptions.......................................................................................... 119
Chapter 10. Sharing Data Between JSP Pages, Requests, and Users.......................... 121
10.1 Passing Control and Data Between Pages.............................................................. 121
10.1.1 Passing Control from One Page to Another .................................................... 122
10.1.2 Passing Data from One Page to Another......................................................... 124
10.1.3 All Together Now............................................................................................ 126
ii
Zgłoś jeśli naruszono regulamin