hep/
Mastering Tomcat
Development
Ian McFarland
Peter Harrison
'. \
Wiley Publishing, Inc. 'Contents
AcknowledgmentsAbout the Author Introduction
xv xvii xix
Part I Tomcat Configuration and Management
Chapter 1 Introduction to TomcatThe Apache Software Foundation Tomcat
Tomcat Versions Servlets
What's New in the Servlet 2.3 Specification JavaServer Pages
What's New in the JSP 1.2 Specification Summary
Chapter 2 Installing Tomcat Installing the Java SDK Installing the Tomcat Files
Windows Installation
Linux, OS X, and Other Unix Installations Setting Up the Environment for Tomcat
Windows 95/98/ME Windows NT/2000 Linux
Mac OS X
Starting Up Tomcat for the First Time Windows
Linux, OS X, and other Unix
1 2 3 4 5 6 7 7 9 10 10 10 12 12 12 13 14 14 14 15 vii
viii Contents
Starting Tomcat on Boot Windows
Linux
Mac OS X Server Mac OS X
Other Unix Flavors First Look at Tomcat Summary
Chapter 3 Configuring Tomcat
Tomcat Configuration Files The HTTP 1.1 Connector The WAEP Connector Other Connectors Webapp Contexts
Web Application Configuration Configuring Tomcat with Apache
Changes to Apache
Configuring Tomcat with Microsoft IIS Adding Registry Entries
Virtual Host Configuration Apache Virtual Hosting Tomcat Virtual Hosting Summary
Chapter 4 Hello World Wide Web Handling a Request What's In a Response Summary
Chapter 5 Anatomy and Life Cycle of a Servlet
The Servlet Life Cycle Servlet Anatomy Life Cycle Events
j avax. servlet. ServletContextlistener javax.servlet.ServletContextAttributelistener javax.servlet.http.HttpSessionListener
javax.servlet.http.HttpSessionAttributeListener Configuring Life Cycle Listeners
Summary
Chapter 6 Configuring Web Applications Installing in /webapps
The Deployment Descriptor: web.xml WAR Files
Configuring Servlets from the server.xml File Summary
Chapter 7 The server.xml File in Depth New Features in Tomcat 4.1 The Basic Model
The <Server/> Node The <Service/> Container
17 17 17 19 19 23 24 24 25 28 29 30 31 31 34 34 35 36 37 38 38 40 40 41 44 46 47
49
49 52 57 57 59 60 61 62 64 65 65 67 69 70 71 73 74 74 75 76Contents
The <Engine/> Container 76 The <Realm/> Node 77 The <Logger/> Node 77 The <Host/> Container 78 The <Context/> Container 82 The <Valve/> Node 84
Access Log Valve 84 Remote Host Filter and Remote Address Filter 85 Request Dumper Valve 86 Single Sign On Valve 86 Additional Valves 86
The <Resources/> Node 87 The <Loader/> Node 87 The <Manager/> Node 89 The <Connector/> Node 91
The Coyote HTTP/1.1 Connector 92 Server Proxy Connectors 93 The JK 2 Connector 94 The Webapp Connector 94
The <Factory/> Node 95 The Tomcat Web Server Administration Tool 98
Setup 98 Security 99 Using the Administration Tool 99
Summary 99
Chapter 8 Managing Authentication with Realms 101
HTTP Authorization Schemes 101 Setting Up Realms on Tomcat 103 The MemoryRealm 103 JDBC Realms 107 JNDI Realms 111 Custom Realms 113 Scope of Realms 113 Single Sign-On 113 Security and Identity 114 Summary 117
Chapter 9 The Manager Application 119
Adding a Management User to the Default MemoryRealm 120
Listing Installed Web Applications 121 Starting and Stopping a Web Application 121
Reloading a Web Application 122 Displaying Statistics 123 Installing a Web Application 123 Removing a Web Application 125 The HTML Interface in Tomcat 4.1 126 The Ant Interface in Tomcat 4.1 126 Summary 128
Contents
Chapter 10 The Model-View-Controller Architecture 129
What Is MVC Architecture? 129
Model Objects 130 View Objects 130 Communication between Controller and View 130 Controller Objects 131
Advantages of MVC 131
Clean Design 131 Encapsulation 131 Extensibility 132 Reuse and Refactoring 132
Using MVC on the Web 132 Summary 134
Part II Tomcat Development
Chapter 11 JavaServer Pages 155
What Does a JSP Page Look Like? 135 JSPs Are Really Servlets 137 Jasper: The Tomcat JSP Compiler 144 Expressions 145 Scriptlets and Declarations 145 Page Directives 146 Including Pages 148 Working with Beans 150
Scope 152
Debugging JSPs 152 Summary 154
Chapter 12 Tag Libraries 155_
Writing a Basic Tag Library 156
Packaging the Tag 157 Using the Tag 158 A Word about Namespaces 159
A Practical Tag Example 159 Tags with Bodies 167 JSTL: The JSP Standard Tag Library 178 Other Tag Libraries from the ASF 180 Summary 182
Chapter 13 MVC with Struts 185
Overview of Struts 184 A Typical Model 1-Style JSP Application 185
The Department Model 185 The Department Database Schema 193 The JSPs for the Model 1 Department Application 194
The MVC Struts Version of the Application 200
List Department Action 201 Action Details 203 The Struts Deptlisting View 203 The Struts Custom Tags 204
Contents
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Employeelisting: Action and View Managing Form Data with Struts
Templates
Struts and XDoclet Summary
Template Systems
Why Templates Rather than JSP? What Is Velocity?
Installing Velocity
An Example of Using Velocity The Velocity Template Language
References Directives Setting Variables Conditionals Loops Including Files Macros Summary Session Management
State Management and HTTP Using HttpSession
Expiring Sessions
Setting Your Own Cookies
Additional Cookie Attributes
Summary
Databases, Connection Pools, and JDBC
JDBC Drivers SQL Database Products MySQL PostgreSQL InterBase/Firebird Oracle DB2 MS SQL Others
Building a Database Servlet
A Simple Database Servlet
Using Connection Pools
Connection Pools for JDBC Datasources
Summary Database-Aware Objects 207 210 222 224 228 229 229 230 230 231 233 233 238 238 240 241 243 244 245 247 247 249 267 267 268 269 271 271 273 273 274 274 274 275 275 276 276 277 282 282 289 289
The Factory and Modified Singleton Design Patterns 290 The Design 294 Setting Up the Project 295 SmartObject.java 295 ObjectFactoryjava 299 Getting Existing Objects 300 Storing the Data 308
x i i Contents
Automated Commits 312 Creating New Objects 316 Using the Framework 319 Adding the getObjects() Method 327 Some Remaining Issues 331 Summary 331
Chapter 18 Security 555
Chapter 19
Configuring Tomcat
SSL, HTTPS, and TLS
Using Tomcat Behind Another Server to Provide HTTPS Setting Up the HTTPS Connector
Digital Certificates
Configuring the Connector
The Security Manager
Bad Code Pernicious Code
Weak, Dangerous, Badly Written, or Misconfigured Code Tomcat and the Security Manager
Using the Security Manager with Tomcat Debugging Permissions Issues
Permissions
Running as an Unprivileged User
Servlet Development
Using Onetime Passwords
Discovering the Protocol of Your Connection Using Certificates as User Credentials Other Techniques for Validating Identity
Summary
The Development Cycle
Structuring Your Workspace Version Control
Build Management
Running the Software You Build
Testing
Developing Unit Tests with JUnit Integration Tests
Refactoring
Staged Release Environment
Setting Up Your Development and Testing Distances User Testing
Load Testing and Profiling Profiling Your Code
Summary
Developing Web Applications with Ant and XDoclet
334 334 335 335 336 338 339 339 340 340 341 345 345 345 346 347 347 348 350 351 351 555 354 355 358 361 361 361 366 369 369 369 370 370 371 372 575 Chapter 20
Setting Up Your Environment to Run Ant 374 What Does an Ant Build File Look Like? 375
Properties, File Sets, and Paths 375 Using Properties 377 Conditional Targets 378 Using Filters 378
Contents
xiii
Creating a Master Build File 380 Using the War Task to Create a WAR File 381 Using XDoclet's webdoclet Task to Create Deployment Descriptors 382
Running Ant for the First Time 392
Using the XDoclet's webdoclet Task to Create Custom Tag TLDs 393 Standard Targets 400 The Hello World Model Project 402
Overview of Model Classes 402 Creating a Project Directory Structure for Model 403 Creating a Build File for a Shared Library 404 Analysis of the Model Project Build File 404 Running an Ant Build File 408
The Hello World Application Project 409
Overview of Application Java Classes 409 Creating a Project Directory Structure for the Application 409 Creating a Manifest File for a Stand-alone Application 410 Creating an Ant Build File for a Stand-Alone Application 410
The Hello World Main Project 413
Creating a Master Build File 413 Analysis of the Master Build File 414
The Applet Project 415
Overview of the Applet Class 415 Creating a Build File for the Applet 419 Building the Applet with Ant 420 Hello World Recap 421
Hello World Model 2 and J2EE 421
The Web Application Project 422 Building and Deploying the Web Application 432 Running the Web Application 434
Summary 437
Appendix A Server Configuration (server.xml) Reference 459
Appendix B The Deployment Descriptor (web.xml) Reference 451