core
SERVLETS ?and? JAVASERVER ?PAGESTM
by Marty Hall
A Sun Microsystems Press/Prentice Hall PTR Book
ISBN 0-13-089340-4

Book Table of Contents

This page gives the complete table of contents of Core Servlets and JavaServer Pages (JSP) in HTML format. For printing purposes, try the PDF version instead. If you arrived at this page other than by a link from the book's home page, you probably want to visit www.coreservlets.com.

Servlet & JSP Training Courses. On-site courses at your organization. Basic, intermediate, or advanced. Taught by the author of Core Servlets and JavaServer Pages and More Servlets and JavaServer Pages.
CSAJSP
Core Servlets
& JSP
MSAJSP
More Servlets
& JSP
Public training course: Programming with Servlet & JSP Technology. Short course based on the materials from this site. April 16-19 in Northern Virginia.
Public training course: Advanced Servlet & JSP Technology. Short course based on More Servlets & JSP. April 23-26 in Northern Virginia.
Acknowledgments xix
About the Author xx
Introduction xxi
Real Code for Real Programmers xxii
How This Book Is Organized xxiii
Conventions xxvii
About the Web Site xxvii

PART 1
Servlets 2.1 and 2.2 2

CHAPTER 1
Overview of Servlets and JavaServer Pages 4

1.1 Servlets 5
1.2 The Advantages of Servlets Over "Traditional" CGI 7
Efficient 7
Convenient 7
Powerful 8
Portable 8
Secure 8
Inexpensive 9
1.3 JavaServer Pages 9
1.4 The Advantages of JSP 10
Versus Active Server Pages (ASP) 10
Versus PHP 10
Versus Pure Servlets 11
Versus Server-Side Includes (SSI) 11
Versus JavaScript 11
Versus Static HTML 12
1.5 Installation and Setup 12
Obtain Servlet and JSP Software 12
Bookmark or Install the Servlet and JSP API Documentation 14
Identify the Classes to the Java Compiler 14
Package the Classes 15
Configure the Server 16
Start the Server 17
Compile and Install Your Servlets 18

CHAPTER 2
First Servlets 20

2.1 Basic Servlet Structure 21
2.2 A Simple Servlet Generating Plain Text 23
Compiling and Installing the Servlet 24
Invoking the Servlet 25
2.3 A Servlet That Generates HTML 26
2.4 Packaging Servlets 27
Creating Servlets in Packages 28
Compiling Servlets in Packages 29
Invoking Servlets in Packages 30
2.5 Simple HTML-Building Utilities 31
2.6 The Servlet Life Cycle 34
The init Method 34
The service Method 36
The doGet, doPost, and doXxx Methods 37
The SingleThreadModel Interface 38
The destroy Method 38
2.7 An Example Using Initialization Parameters 39
2.8 An Example Using Servlet Initialization and Page Modification Dates 44
2.9 Debugging Servlets 50
2.10 WebClient: Talking to Web Servers Interactively 52
WebClient 52
HttpClient 55
NetworkClient 57
SocketUtil 59
CloseableFrame 60
LabeledTextField 61
Interruptible 63

CHAPTER 3
Handling the Client Request: Form Data 64

3.1 The Role of Form Data 65
3.2 Reading Form Data from Servlets 66
3.3 Example: Reading Three Explicit Parameters 67
3.4 Example: Reading All Parameters 70
3.5 A Resume Posting Service 74
3.6 Filtering Strings for HTML-Specific Characters 87
Code for Filtering 88
Example 89

CHAPTER 4
Handling the Client Request: HTTP Request Headers 92

4.1 Reading Request Headers from Servlets 94
4.2 Printing All Headers 96
4.3 HTTP 1.1 Request Headers 98
4.4 Sending Compressed Web Pages 104
4.5 Restricting Access to Web Pages 107

CHAPTER 5
Accessing the Standard CGI Variables 114

5.1 Servlet Equivalent of CGI Variables 116
5.2 A Servlet That Shows the CGI Variables 119

CHAPTER 6
Generating the Server Response: HTTP Status Codes 122

6.1 Specifying Status Codes 124
6.2 HTTP 1.1 Status Codes and Their Purpose 126
6.3 A Front End to Various Search Engines 135

