hep/ Mastering Tomcat Development Ian McFarland Peter Harrison '. \ Wiley Publishing, Inc. '
Part I Chapter 1 Chapter 2 Acknowledgments About the Author Introduction Tomcat Configuration and Management Introduction to Tomcat The 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 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 xv xvii xix 1 2 3 4 5 6 7 7 9 10 10 10 12 12 12 13 14 14 14 15 vii
viii Contents Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Starting Tomcat on Boot Windows Linux Mac OS X Server Mac OS X Other Unix Flavors First Look at Tomcat 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 Hello World Wide Web Handling a Request What's In a Response 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 Configuring Web Applications Installing in /webapps The Deployment Descriptor: web.xml WAR Files Configuring Servlets from the server.xml File 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 76
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 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 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 128
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 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 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 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
Chapter 14 Chapter 15 Chapter 16 Chapter 17 Employeelisting: Action and View Managing Form Data with Struts Templates Struts and XDoclet 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 Session Management State Management and HTTP Using HttpSession Expiring Sessions Setting Your Own Cookies Additional Cookie Attributes 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 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
xii Contents Chapter 19 Automated Commits 312 Creating New Objects 316 Using the Framework 319 Adding the getobjects() Method 327 Some Remaining Issues 331 331 Chapter 18 Security 555 Configuring Tomcat 334 SSL, HTTPS, and TLS 334 Using Tomcat Behind Another Server to Provide HTTPS 335 Setting Up the HTTPS Connector 335 Digital Certificates 336 Configuring the Connector 338 The Security Manager 339 Bad Code 339 Pernicious Code 340 Weak, Dangerous, Badly Written, or Misconfigured Code 340 Tomcat and the Security Manager 341 Using the Security Manager with Tomcat 345 Debugging Permissions Issues 345 Permissions 345 Running as an Unprivileged User 346 Servlet Development 347 Using Onetime Passwords 347 Discovering the Protocol of Your Connection 348 Using Certificates as User Credentials 350 Other Techniques for Validating Identity 351 351 Chapter 20 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 555 354 355 358 361 361 361 366 369 369 369 370 370 371 372 Developing Web Applications with Ant and XDoclet 575 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
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 437 Appendix A Server Configuration (server.xml) Reference 459 Appendix B The Deployment Descriptor (web.xml) Reference 451 Index 479