Tomcat 6 Developer's Guide

Book description

Build better web applications by learning how a servlet container actually works.

  • Take your Java EE web programming skills to the next level by getting an expert's level understanding of the servlet specification and its reference implementation, Apache Tomcat.
  • Build a Tomcat distribution from its source code, and explore the components, classes, and technologies that make up this container.
  • Use standard development tools such as Eclipse, Ant, and Subversion to dissect a Tomcat distribution.
  • Discover the touch points between the servlet specification and a servlet container's implementation.
  • Acquire specialist grade skills in a range of technologies that contribute to Java server side development.

In Detail

While Tomcat is one of the most popular servlet containers, its inner workings still remain a mystery to many developers. If you only have a superficial familiarity of how this container actually functions, much of its power remains untapped and underutilized. This book will provide you with all that you need to undertand how to effectively use Apache Tomcat.

This book begins by providing detailed instructions on building a Tomcat distribution. The next few chapters introduce you to the conceptual underpinnings of web servers, the Java EE and servlet specifications, and the Tomcat container. Subsequent chapters address the key Tomcat components, taking care to provide you with the information needed to understand the internal workings of each component. Detailed examples let you walk through a Tomcat installation, stepping into key Tomcat components, as well as into your own custom servlets.

During the course of the book you will encounter various structural components such as the Server and Service; containers such as the Engine, Host, Context, and Wrapper; and helpers such as the Loader, Manager, and Valve. You will also see how Tomcat implements the JNDI API to provide both a directory service for storage agnostic access to its resources, as well as a naming service that implements the Java EE Environment Naming Context.

Along the way you will learn how various elements of the servlet 2.5 specification, as well as the HTTP RFCs are implemented by a servlet container. By the end of your journey, you will be able to count yourself as part of the elite minority of Java EE web developers who truly understand what goes on under the covers of a servlet container.