CHAPTER 7
Generating the Server Response: HTTP Response Headers 142

7.1 Setting Response Headers from Servlets 143
7.2 HTTP 1.1 Response Headers and Their Meaning 145
7.3 Persistent Servlet State and Auto-Reloading Pages 154
7.4 Using Persistent HTTP Connections 163
7.5 Using Servlets to Generate GIF Images 168

CHAPTER 8
Handling Cookies 178

8.1 Benefits of Cookies 179
Identifying a User During an E-commerce Session 180
Avoiding Username and Password 180
Customizing a Site 180
Focusing Advertising 181
8.2 Some Problems with Cookies 181
8.3 The Servlet Cookie API 183
Creating Cookies 183
Cookie Attributes 183
Placing Cookies in the Response Headers 186
Reading Cookies from the Client 186
8.4 Examples of Setting and Reading Cookies 186
8.5 Basic Cookie Utilities 190
Finding Cookies with Specified Names 190
Creating Long-Lived Cookies 191
8.6 A Customized Search Engine Interface 191

CHAPTER 9
Session Tracking 198

9.1 The Need for Session Tracking 199
Cookies 200
URL-Rewriting 200
Hidden Form Fields 201
Session Tracking in Servlets 201
9.2 The Session Tracking API 201
Looking Up the HttpSession Object Associated with the Current Request 202
Looking Up Information Associated with a Session 202
Associating Information with a Session 205
Terminating Sessions 206
Encoding URLs Sent to the Client 206
9.3 A Servlet Showing Per-Client Access Counts 207
9.4 An On-Line Store Using a Shopping Cart and Session Tracking 209
Building the Front End 210
Handling the Orders 215
Behind the Scenes: Implementing the Shopping Cart and
Catalog Items 220

PART 2
JavaServer Pages 228

CHAPTER 10 JSP Scripting Elements 230

10.1 Scripting Elements 233
Template Text 234
10.2 JSP Expressions 234
Predefined Variables 234
XML Syntax for Expressions 235
Using Expressions as Attribute Values 235
Example 236
10.3 JSP Scriptlets 238
Using Scriptlets to Make Parts of the JSP File Conditional 241
Special Scriptlet Syntax 242
10.4 JSP Declarations 242
Special Declaration Syntax 244
10.5 Predefined Variables 244

CHAPTER 11
The JSP page Directive: Structuring Generated Servlets 246

11.1 The import Attribute 248
Directories for Custom Classes 248
Example 249
11.2 The contentType Attribute 251
Generating Plain Text Documents 252
Generating Excel Spreadsheets 254
11.3 The isThreadSafe Attribute 258
11.4 The session Attribute 259
11.5 The buffer Attribute 259
11.6 The autoflush Attribute 260
11.7 The extends Attribute 260
11.8 The info Attribute 260
11.9 The errorPage Attribute 261
11.10 The isErrorPage Attribute 261
11.11 The language Attribute 264
11.12 XML Syntax for Directives 265

CHAPTER 12
Including Files and Applets in JSP Documents 266

12.1 Including Files at Page Translation Time 268
12.2 Including Files at Request Time 270
12.3 Including Applets for the Java Plug-In 274
The jsp:plugin Element 275
The jsp:param and jsp:params Elements 277
The jsp:fallback Element 278
Example: Building Shadowed Text 279

CHAPTER 13
Using JavaBeans with JSP 286

13.1 Basic Bean Use 288
Accessing Bean Properties 290
Setting Bean Properties: Simple Case 290
Installing Bean Classes 291
13.2 Example: StringBean 292
13.3 Setting Bean Properties 294
Associating Individual Properties with Input Parameters 298
Automatic Type Conversions 300
Associating All Properties with Input Parameters 301
13.4 Sharing Beans 302
Conditional Bean Creation 304

CHAPTER 14
Creating Custom JSP Tag Libraries 308

