For all the people which doesn't have enough money to buy good books. For all the rippers. pro2002 ☺ 1 Apache Jakarta-Tomcat by James Goodwill ISBN: 1893115364 Apress © 2002 (237 pages) Learn about Tomcat Server configuration, its features, and more, complete with examples and source code. Apache Jakarta Tomcat..................................................................................................8 Dedication.............................................................................................................................9 Preface.............................................................................................................................10 Introduction......................................................................................................................11 About the Author.....................................................................................................11 About the Technical Reviewer...........................................................................11 Chapter 1: Jakarta Tomcat ...................................................................12 The Jakarta Tomcat Server................................................................................12 The Tomcat Manager Web Application..............................................................12 Specialized Realm Implementations...................................................................12 Tomcat Valves........................................................................................................12 Further Information................................................................................................12 The Architecture of Tomcat................................................................................13 The Server...............................................................................................................14 The Service.............................................................................................................14 The Connector........................................................................................................14 The Engine..............................................................................................................15 The Host..................................................................................................................15 The Context.............................................................................................................15 Java Web Applications.........................................................................................15 The Directory Structure.........................................................................................15 The Deployment Descriptor..................................................................................16 Packaging................................................................................................................17 Requirements for Installing and Configuring Tomcat................................17 Installing and Configuring Tomcat ...................................................................18 Manually Installing to Windows NT/2000...........................................................18 Installing to Linux....................................................................................................20 Testing Your Tomcat Installation......................................................................21 Summary...................................................................................................................24 Chapter 2: Deploying Web Applications to Tomcat ......25 The Tomcat Directory Structure........................................................................25 Manually Deploying Web Applications to Tomcat......................................25 Creating the Web Application Directory Structure............................................26 Creating a Web Application ServletContext.......................................................26 Adding JSPs............................................................................................................27 Adding Servlets......................................................................................................30 Adding Tag Libraries..............................................................................................33 2 Creating and Deploying a WAR File...................................................................37 Summary...................................................................................................................37 Chapter 3: Servlets, JSPs, and the ServletContext .....38 What Are Java Servlets?.....................................................................................38 The GenericServlet and HttpServlet Classes....................................................39 The Lifecycle of a Servlet......................................................................................40 A Simple Servlet.....................................................................................................41 What Are JavaServer Pages?...........................................................................44 The Components of a JavaServer Page............................................................45 The ServletContext and its Relationship to a Web Application.............61 The Relationship Between a Web Application and the ServletContext.........61 Examples of How the Web Application Affects Web Application Components ...................................................................................................................................62 Summary...................................................................................................................67 Chapter 4: Using Tomcat's Manager Application ...........69 What is the Manager Web Application?.........................................................69 Gaining Access to the Manager Web Application......................................69 Using the Manager Web Application...............................................................71 install........................................................................................................................71 list..............................................................................................................................73 reload.......................................................................................................................73 sessions...................................................................................................................74 stop...........................................................................................................................75 start...........................................................................................................................76 remove.....................................................................................................................76 Summary...................................................................................................................77 Chapter 5: Configuring Security Realms .................................78 Security Realms......................................................................................................78 Memory Realms......................................................................................................78 Protecting a Resource with a MemoryRealm....................................................79 JDBC Realms..........................................................................................................81 Creating the Users Database...............................................................................81 Configuring Tomcat to Use a JDBC Realm.......................................................87 The Benefits of Using a JDBCRealm..................................................................89 Accessing an Authenticated User....................................................................89 Summary...................................................................................................................91 Chapter 6: Embedding Tomcat .........................................................92 Embedding Tomcat into a Java Application.................................................92 Summary.................................................................................................................100 Chapter 7: Persistent Sessions ......................................................101 HTTP Sessions.....................................................................................................101 The Servlet Implementation of HTTP Sessions..............................................101 3 Configuring Tomcat's Persistent Session Stores.....................................106 The FileStore........................................................................................................106 The JDBCStore.....................................................................................................108 Creating the Sessions Database.......................................................................108 Configuring Tomcat to Use a JDBCStore........................................................109 Summary.................................................................................................................111 Chapter 8: Valves and Servlet Filters .......................................112 What is a Tomcat Valve?..................................................................................112 The Access Log Valve.........................................................................................112 The Remote Address Filter.................................................................................114 The Remote Host Filter.......................................................................................115 The Request Dumper Valve...............................................................................116 What is a Servlet Filter?.....................................................................................116 Deploying a Servlet Filter....................................................................................119 Chaining Servlet Filters.......................................................................................123 Summary.................................................................................................................127 Chapter 9: Integrating the Apache HTTP Server ..........128 What is the Apache Web Server?..................................................................128 Integrating Tomcat and the Apache Web Server.....................................128 Summary.................................................................................................................132 Chapter 10: Integrating the Jakarta-Struts Project ......134 The Jakarta-Struts Project................................................................................134 Understanding the MVC Design Pattern..........................................................134 The Struts Implementation of the MVC............................................................134 Creating and Installing a Struts Web Application.....................................135 The Views..............................................................................................................137 The Model..............................................................................................................140 The Controller.......................................................................................................142 Walking Through the apress-struts Web Application..............................145 Summary.................................................................................................................148 Chapter 11: Integrating the Jakarta-Log4J Project .....149 The Jakarta-Log4J Project................................................................................149 Layouts...................................................................................................................149 Appenders.............................................................................................................149 Categories.............................................................................................................150 Using Log4J in an Application.........................................................................153 Integrating Log4J into the apress Web Application.................................155 Summary.................................................................................................................159 Chapter 12: Integrating the Apache SOAP Project ....160 Introducing the Apache SOAP Project.........................................................160 Integrating Apache SOAP into Tomcat........................................................160 Deploying Apache-SOAP Using Tomcat..........................................................161 4 Creating a Sample SOAP Application..........................................................162 SOAP Services.....................................................................................................163 SOAP Clients........................................................................................................167 Summary.................................................................................................................170 Appendix A: The server.xml File ...................................................171 Containers...............................................................................................................173 The <Server> Element......................................................................................173 The <Service> Element...................................................................................174 The <Engine> Element......................................................................................175 The <Host> Element..........................................................................................176 The <Context> Element...................................................................................177 Connectors..............................................................................................................178 The HTTP Connector ..........................................................................................179 The Warp Connector...........................................................................................180 Appendix B: The web.xml File .........................................................182 Overview..................................................................................................................182 Adding a Servlet Filter........................................................................................184 Adding a Servlet Definition...............................................................................185 Adding a Servlet Mapping.................................................................................186 Configuring the Session.....................................................................................187 Adding a Welcome File List..............................................................................187 Adding a Tag Library...........................................................................................187 Adding a Security Constraint...........................................................................188 Adding a Login Config........................................................................................189 List of Figures ...................................................................................................191 Chapter 1: Jakarta Tomcat...............................................................................191 Chapter 2: Deploying Web Applications to Tomcat.................................191 Chapter 3: Servlets, JSPs, and the ServletContext.................................191 Chapter 4: Using Tomcat's Manager Application.....................................191 Chapter 5: Configuring Security Realms.....................................................192 Chapter 7: Persistent Sessions.......................................................................192 Chapter 8: Valves and Servlet Filters...........................................................192 Chapter 9: Integrating the Apache HTTP Server.....................................192 Chapter 10: Integrating the Jakarta-Struts Project..................................193 Chapter 12: Integrating the Apache SOAP Project.................................193 List of Tables .....................................................................................................194 Chapter 1: Jakarta Tomcat...............................................................................194 Chapter 2: Deploying Web Applications to Tomcat.................................194 Chapter 3: Servlets, JSPs, and the ServletContext.................................194 Chapter 4: Using Tomcat's Manager Application.....................................194 Chapter 5: Configuring Security Realms.....................................................194 5 Chapter 7: Persistent Sessions.......................................................................195 Chapter 8: Valves and Servlet Filters...........................................................195 Chapter 9: Integrating the Apache HTTP Server.....................................195 Chapter 10: Integrating the Jakarta-Struts Project..................................195 Chapter 12: Integrating the Apache SOAP Project.................................195 Appendix A: The server.xml File.....................................................................196 Appendix B: The web.xml File.........................................................................196 List of Examples ............................................................................................197 Chapter 2: Deploying Web Applications to Tomcat.................................197 Chapter 3: Servlets, JSPs, and the ServletContext.................................197 Chapter 4: Using Tomcat's Manager Application.....................................197 Chapter 5: Configuring Security Realms.....................................................197 Chapter 6: Embedding Tomcat.......................................................................198 Chapter 7: Persistent Sessions.......................................................................198 Chapter 8: Valves and Servlet Filters...........................................................198 Chapter 10: Integrating the Jakarta-Struts Project..................................198 Chapter 11: Integrating the Jakarta-Log4J Project..................................198 Chapter 12: Integrating the Apache SOAP Project.................................198 Appendix A: The server.xml File.....................................................................198 Appendix B: The web.xml File.........................................................................199 6 7 Apache Jakarta Tomcat JAMES GOODWILL Apress Apache Jakarta Tomcat Copyright © 2002 James Goodwill All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. (pbk): 1-893115-36-4 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Editorial Directors: Dan Appleman, Gary Cornell, Jason Gilmore, Karen Watterson Technical Reviewer: Aaron Bandell Project Managers: Alexa Stuart, Erin Mulligan Developmental and Copy Editor: Tom Gillen, Gillen Editorial, Inc. Production Editor: Sofia Marchant Compositor: Impressions Book and Journal Services, Inc. Indexer: Ron Strauss Cover Designer: Tom Debolski Managing Editor: Grace Wong Marketing Manager: Stephanie Rodriguez Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 175 Fifth Avenue, New York, NY, 10010 and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany In the United States, phone 1-800-SPRINGER, email [email protected], or visit http://www.springer-ny.com. Outside the United States, fax +49 6221 345229, email [email protected], or visit http://www.springer.de. For information on translations, please contact Apress directly at 901 Grayson Street, Suite 204BerkeleyCA94710510-549-5930510-549- 5939<[email protected]>, or visit http://www.apress.com The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author nor 8 Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Downloads section. You will need to answer questions pertaining to this book to successfully download the code. Dedication To my girls Christy, Abby, and Emma. 9 Preface Acknowledgments I would like to begin this text by thanking the people who made this book what it is today. They are the people who took my words and shaped them into something that I hope will help you use the Tomcat container to its fullest. Of these people, I would like to explicitly thank Jason Gilmore, Alexa Stuart, Erin Mulligan, Aaron Bandell, Tom Gillen, Sofia Marchant, Ron Strauss, and Grace Wong. Each and every person made this book what it is today. On a closer note, I would like to thank everyone at my company, Virtuas Solutions, LLC, for their support while I was completing this text. The entire, "UNREAL", staff contributed by picking up my assignments when my plate was too full. Finally, the most important contributors to this book are my wife Christy, and our daughters Abby and Emma. They are the ones who really sacrificed during the development of this text, especially when I walked in the room discussing another Tomcat beta release. They are the ones who deserve the credit for this book. With their support, I can do anything. 10
Description: