Table Of Content785000FM.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 orders-ny@springer-sbm.com, 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 info@apress.com, 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 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 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
785000FM.qxd 2/28/07 11:23 AM Page x
x ■ CONTENTS
■ CHAPTER 6 Using Tomcat’s Administration Tools. . . . . . . . . . . . . . . . . . . . . . 91
UsingtheManagerApplication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
SettingUptheManagerApplication. . . . . . . . . . . . . . . . . . . . . . . . . . . 92
ConfiguringtheManagerApplication. . . . . . . . . . . . . . . . . . . . . . . . . . 93
UsingtheManagerApplication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Troubleshooting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
ManagingApplicationswithAnt. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
UsingtheTomcatAdministrationTool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
■ CHAPTER 7 Configuring Tomcat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
UsingValvestoInterceptUserRequests. . . . . . . . . . . . . . . . . . . . . . . . . . . 111
StandardValves. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
ConfiguringUserSessions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
ConfiguringaSessionManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
ConfiguringaCluster. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
■ CHAPTER 8 Understanding Tomcat’s Class Loaders. . . . . . . . . . . . . . . . . . 133
ExaminingtheStandardJavaSEClassLoaders. . . . . . . . . . . . . . . . . . . . 133
TheBootstrapClassLoader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
TheExtensionClassLoader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
TheSystemClassLoader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
TheDelegationModel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
TheEndorsedStandardsOverrideMechanism . . . . . . . . . . . . . . . . 135
UnderstandingClassLoaderAttributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
LoadingClassesonDemand. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
ClassCaching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
SeparateNamespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
CreatingaCustomClassLoader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
UnderstandingSecurityandClassLoaders . . . . . . . . . . . . . . . . . . . . . . . . 136
ClassLoaderDelegation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
CoreClassRestriction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
SeparateClassLoaderNamespaces . . . . . . . . . . . . . . . . . . . . . . . . . 137
SecurityManager. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
32eeceee020b1b6c36f7005aec98cc94
785000FM.qxd 2/28/07 11:23 AM Page xi
■ CONTENTS xi
UnderstandingTomcatandClassLoaders . . . . . . . . . . . . . . . . . . . . . . . . . 137
TomcatandtheSystemClassLoader . . . . . . . . . . . . . . . . . . . . . . . . 138
Tomcat’sCommonClassLoader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Tomcat’sWebApplicationClassLoader. . . . . . . . . . . . . . . . . . . . . . . 140
RevisitingClassLoaderOrder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
DynamicClassReloading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
AvoidingClassLoaderPitfalls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
PackagesSplitAmongDifferentClassLoaders. . . . . . . . . . . . . . . . 141
Singletons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
■ CHAPTER 9 Using Tomcat’s Connectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
UsingtheHTTPConnector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
ConfiguringtheHTTP/1.1Connector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
ConfiguringSSLonTomcat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
WorkingwithKeystores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
RunningTomcatBehindaProxyServer. . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
UsingtheAJPConnector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
TheApacheJServProtocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
WorkerImplementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
IntegratingTomcatwithApache1.3Usingmod_jk. . . . . . . . . . . . . 147
IntegratingTomcatwithIIS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
ConfiguringDistributedNetworkswithTomcat . . . . . . . . . . . . . . . . . . . . . 158
UnderstandingTomcatLoadBalancing. . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
PreparingforLoadBalancing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
TheWorkers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
ConfiguringApache1.3forLoadBalancing . . . . . . . . . . . . . . . . . . . 161
ConfiguringTomcatforLoadBalancing. . . . . . . . . . . . . . . . . . . . . . . 163
TestingtheLoadBalancingBehavior. . . . . . . . . . . . . . . . . . . . . . . . . 165
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
■ CHAPTER 10 Connecting to Databases Using JDBC. . . . . . . . . . . . . . . . . . . . 167
IntroducingSQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
IntroducingJDBC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
RunningBasicJDBCOperations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
WhichJDBCVersion? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
ExaminingJDBCDriverTypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
DatabaseConnectionPooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
785000FM.qxd 2/28/07 11:23 AM Page xii
xii ■ CONTENTS
UsingTomcatandJDBC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
ProvidingJDBCDataSourcesinTomcat. . . . . . . . . . . . . . . . . . . . . . 171
ConfiguringJNDIJDBCResources. . . . . . . . . . . . . . . . . . . . . . . . . . . 171
UsingtheResourceandResourceParamsElements. . . . . . . . . . . . 172
TransactionsandDistributedTransactionsSupport. . . . . . . . . . . . . 172
TestingJNDIResourceConfiguration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
CreatingtheMySQLTestDatabase . . . . . . . . . . . . . . . . . . . . . . . . . . 173
SettingUptheRead-OnlyUser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
AddingtheJDBCJNDIResourcetotheServer. . . . . . . . . . . . . . . . . 175
UsingJNDItoLookUpaDataSource . . . . . . . . . . . . . . . . . . . . . . . . 176
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
■ CHAPTER 11 Working with User Authentication. . . . . . . . . . . . . . . . . . . . . . . . 179
LookingatRealms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
UnderstandingContainer-ManagedSecurity . . . . . . . . . . . . . . . . . . . . . . . 180
StoringaDigestedPassword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
ConfiguringRealms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
ConfiguringaFile-BasedRealm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
ConfiguringaUserDatabaseRealm. . . . . . . . . . . . . . . . . . . . . . . . . . 185
ProtectingaResourcewithaRealm . . . . . . . . . . . . . . . . . . . . . . . . . 190
ConfiguringaJDBCRealm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
ConfiguringJNDIRealms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
■ CHAPTER 12 Securing Tomcat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
SecuringtheWindowsFileSystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
ControllingUsers,Groups,andOwnersinWindows . . . . . . . . . . . . 212
AssigningPermissionsinWindows. . . . . . . . . . . . . . . . . . . . . . . . . . . 216
PlanningSecurityPermissionsinWindows. . . . . . . . . . . . . . . . . . . . 217
ConfiguringFilePermissionsinWindows . . . . . . . . . . . . . . . . . . . . . 219
SecuringtheUnixFileSystem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
ControllingUsers,Groups,andOwnersinUnix . . . . . . . . . . . . . . . . 221
AssigningPermissionsinUnix. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
PlanningSecurityPermissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
ConfiguringFilePermissionsinUnix . . . . . . . . . . . . . . . . . . . . . . . . . 225
ExaminingGeneralTomcatSecurityPrinciples . . . . . . . . . . . . . . . . . . . . . 226
RetainingTomcat’sAdministrationTools. . . . . . . . . . . . . . . . . . . . . . 226
Read-OnlywebappsDirectory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
SecuringYourFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
KnowingIfYourSecurityHasBeenViolated. . . . . . . . . . . . . . . . . . . 227
Read-OnlyFileSystems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
785000FM.qxd 2/28/07 11:23 AM Page xiii
■ CONTENTS xiii
SecuringTomcat’sDefaultConfiguration . . . . . . . . . . . . . . . . . . . . . . . . . . 228
SecuringTomcat’sPermissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
TheJavaSecurityManager. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
UsingtheSecurityManagerwithTomcat . . . . . . . . . . . . . . . . . . . . . 232
Tomcat’sPolicyFile. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
RecommendedSecurityManagerPractices. . . . . . . . . . . . . . . . . . . 236
UsingSecurityRealms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
AddingSettingstoweb.xml. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
ChoosingForm-BasedAuthentication . . . . . . . . . . . . . . . . . . . . . . . . 239
UsingCustomLoginandErrorPages. . . . . . . . . . . . . . . . . . . . . . . . . 240
UsingtheSecureSocketsLayer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
InstallingJSSE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
PreparingtheCertificateKeystore . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
InstallingaCertificatefromaCertificateAuthority . . . . . . . . . . . . . 244
ImportingtheCertificate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
ProtectingResourceswithSSL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
ConfiguringtheSSLConnector. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
UsingSSLwiththeApacheWebServer. . . . . . . . . . . . . . . . . . . . . . . 246
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
■ CHAPTER 13 Implementing Shared Tomcat Hosting. . . . . . . . . . . . . . . . . . . . 253
ExaminingVirtualHosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
IP-BasedVirtualHosting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Name-BasedVirtualHosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
ImplementingVirtualHostingwithTomcat. . . . . . . . . . . . . . . . . . . . . . . . . 257
CreatinganExampleConfiguration . . . . . . . . . . . . . . . . . . . . . . . . . . 258
SettingUptheVirtualHosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
TestingtheVirtualHosting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
ImplementingVirtualHostingwithApacheandTomcat. . . . . . . . . . . . . . 264
SettingaJVMforEachVirtualHost . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
■ CHAPTER 14 Testing Tomcat’s Performance. . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
PreparingforLoadTesting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
ConfiguringtheJavaHeapSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
ConfiguringTomcat’sConnectors. . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
ConfiguringApplicationSessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
AlteringTomcat’sDeploymentArchitecture . . . . . . . . . . . . . . . . . . . 276
WorkingwithaDeveloper’sCode. . . . . . . . . . . . . . . . . . . . . . . . . . . . 276