14.1 The Components That Make Up a Tag Library 310
The Tag Handler Class 310
The Tag Library Descriptor File 311
The JSP File 313
14.2 Defining a Basic Tag 314
The Tag Handler Class 315
The Tag Library Descriptor File 316
The JSP File 318
14.3 Assigning Attributes to Tags 319
The Tag Handler Class 319
The Tag Library Descriptor File 321
The JSP File 322
14.4 Including the Tag Body 323
The Tag Handler Class 324
The Tag Library Descriptor File 326
The JSP File 328
14.5 Optionally Including the Tag Body 329
The Tag Handler Class 329
The Tag Library Descriptor File 331
The JSP File 332
14.6 Manipulating the Tag Body 334
The Tag Handler Class 334
The Tag Library Descriptor File 335
The JSP File 336
14.7 Including or Manipulating the Tag Body Multiple Times 338
The Tag Handler Class 338
The Tag Library Descriptor File 340
The JSP File 341
14.8 Using Nested Tags 341
The Tag Handler Classes 342
The Tag Library Descriptor File 348
The JSP File 350

CHAPTER 15
Integrating Servlets and JSP 352

15.1 Forwarding Requests 354
Using Static Resources 354
Supplying Information to the Destination Pages 355
Interpreting Relative URLs in the Destination Page 357
Alternative Means of Getting a RequestDispatcher 358
15.2 Example: An On-Line Travel Agent 358
15.3 Including Static or Dynamic Content 375
15.4 Example: Showing Raw Servlet and JSP Output 377
15.5 Forwarding Requests From JSP Pages 380

PART 3
Supporting Technologies 382

CHAPTER 16
Using HTML Forms 384

16.1 How HTML Forms Transmit Data 385
16.2 The FORM Element 390
16.3 Text Controls 395
Textfields 395
Password Fields 397
Text Areas 398
16.4 Push Buttons 400
Submit Buttons 401
Reset Buttons 404
JavaScript Buttons 405
16.5 Check Boxes and Radio Buttons 405
Check Boxes 406
Radio Buttons 407
16.6 Combo Boxes and List Boxes 409
16.7 File Upload Controls 412
16.8 Server-Side Image Maps 414
IMAGE -- Standard Server-Side Image Maps 414
ISMAP -- Alternative Server-Side Image Maps 417
16.9 Hidden Fields 419
16.10 Grouping Controls 420
16.11 Controlling Tab Order 422
16.12 A Debugging Web Server 423
EchoServer 423
ThreadedEchoServer 427
NetworkServer 428

CHAPTER 17
Using Applets As Servlet Front Ends 432

17.1 Sending Data with GET and Displaying the Resultant Page 434
17.2 A Multisystem Search Engine Front End 435
17.3 Sending Data with GET and Processing the Results Directly (HTTP Tunneling) 438
Reading Binary or ASCII Data 439
Reading Serialized Data Structures 441
17.4 A Query Viewer That Uses Object Serialization and HTTP Tunneling 443
17.5 Sending Data by POST and Processing the Results Directly (HTTP Tunneling) 450
17.6 An Applet That Sends POST Data 453
17.7 Bypassing the HTTP Server 459

CHAPTER 18
JDBC and Database Connection Pooling 460

18.1 Basic Steps in Using JDBC 462
Load the Driver 462
Define the Connection URL 463
Establish the Connection 464
Create a Statement 465
Execute a Query 465
Process the Results 465
Close the Connection 466
18.2 Basic JDBC Example 467
18.3 Some JDBC Utilities 473
18.4 Applying the Database Utilities 482
18.5 An Interactive Query Viewer 487
Query Viewer Code 489
18.6 Prepared Statements (Precompiled Queries) 497
18.7 Connection Pooling 501
18.8 Connection Pooling: A Case Study 508
18.9 Sharing Connection Pools 515
Using the Servlet Context to Share Connection Pools 515
Using Singleton Classes to Share Connection Pools 516

Appendix
Servlet and JSP Quick Reference 518

