CYAN YELLOW MAGENTA BLACK PANTONE 123 CV EMPOWERING PRODUCTIVITY FOR THE JAVA™ DEVELOPER THE EXPERT’S VOICE® IN JAVA™ TECHNOLOGY Companion eBook Available Matthew Moodie, Pro Apache Tomcat 6 author of Pro Apache Tomcat 5/5.5 P Pro Apache Ant Dear Reader, r The lightweight, open source Apache Tomcat 6 servlet container is the refer- o ence implementation of the latest JSP™ 2.1 and Servlet 2.5 specifications, Kunal Mittal,author of which means it’s the first server to provide the new specifications’ features. This Pro Apache Beehive also makes it an incredibly popular choice as a web server—it has reached a A Pro Apache BEA WebLogic Server 8.1 significant level of maturity by being adopted by companies and organizations Unleashed from around the world. p Pro Apache Tomcat 6provides accurate, detailed information on how to work with Tomcat’s enterprise-class features out of the box for busy system a administrators and others using Tomcat 6. Though you will explore the theory Tomcat 6 c of Java-based, multi-tiered systems with reference to Tomcat’s place in them, you won’t waste time revisiting JSP or servlet coding skills. Instead, you’ll learn h how to obtain, install, and administer Tomcat 6. You’ll see how Tomcat 6’s built-in features allow you to configure clustering, load balancing, and shared hosting e to enhance its reliability and performance. You’ll also learn how to effectively integrate Tomcat 6 with other popular and necessary systems, including the Apache web server 1.3 and 2.0, Microsoft’s IIS web server, MySQL databases, T and LDAP and ODBC data sources. o Pro Apache Tomcat 6is full of invaluable information that will help you get up to speed on managing Tomcat 6 as quickly as possible. m Yours, Matthew Moodie and Kunal Mittal c a Effectively deploy Tomcat 6 to maximize your JSP™ t THE APRESS JAVA™ ROADMAP and servlet-based web applications. Pro Apache Tomcat 6 Companion eBook 6 Beginning Java™ EE 5: Pro JSP™ 2, From Novice to Professional 4th Edition Pro JSF™ and Ajax: Building See last page for details Rich Internet Components on $10 eBook version ISBN-13: 978-1-59059-785-9 SOURCECODEONLINE ISBN-10: 1-59059-785-0 M Matthew Moodie www.apress.com 53999 ittM ao java.apress.com l,o Edited by Kunal Mittal Ed die US $39.99 ., Shelve in Java Programming 9 781590 597859 User level: Intermediate–Advanced this print for content only—size & color not accurate spine = 0.82" 352 page count 785000FM.qxd 2/28/07 11:23 AM Page i Pro Apache Tomcat 6 Matthew Moodie Edited by Kunal Mittal 785000FM.qxd 2/28/07 11:23 AM Page ii Pro Apache Tomcat 6 Copyright © 2007 by Matthew Moodie 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. ISBN-13 (pbk): 978-1-59059-785-9 ISBN-10 (pbk): 1-59059-785-0 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use atrademark symbol with every occurrence of atrademarked 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. Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was writ- ten without endorsement from Sun Microsystems, Inc. Lead Editor: Steve Anglin Editor: Kunal Mittal Technical Reviewer: Scott Davis Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Paul Sarknas, Jim Sumser, Matt Wade Project Manager: Beth Christmas Copy Edit Manager: Nicole Flores Copy Editor: Heather Lang Assistant Production Director: Kari Brooks-Copony Production Editor: Kelly Gunther Compositor: Kinetic Publishing Services, LLC Proofreader: Elizabeth Berry Indexer: Toma Mulligan Artist: Kinetic Publishing Services, LLC Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [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(s) nor 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.comin the Source Code/Download section. 785000FM.qxd 2/28/07 11:23 AM Page iii To Laura 785000FM.qxd 2/28/07 11:23 AM Page iv 785000FM.qxd 2/28/07 11:23 AM Page v Contents at a Glance About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi ■ CHAPTER 1 IntroducingTomcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 ■ CHAPTER 2 InstallingTomcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 ■ CHAPTER 3 ExaminingTomcat’sDirectories. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 ■ CHAPTER 4 WorkingwithTomcat’sConfigurationFiles. . . . . . . . . . . . . . . . . . . . . . 35 ■ CHAPTER 5 AdministeringWebApplications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 ■ CHAPTER 6 UsingTomcat’sAdministrationTools. . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 ■ CHAPTER 7 ConfiguringTomcat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 ■ CHAPTER 8 UnderstandingTomcat’sClassLoaders. . . . . . . . . . . . . . . . . . . . . . . . 133 ■ CHAPTER 9 UsingTomcat’sConnectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 ■ CHAPTER 10 ConnectingtoDatabasesUsingJDBC . . . . . . . . . . . . . . . . . . . . . . . . . 167 ■ CHAPTER 11 WorkingwithUserAuthentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 ■ CHAPTER 12 Securing Tomcat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 ■ CHAPTER 13 Implementing Shared Tomcat Hosting. . . . . . . . . . . . . . . . . . . . . . . . . 253 ■ CHAPTER 14 Testing Tomcat’s Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 ■ APPENDIX Installing MySQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 ■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 v 785000FM.qxd 2/28/07 11:23 AM Page vi 785000FM.qxd 2/28/07 11:23 AM Page vii Contents About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi ■ CHAPTER 1 Introducing Tomcat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 UnderstandingtheWebToday. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 LookingBeyondCGI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 IntroducingJavaontheWeb. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 AddingtoServlets:JavaServerPages. . . . . . . . . . . . . . . . . . . . . . . . . . 3 IntroducingServletContainers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 LookingatTomcat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 What’sNewinTomcat6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 UnderstandingTomcat’sArchitecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Top-LevelComponents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 TheConnectorComponents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 TheContainerComponents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 TheNestedComponents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 ■ CHAPTER 2 Installing Tomcat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 InstallingJava . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 InstallingJavaonWindows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 InstallingJavaonLinux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 InstallingTomcat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 InstallingTomcatonWindowsUsingtheInstaller . . . . . . . . . . . . . . . 15 InstallingTomcatonWindowsUsingtheZippedFile. . . . . . . . . . . . . 20 InstallingTomcatonLinuxorMacOS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 ViewingtheDefaultInstallation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 RunningTomcatwiththeServerOption . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 InstallingAnt. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 InstallingTomcatfromSource. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 vii 785000FM.qxd 2/28/07 11:23 AM Page viii viii ■ CONTENTS TroubleshootingandTips. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 TheTomcatWindowDisappears. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 ThePortNumberIsinUse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 ■ CHAPTER 3 Examining Tomcat’s Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 LookingatCATALINA_HOME. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 ThebinDirectory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 TheconfDirectory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 ThelogsDirectory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 ThelibDirectory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 ThetempDirectory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 ThewebappsDirectory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 TheworkDirectory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 UnderstandingWebApplicationStructure. . . . . . . . . . . . . . . . . . . . . . . . . . . 31 WebApplicationContext. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 TheWEB-INFDirectory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 TheMETA-INFDirectory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 ■ CHAPTER 4 Working with Tomcat’s Configuration Files. . . . . . . . . . . . . . . 35 ExaminingTomcat’sConfigurationFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Usingcatalina.policyforAccessControl. . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Usingcatalina.propertiestoConfigureTomcat’sClassLoaders. . . . . . . . 39 Usingserver.xmltoConfigureTomcat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 ConfiguringaServer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 ConfiguringGlobalNamingResources. . . . . . . . . . . . . . . . . . . . . . . . . 42 ConfiguringaService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 ConfiguringaConnector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 ConfiguringanEngine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 TomcatLogging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 ConfiguringaRealm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 ConfiguringaHost. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 ConfiguringaValve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 ConfiguringaListener. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 ConfiguringanAlias. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 UnderstandingAuthenticationandthetomcat-users.xmlFile . . . . . . . . . 64 785000FM.qxd 2/28/07 11:23 AM Page ix ■ CONTENTS ix ConfiguringWebApplicationDefaultswithweb.xml. . . . . . . . . . . . . . . . . . 64 DefaultServletDefinitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 MatchingURLs:ServletMappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 ConfiguringSessionTimeout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 ConfiguringMIMEMappings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 ConfiguringWelcomeFiles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 ChangingServiceOptionsonWindows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 ■ CHAPTER 5 Administering Web Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . 71 ConfiguringContexts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 ConfiguringDefaultContexts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 TheContextElement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 ConfiguringaParameter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 ConfiguringaResourceLink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 ExaminingaWebApplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 MappingURLstoResources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 ExaminingtheWEB-INFFolder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Examiningtheweb.xmlFile. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 <distributable> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 <context-param> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 <filter> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 <filter-mapping>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 <servlet> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 <servlet-mapping>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 <session-config> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 <mime-mapping>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 <welcome-file-list> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 <error-page>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 <resource-env-ref> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 <resource-ref> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 <security-constraint>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 <login-config>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 <security-role>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Description: