You are previewing How Tomcat Works: A Guide to Developing Your Own Java Servlet Container.
O'Reilly logo
How Tomcat Works: A Guide to Developing Your Own Java Servlet Container

Book Description

How Tomcat Works is the only book that explains the internal workings of Tomcat, the open source project used by millions of Java developers. Unlike other Tomcat titles, it is unique because it does not simply covers the configuration or servlet development with Tomcat. Rather, this book is meant for advanced readers interested in writing their own Tomcat modules or in understanding more beyond servlet/JSP programming.
The authors of this book have cracked open Tomcat 4 and 5 and revealed the internal workings of each component. Upon understanding the contents of this book, you will be able to develop your own Tomcat components or extend the existing ones.

In particular, this book explains:
- How to develop Java web servers
- Whether or not more Tomcat creates more than one instance for each servlet
- How Tomcat runs a servlet that implements the SingleThreadModel interface
- Why you cannot change the value of request parameters.
- The two main modules of a servlet container: connector and container
- How to build your own connector or extend the existing ones
- The four types of containers: engine, host, context, wrapper
- How Tomcat manages sessions and how to extend the session manager in a distributed environment.
- The class loader and how to create custom loaders
- How Tomcat implements security and basic/form-based/digest authentication
- How realms and login configuration work.
- How Tomcat processes the configuration file (server.xml) and converts the XML elements to Java objects using Digester
- Tomcat's shutdown hook
- JMX, Apache's Commons Modeler, and Tomcat JMX manageable resources