A.1 Overview of Servlets and JavaServer Pages 519
Advantages of Servlets 519
Advantages of JSP 519
Free Servlet and JSP Software 519
Documentation 520
Servlet Compilation: CLASSPATH Entries 520
Tomcat 3.0 Standard Directories 520
Tomcat 3.1 Standard Directories 520
JSWDK 1.0.1 Standard Directories 520
Java Web Server 2.0 Standard Directories 521
A.2 First Servlets 521
Simple Servlet 521
Installing Servlets 521
Invoking Servlets 521
Servlet Life Cycle 522
A.3 Handling the Client Request: Form Data 523
Reading Parameters 523
Example Servlet 523
Example Form 524
Filtering HTML-Specific Characters 524
A.4 Handling the Client Request: HTTP Request Headers 524
Methods That Read Request Headers 524
Other Request Information 525
Common HTTP 1.1 Request Headers 525
A.5 Accessing the Standard CGI Variables 526
Capabilities Not Discussed Elsewhere 526
Servlet Equivalent of CGI Variables 526
A.6 Generating the Server Response: HTTP Status Codes 527
Format of an HTTP Response 527
Methods That Set Status Codes 527
Status Code Categories 527
Common HTTP 1.1 Status Codes 527
A.7 Generating the Server Response: HTTP Response Headers 528
Setting Arbitrary Headers 528
Setting Common Headers 528
Common HTTP 1.1 Response Headers 528
Generating GIF Images from Servlets 529
A.8 Handling Cookies 530
Typical Uses of Cookies 530
Problems with Cookies 530
General Usage 530
Cookie Methods 530
A.9 Session Tracking 531
Looking Up Session Information: getValue 531
Associating Information with a Session: putValue 531
HttpSession Methods 532
Encoding URLs 533
A.10 JSP Scripting Elements 533
Types of Scripting Elements 533
Template Text 533
Predefined Variables 533
A.11 The JSP page Directive: Structuring Generated Servlets 534
The import Attribute 534
The contentType Attribute 534
Example of Using contentType 534
Example of Using setContentType 535
The isThreadSafe Attribute 535
The session Attribute 536
The buffer Attribute 536
The autoflush Attribute 536
The extends Attribute 536
The info Attribute 536
The errorPage Attribute 536
The isErrorPage Attribute 536
The language Attribute 536
XML Syntax 537
A.12 Including Files and Applets in JSP Documents 537
Including Files at Page Translation Time 537
Including Files at Request Time 537
Applets for the Java Plug-In: Simple Case 537
Attributes of jsp:plugin 537
Parameters in HTML: jsp:param 538
Alternative Text 538
A.13 Using JavaBeans with JSP 539
Basic Requirements for Class to be a Bean 539
Basic Bean Use 539
Associating Properties with Request Parameters 539
Sharing Beans: The scope Attribute of jsp:useBean 539
Conditional Bean Creation 540
A.14 Creating Custom JSP Tag Libraries 540
The Tag Handler Class 540
The Tag Library Descriptor File 541
The JSP File 541
Assigning Attributes to Tags 541
Including the Tag Body 541
Optionally Including the Tag Body 542
Manipulating the Tag Body 542
Including or Manipulating the Tag Body Multiple Times 542
Using Nested Tags 542
A.15 Integrating Servlets and JSP 542
Big Picture 542
Request Forwarding Syntax 543
Forwarding to Regular HTML Pages 543
Setting Up Globally Shared Beans 543
Setting Up Session Beans 543
Interpreting Relative URLs in the Destination Page 543
Getting a RequestDispatcher by Alternative Means (2.2 Only) 543
Including Static or Dynamic Content 544
Forwarding Requests from JSP Pages 544
A.16 Using HTML Forms 544
The FORM Element 544
Textfields 544
Password Fields 544
Text Areas 545
Submit Buttons 545
Alternative Push Buttons 545
Reset Buttons 545
Alternative Reset Buttons 545
JavaScript Buttons 546
Alternative JavaScript Buttons 546
Check Boxes 546
Radio Buttons 546
Combo Boxes 546
File Upload Controls 547
Server-Side Image Maps 547
Hidden Fields 547
Internet Explorer Features 547
A.17 Using Applets As Servlet Front Ends 547
Sending Data with GET and Displaying the Resultant Page 547
Sending Data with GET and Processing the Results Directly (HTTP Tunneling) 548
Sending Serialized Data: The Applet Code 549
Sending Serialized Data: The Servlet Code 549
Sending Data by POST and Processing the Results Directly (HTTP Tunneling) 550
Bypassing the HTTP Server 551
A.18 JDBC and Database Connection Pooling 552
Basic Steps in Using JDBC 552
Database Utilities 553
Prepared Statements (Precompiled Queries) 553
Steps in Implementing Connection Pooling 554
Index 557

Go to book's home page at http://www.coreservlets.com.