Table of contents

  1. Tomcat 6 Developer's Guide
    1. Table of Contents
    2. Tomcat 6 Developer's Guide
    3. Credits
    4. About the author
    5. Acknowledgement
    6. About the reviewers
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Errata
        2. Piracy
        3. Questions
    8. 1. Introduction to Tomcat
      1. What's the story behind Tomcat?
      2. What exactly is Tomcat?
        1. JSPs
        2. Servlet container
      3. Why this book?
      4. Gathering our tools
        1. Java Development Kit (JDK) 6.0
        2. Verifying downloaded files
          1. Apache Ant
          2. Subversion
        3. Obtaining the Tomcat source code
          1. Using Subversion
          2. Tomcat source distribution
          3. Getting ready to build Tomcat
            1. Downloading dependencies
            2. Building Tomcat
          4. Starting up your shiny new Tomcat
        4. Eclipse
      5. Summary
    9. 2. Servlet API Overview
      1. Java Community Process
      2. Java Enterprise Edition platform
        1. What is an enterprise application?
          1. Simultaneous users
          2. Highly available, reliable, and scalable
          3. Heterogeneous environment
          4. Distributed nature
        2. Java EE architecture
          1. Java based
          2. Generic APIs
          3. Java EE containers and container services
          4. Java EE components and their APIs
          5. Java EE Service APIs
        3. What is a typical Java EE application architecture?
      3. Hypertext Transfer Protocol (HTTP)
        1. Uniform Resource Locator, URL
        2. HTTP methods
          1. GET (HTTP/0.9)
          2. POST (HTTP/1.0)
          3. PUT (HTTP/1.1)
          4. DELETE (HTTP/1.1)
          5. TRACE (HTTP/1.1)
          6. OPTIONS (HTTP/1.1)
          7. HEAD (HTTP/1.0)
        3. HTTP requests
        4. HTTP responses
          1. The response line
          2. Response headers
        5. Spying on HTTP
      4. What is the Servlet API?
        1. Servlet container
        2. Core servlet classes
          1. Servlets
          2. ServletConfig interface
          3. ServletContext interface
          4. Multithreading in servlets
          5. Request
            1. Request parameters and attributes
            2. Protocol and URI details
            3. Reading request data
            4. Connection information
            5. HTTP specific information
          6. Response
          7. Sessions
          8. Filters
          9. Listeners
        3. Web applications
          1. Deployment descriptors
      5. Summary
    10. 3. Servlet Container Overview
      1. Component taxonomy
        1. Architectural benefits
        2. Top Level Components
          1. Server
          2. Service
        3. Connectors
        4. Container components
          1. Engine
          2. Virtual host
            1. Basic concepts
            2. Virtual host techniques
              1. IP-based virtual hosting
              2. Name-based virtual hosting
            3. Virtual host aliasing
          3. Context
            1. Context configuration
          4. Wrapper
        5. Nested components
          1. Valve
          2. Realm
          3. Executor
          4. Listener
          5. Manager
          6. Loader
            1. Delegation model
            2. Java EE class loading
            3. Tomcat's additional class loaders
            4. Class reloading in web applications
          7. Logger
          8. Resources
      2. Summary
    11. 4. Starting Up Tomcat
      1. Using scripts
        1. Setting up the environment
        2. Executing the requested command
        3. Setting up your project
          1. Modifying the Run/Debug configuration
        4. Bootstrapping Tomcat
        5. Bootstrapping environment variables
        6. Bootstrapping class loading
        7. Bootstrapping the embedded container
        8. Bootstrapping the Tomcat component hierarchy
          1. Commons Digester
            1. The pattern
            2. The rule
            3. The object stack
          2. Using the Digester
          3. The server.xml Digester
          4. Parsing the server.xml file
          5. Initializing the Server
          6. Adding a new web application to Tomcat in Eclipse
      2. Summary
    12. 5. The Server and Service Components
      1. Setting up your project
        1. Installing MySQL
        2. Installing the MySQL driver
        3. Enhancing the devguide web application
          1. server.xml
          2. Context fragment
          3. Web application deployment descriptor
          4. The contact list servlet
          5. Additional notes
      2. JNDI service
        1. Basic JNDI concepts
          1. Context
          2. InitialContext
          3. Environment naming context
          4. JNDI URLs
          5. Initializing an InitialContext
          6. Looking up names
            1. Using multiple naming implementations
            2. URL context factory lookup implementation details
          7. References
          8. JNDI resources
        2. Using JNDI
          1. Component developer responsibilities
            1. Using JNDI within a web application component
            2. Publishing logical references
              1. <resource-ref>
              2. <resource-env-ref>
              3. <env-entry>
          2. Application deployer responsibilities
            1. Resource definition levels
            2. Binding logical references
              1. <Environment>
              2. <Resource>
              3. <ResourceLink>
      3. StandardServer
        1. Lifecycle
        2. Shutdown
        3. JNDI implementation
          1. Structural relationships
            1. NamingContext
            2. References
            3. Factories
          2. URL context factory
      4. StandardService
      5. Summary
    13. 6. The Connector Component
      1. Connector classification
        1. Usage scenario
          1. Tomcat with external web server
            1. mod_jk
            2. mod_proxy
          2. Tomcat in standalone mode
          3. Protocol
          4. Implementation architecture
            1. Apache Portable Runtime Connector
            2. JIO Connector aka Coyote
            3. NIO connector
        2. Connector configuration
          1. Binding attributes
          2. Socket attributes
          3. Thread parameters
        3. The connector subsystem—a structural perspective
          1. Objective 1: Collection point
          2. Objective 2: Establishing relationships
          3. Objective 3: Accept incoming connections
          4. Objective 4: Maintain a pool of worker threads
          5. Objective 5: Maintain a mapping of request processing components
          6. Objective 6: Insulate the engine from the outside world
        4. Modeling the Request and Response
          1. Coyote and Catalina
          2. Request classes
            1. Coyote Request
            2. Catalina Request
          3. Response classes
          4. HTTP classes
            1. MimeHeaders
            2. Parameters and ParameterMap
            3. Cookies and ServerCookie
          5. Stream classes
            1. InternalInputBuffer
            2. InternalOutputBuffer
            3. InputBuffer
            4. OutputBuffer
            5. ServletInputStream and CoyoteInputStream
            6. ServletOutputStream and CoyoteOutputStream
          6. Helper classes
            1. ByteChunk and CharChunk
            2. MessageBytes
        5. Socket programming
          1. Socket options
            1. SO_TIMEOUT
            2. TCP_NODELAY
            3. SO_LINGER
        6. The connector subsystem—a dynamic perspective
          1. Initializing the Apache Portable Runtime (APR)
          2. Instantiating a Connector
          3. Initializing the Mapper
        7. Receiving a request
          1. Thread notification
          2. End point
          3. Connection handler
            1. ConcurrentLinkedQueue
          4. Http11Processor
          5. CoyoteAdapter
          6. Completing the request
      2. Summary
    14. 7. The Engine Component
      1. Containers
      2. Nested components
      3. Engine
        1. Configuring an Engine
      4. Implementation details
        1. StandardEngine
        2. Container interface
        3. ContainerBase abstract class
          1. Parent and child relationships
          2. Nested components
          3. Valve management
          4. Life cycle methods
          5. Listener management
          6. Background processing
      5. Pipeline
        1. Pipeline execution scope
        2. Valve execution order
        3. StandardPipeline
          1. Valve manipulation
          2. Lifecycle methods
      6. Valve
        1. ValveBase
        2. StandardEngineValve
        3. Request Dumper Valve
        4. Request Filter Valve
        5. AccessLogValve
          1. Valve instantiation
          2. AccessLogElement
            1. Named patterns
          3. Valve startup
          4. Valve invocation
      7. Summary
    15. 8. The Host Component
      1. Virtual hosts
        1. Tomcat virtual hosts
        2. Virtual hosting scenarios
          1. Case 1: Name-based virtual hosting
            1. Aliasing: Multiple domains to a single set of content
          2. Case 2: IP-based virtual hosting
      2. Configuring a host
      3. StandardHost
      4. HostConfig
        1. Contexts and context fragments
        2. DeployedApplication
        3. Lifecycle events
          1. Deploying context fragments
          2. Deploying WAR files and exploded directories
        4. Periodic events
      5. Examples of name-based virtual hosting
        1. Web Application Deployment Descriptor (web.xml)
        2. Context Fragment (context.xml)
        3. webapp1/index.html
        4. build.xml
        5. Aliasing
        6. Name-based virtual hosting
        7. IP-based virtual hosting
      6. StandardHostValve
        1. The servlet error handling mechanism
        2. Exception processing
          1. Finding a custom error page by exception type
          2. Finding a custom error page by response status code
          3. Setting request attributes
          4. Forwarding to the error resource
        3. Method termination
      7. ErrorReportValve
        1. Error page mechanism example
      8. Summary
    16. 9. The Context Component
      1. Aspects of the Context component
        1. The Context aspect
          1. Configuration files
            1. Document base and context path
          2. Context children
            1. WatchedResource
            2. Loader
            3. Resources
            4. ResourceLink
            5. Context parameters
            6. Environment variables
            7. Valves
            8. Manager
            9. Realm
        2. Web application aspect
          1. Configuration files
      2. Web application resources
        1. Resources
        2. Resource cache
        3. Resource retrieval
          1. BaseDirContext
          2. FileDirContext
          3. ProxyDirContext
            1. Looking up resources
            2. Cache lookup
            3. Miscellaneous methods
      3. Shared library mechanism
        1. Manifest file format
      4. URLs and protocol handlers
        1. Accessing resources
        2. Protocol handlers
          1. java.net.URL
          2. java.net.URLStreamHandler
          3. java.net.URLConnection
          4. java.net.URLStreamHandlerFactory
        3. Protocol handler implementation
          1. Locating a stream handler
          2. The jndi protocol
            1. DirContextURLStreamHandlerFactory
            2. DirContextURLStreamHandler
            3. DirContextURLConnection
      5. Java class loading
        1. Advantages of dynamic class loading
        2. Class loader hierarchy
        3. Class loader rules
        4. Implicit and explicit class loading
        5. Tomcat class loading
      6. Implementing a custom class loader
      7. Making a class available
        1. The custom web application class loader
        2. Class loader initialization
          1. ResourceEntry
          2. loadClass()
          3. findClass()
          4. getResource()
      8. Context initialization and startup
        1. Context initialization
          1. Digesting the context fragment
          2. Setting the document base
        2. Context startup
          1. Anti resource locking
          2. Setting up the Context's resources
          3. Setting up the Context's Loader
          4. Publish the work directory
          5. Setting up a servlet context
          6. Validating optional JARs
          7. Naming context
          8. Starting nested components
          9. Configuring the web application aspect
            1. The Mapper
          10. Starting listeners
          11. Starting application filters
          12. Session manager and the background thread
          13. Loading servlets on startup
      9. Request processing
      10. Hot deploying a context
        1. Determining if a reload is necessary
        2. Reloading a context
      11. Context example
        1. Deployment descriptor
        2. Source files
          1. HelloWorldServlet.java
          2. Message.java
          3. Listeners
            1. MyServletContextListener.java
            2. MyServletRequestListener.java
          4. MyFilter.java
      12. Summary
    17. 10. The Wrapper Component
      1. StandardWrapper
      2. Servlet lifecycle
        1. Loading a servlet
      3. Mappers
        1. Mapping rules
          1. Partial URLs
        2. Tomcat mappers
          1. The connector's mapper
            1. Locating the host and context
            2. Locating the wrapper
          2. Setting up the StandardContext mapper
      4. Request processing
      5. Filters
        1. Filter chain construction
        2. Filter chain invocation
      6. Request dispatcher
        1. Obtaining a request dispatcher
        2. Using a request dispatcher
          1. Forwards
          2. Includes
        3. Wrapper classes
          1. Wrapper class structure
          2. Processing a forward
          3. Including a resource
      7. Examples
        1. Servlets
          1. Declaration
        2. ForwardingServlet.java
        3. Static resources
        4. IncludingServlet.java
        5. TemplateServlet.java
      8. Summary
    18. 11. The Manager Component
      1. Session propagation mechanisms
        1. Cookies
        2. URL rewriting
      2. Session
        1. Session identifiers
        2. Session attributes
        3. Session lifetime
          1. Session creation and access
          2. Session validity
          3. Session expiry
          4. Session cleanup
        4. Session lifecycle events
          1. Object binding events
          2. Session lifecycle events
          3. Attribute change events
          4. Session migration events
        5. Session serialization
          1. Serialization
            1. Versioning
            2. Class loading
          2. Serialization in a StandardSession
        6. Session multi threading
      3. Manager
        1. ManagerBase
          1. Session security
            1. Employing a cryptographic Random Number Generator
            2. Generating a session identifier of adequate length
            3. Using an inactive timeout to expire sessions
          2. Generating a session identifier
          3. Session construction
          4. Background processing
          5. Other methods
        2. StandardManager
          1. Manager element attributes
            1. Session identifier generation attributes
            2. Session attributes
          2. Session persistence mechanism
        3. Persistent manager
          1. Persisting sessions
          2. Finding a session
          3. Background processing
        4. Store
          1. FileStore
      4. Summary
    19. Index

Product information

  • Title: Tomcat 6 Developer's Guide
  • Author(s): Damodar Chetty
  • Release date: December 2009
  • Publisher(s): Packt Publishing
  • ISBN: 9781847197283