Table of Contents

  1. Copyright
    1. Dedication
  2. Introduction
    1. Who This Book Is for
    2. How A Servlet Container Works
    3. Catalina Block Diagram
    4. Tomcat 4 and 5
    5. Overview of Each Chapter
    6. The Application for Each Chapter
    7. Preparing the Prerequisite Software
  3. 1. A Simple Web Server
    1. The Hypertext Transfer Protocol (HTTP)
      1. HTTP Requests
      2. HTTP Responses
    2. The Socket Class
    3. The ServerSocket Class
    4. The Application
      1. The HttpServer Class
      2. The Request Class
      3. The Response Class
      4. Running the Application
    5. Summary
  4. 2. A Simple Servlet Container
    1. The javax.servlet.Servlet Interface
    2. Application 1
      1. The HttpServer1 Class
      2. The Request Class
      3. The Response Class
      4. The StaticResourceProcessor Class
      5. The ServletProcessor1 Class
      6. Running the application
    3. Application 2
      1. Running the application
    4. Summary
  5. 3. Connector
    1. The StringManager Class
    2. The Application
      1. Starting the Application
      2. The Connector
      3. Creating an HttpRequest Object
        1. Reading the socket’s input stream
        2. Parsing the Request Line
        3. Parsing Headers
        4. Parsing Cookies
        5. Obtaining Parameters
      4. Creating a HttpResponse Object
      5. Static resource processor and servlet processor
      6. Running the Application
    3. Summary
  6. 4. Tomcat Default Connector
    1. HTTP 1.1 New Features
      1. Persistent Connections
      2. Chunked Encoding
      3. Use of the 100 (Continue) Status
    2. The Connector interface
    3. The HttpConnector Class
      1. Creating a Server Socket
      2. Maintaining HttpProcessor Instances
      3. Serving HTTP Requests
    4. The HttpProcessor Class
    5. Request Objects
    6. Response Objects
    7. Processing Requests
      1. Parsing the Connection
      2. Parsing the Request
      3. Parsing Headers
    8. The Simple Container Application
      1. Running the Application
    9. Summary
  7. 5. Container
    1. The Container Interface
    2. Pipelining Tasks
      1. The Pipeline Interface
      2. The Valve Interface
      3. The ValveContext Interface
      4. The Contained Interface
    3. The Wrapper Interface
    4. The Context Interface
    5. The Wrapper Application
      1. ex05.pyrmont.core.SimpleLoader
      2. ex05.pyrmont.core.SimplePipeline
      3. ex05.pyrmont.core.Simple Wrapper
      4. ex05.pyrmont.core.SimpleWrapperValve
      5. ex05.pyrmont.valves.ClientIPLoggerValve
      6. ex05.pyrmont.valves.HeaderLoggerValve
      7. ex05.pyrmont.startup.Bootstrap1
      8. Running the Application
    6. The Context Application
      1. ex05.pyrmont.core.SimpleContextValve
      2. ex05.pyrmont.core.SimpleContextMapper
      3. ex05.pyrmont.core.SimpleContext
      4. ex05.pyrmont.startup.Bootstrap2
      5. Running the Application
    7. Summary
  8. 6. Lifecycle
    1. The Lifecycle Interface
    2. The LifecycleEvent Class
    3. The LifecycleListener Interface
    4. The LifecycleSupport Class
    5. The Application
      1. ex06.pyrmont.core.SimpleContext
      2. ex06.pyrmont.core.SimpleContextLifecycleListener
      3. ex06.pyrmont.core.SimpleLoader
      4. ex06.pyrmont.core.SimplePipeline
      5. ex06.pyrmont.core.Simple Wrapper
      6. Running the Application
    6. Summary
  9. 7. Logger
    1. The Logger Interface
    2. Tomcat’s Loggers
      1. The LoggerBase Class
      2. The SystemOutLogger Class
      3. The SystemErrLogger Class
      4. The FileLogger Class
        1. The open method
        2. The close method
        3. The log method
    3. The Application
    4. Summary
  10. 8. Loader
    1. Java Class Loader
    2. The Loader Interface
    3. The Reloader Interface
    4. The WebappLoader Class
      1. Creating A Class Loader
      2. Setting Repositories
      3. Setting the Class Path
      4. Setting Permissions
      5. Starting a New Thread for Auto-Reload
    5. The WebappClassLoader Class
      1. Caching
      2. Loading Classes
    6. The Application
      1. Running the Application
    7. Summary
  11. 9. Session Management
    1. Sessions
      1. The Session interface
      2. The StandardSession Class
      3. The StandardSessionFacade Class
    2. Manager
      1. The Manager Interface
      2. The ManagerBase Class
      3. StandardManager
      4. PersistentManagerBase
        1. Swap Out
        2. Back-up
      5. PersistentManager
      6. DistributedManager
    3. Stores
      1. StoreBase
      2. FileStore
      3. JDBCStore
    4. The Application
      1. The Bootstrap Class
      2. The SimpleWrapperValve Class
      3. Running the Application
    5. Summary
  12. 10. Security
    1. Realm
    2. GenericPrincipal
    3. LoginConfig
    4. Authenticator
    5. Installing the Authenticator Valve
    6. The Applications
      1. The ex10.pyrmont.core.SimpleContextConfig Class
      2. The ex10.pyrmont.realm.SimpleRealm Class
      3. The ex10.pyrmont.realm.SimpleUserDatabaseRealm Class
      4. The ex10.pyrmont.startup.Bootstrap1 Class
      5. The ex10.pyrmont.startup.Bootstrap2 Class
      6. Running the Applications
    7. Summary
  13. 11. StandardWrapper
    1. Sequence of Methods Invocation
    2. SingleThreadModel
    3. StandardWrapper
      1. Allocating the servlet
      2. Loading the Servlet
      3. The ServletConfig Object
        1. getServletContext
        2. getServletName
        3. getInitParameter
        4. getInitParameterNames
      4. Parent and Children
    4. StandardWrapperFacade
    5. StandardWrapperValve
    6. FilterDef
    7. ApplicationFilterConfig
    8. ApplicationFilterChain
    9. The Application
      1. Running the Applications
    10. Summary
  14. 12. StandardContext
    1. StandardContext Configuration
      1. StandardContext Class’s Constructor
      2. Starting StandardContext
      3. The Invoke Method
    2. StandardContextMapper
    3. Support for Reloading
    4. The backgroundProcess Method
    5. Summary
  15. 13. Host and Engine
    1. The Host Interface
    2. StandardHost
    3. StandardHostMapper
    4. StandardHostValve
    5. Why You Cannot Live without a Host
    6. Application 1
      1. Running the Applications
    7. The Engine Interface
    8. StandardEngine
    9. StandardEngineValve
    10. Application 2
      1. Running the Applications
    11. Summary
  16. 14. Server and Service
    1. Server
    2. StandardServer
      1. The initialize Method
      2. The start Method
      3. The stop Method
      4. The await Method
    3. Service
    4. StandardService
      1. Container and Connector(s)
      2. Lifecycle Methods
    5. The Application
      1. The Bootstrap Class
      2. The Stopper Class
      3. Running the Applications
    6. Summary
  17. 15. Digester
    1. Digester
      1. The Digester Class
        1. Creating Objects
        2. Setting Properties
        3. Calling Methods
        4. Creating Relationships between Objects
        5. Validating the XML Document
      2. Digester Example 1
      3. Digester Example 2
      4. The Rule Class
      5. Digester Example 3: Using RuleSet
    2. ContextConfig
      1. The defaultConfig Method
      2. The applicationConfig Method
      3. Creating Web Digester
    3. The Application
      1. Running the Applications
    4. Summary
  18. 16. Shutdown Hook
    1. A Shutdown Hook Example
    2. Shutdown Hook in Tomcat
    3. Summary
  19. 17. Tomcat Startup
    1. The Catalina Class
      1. The start Method
      2. The stop Method
      3. Start Digester
      4. Stop Digester
    2. The Bootstrap Class
    3. Running Tomcat on Windows
      1. Introduction to Writing Batch Files
        1. rem
        2. pause
        3. echo
        4. echo off
        5. @echo off
        6. set
        7. label
        8. goto
        9. if
        10. not
        11. exist
        12. Accepting parameters
        13. shift
        14. call
        15. setLocal
        16. start
      2. The catalina.bat Batch File
      3. Starting Tomcat on Windows
      4. Stopping Tomcat on Windows
    4. Running Tomcat on Unix/Linux
      1. Introduction to Writing Unix/Linux Shell Scripts
        1. comment
        2. clear
        3. exit
        4. echo
        5. Calling A Function
        6. System and User Defined Variables
        7. expr
        8. Accessing parameters
        9. shift
        10. if ... then ... [else ... ] fi
        11. for Loop
        12. while Loop
        13. case
        14. Output Redirection
        15. Conditional Executions
      2. The catalina.sh File
      3. Starting Tomcat on Linux/Unix
      4. Stopping Tomcat on Linux/Unix
    5. Summary
  20. 18. Deployer
    1. Deploying A Web Context
      1. Deploying a Descriptor
      2. Deploying a WAR File
      3. Deploying a Directory
      4. Live Deploy
    2. The Deployer Interface
    3. The StandardHostDeployer Class
      1. Installing a Descriptor
      2. Installing a WAR file and a Directory
      3. Starting A Context
      4. Stopping A Context
    4. Summary
  21. 19. Manager Servlet
    1. Using the Manager Application
    2. The ContainerServlet Interface
    3. Initializing ManagerServlet
    4. Listing Deployed Web Applications
    5. Starting A Web Application
    6. Stopping A Web Application
    7. Summary
  22. 20. JMX-Based Management
    1. Introduction to JMX
    2. The JMX API
      1. MBeanServer
      2. ObjectName
    3. Standard MBeans
    4. Model MBeans
      1. MBeanInfo and ModelMBeanInfo
      2. ModelMBean Example
    5. Commons Modeler
      1. MBean Descriptor
        1. mbean
        2. attribute
        3. operation
        4. parameter
        5. Example mbean Element
      2. Writing Your Own Model MBean Class
      3. Registry
      4. ManagedBean
      5. BaseModelMBean
      6. Using the Modeler API
    6. Catalina’s MBeans
      1. ClassNameMBean
      2. StandardServerMBean
      3. MBeanFactory
      4. MBeanUtil
    7. Catalina’s MBeans Creation
    8. The Application
    9. Summary