Vous êtes sur la page 1sur 43

Jakarta Tomcat:

A look inside a servlet container

What well cover:


History of Tomcat
Relationship to J2EE Tomcat Features Tomcat Architecture

In the beginning..
Web servers serving static html
Web servers using cgi for dynamic content Modules to handle dynamic content
Ex: Apache and Mod-Perl

Then along came servlets

Web Containers Using Servlets and JSPs


Java alternative to CGI
Servlets - Java with a bit of HTML thrown in JSPs (JavaServer Pages) - HTML with a bit of

Java thrown in JSP converted to a Servlet the first time accessed Servlets stay in memory - big plus over CGI

History of Tomcat
Java Web Server
First servlet container Created by Sun to demonstrate Servlet technology

Jserv
Integrated with Apaches web server Created by ASF (Apache Software Foundation)

Tomcat 3.x
Merger of Java Web Server & Jserv Reference implementation of Servlet & JSP specs
5

History of Tomcat (cont)


Tomcat 4.0
released in 2001 Major architecture overhaul

Tomcat 5.x
the most recent version Builds on version 4 Updated to implement Suns latest Servlet & JSP specifications

How does this fit into J2EE?


What is J2EE?
Java 2, Enterprise Edition For creating distributed applications Open standard created by Sun For application servers and apps that connect to them Deployment tools EJB container - session beans, entity beans Servlet container - servlets and JSPs Web server - html pages
7

Components of a J2EE application server:

J2EE (cont)
Examples of J2EE application servers
BEA Weblogic IBM Websphere Jboss

Tomcat & J2EE


Just implements the servlet container and web server portions of the specification Not a full-fledged application server
8

Tomcat Features
Runs as a service (Windows) or daemon(Unix)
Run stand-alone or on beefier web server Manager Application provides tools:
Deploy, undeploy, list, start and stop applications

Shared hosting - handling multiple sites Clustering - running multiple instances


For scalability and availability

How Does Tomcat Work?


Nested hierarchy of components:
Top-level components - containers Nested components - reside in containers and do not contain other components

Components are all configurable Not all components are required

10

Directory Structure
bin Start and Stop Server Scripts + useful Tools common common used classes and libraries (Tomcat and Applications) conf Config files of Tomcat
11

Directory Structure
logs

Logs all log files of Tomcat server classes and libraries only for Tomcat + system Web applications, e.g. the manager App or the Admin Tool

12

Directory Structure
shared

classes and libraries only for Web applications temp the JVM temporary files puts here webapps contains all user-defined Web applications work contains temporary files of Tomcat, e.g. to Servlets compiled JSP files
13

Directory Structure

14

Simple Servlet Container

Servlet Container Client1 Servlet1

Client2

Servlet2
Client3
15

Tomcat Components
Server
Service Connectors Engine Realm

Valves
Logger Host Context

16

The Major Components


Service - has one or more connectors

and one or more containers Catalina - the servlet container Jasper - the JSP engine Coyote - the web connector
17

The Connector
Manages the connection between

applications and clients Point at which client requests are received Implements org.apache.catalina.Connector Example implementations:
Coyote (HTTP) - the default Apache Jserv Protocol (SSL)
18

The Connector (cont)


Waits for incoming HTTP requests
Parses HTTP request headers Creates request and response objects Passes these objects to the container Default port for HTTP apps is 8080 but can

change it to 80 for stand-alone mode

19

The Container/Engine
Request processing component
Passes the request and response objects

from connector to the servlet Implements org.apache.catalina.Container Catalina is the default implementation

20

The Container/Engine (cont)


Passes objects to the Servlet through service() On first request for a servlet, the container: loads the servlet class calls the servlets init() method calls servlets service() method On subsequent requests for the servlet the container only calls service()

21

Servlet Lifecycle in Tomcat


Container instantiates the Servlet and calls

init() Container gets a request and calls service() The Servlet receives HttpServletRequest and HttpServletResponse objects Servlet finds HTTP request data in the request object - ex: form data, cookies, headers
22

Servlet Lifecyle (cont)


Servlet processes the data and writes

output to the response object Container passes the response object back to the connector Connector sends reply back to client Container calls destroy() before removing a servlet instance
23

Why Tomcat?
Highly configurable
Modular design means you can

customize it by writing your own modules No longer just a reference implementation


24

Server Configuration Reference

Configuration directives that can be included in a conf/server.xml file to configure the behavior of the Tomcat 4 servlet/JSP container The configuration element descriptions are organized into the following major categories: Top Level Elements - <Server> is the root element of the entire configuration file, while <Service> represents a group of Connectors that is associated with an Engine. Connectors - Represent the interface between external clients sending requests to (and receiving responses from) a particular Service. Containers - Represent components whose function is to process incoming requests, and create the corresponding responses. An Engine handles all requests for a Service, a Host handles all requests for a particular virtual host, and a Context handles all requests for a specific web application. Nested Components - Represent elements that can be nested inside the element for a Container. Some elements can be nested inside any Container, while others can only be nested inside a Context

25

The Server Component


A Server element represents the entire Catalina servlet container. Therefore, it must be the single outermost element in the conf/server.xml configuration file. Its attributes represent the characteristics of the servlet container as a whole.

26

Common Attributes All implementations of Server support the following attributes


Attribute
className

Description
Java class name of the implementation to use. This class must implement the org.apache.catalina.Server interface. If no class name is specified, the standard implementation will be used The TCP/IP port number on which this server waits for a shutdown command. This connection must be initiated from the same server computer that is running this instance of Tomcat The command string that must be received via a TCP/IP connection to the specified port number, in order to shut down Tomcat

port

shutdown

27

Standard Implementation The standard implementation of Server is org.apache.catalina.core.StandardServer. It supports the following additional attributes (in addition to the common attributes listed above):
Attribute Description
The level of debugging detail logged by this Server to the associated Logger. Higher numbers generate more detailed output. If not specified, the default debugging detail level is zero (0).

debug

28

Nested Components
The following components may be nested

inside a Server element:


Service - One or more service element. GlobalNamingResources - Configure the JNDI global resources for the server

29

The Service Component


A Service element represents the

combination of one or more Connector components that share a single Engine component for processing incoming requests. One or more Service elements may be nested inside a Server element.

30

Common Attributes
Attribute
className

Description
Java class name of the implementation to use. This class must implement the org.apache.catalina.Service interface. If no class name is specified, the standard implementation will be used The display name of this Service, which will be included in log messages if you utilize standard Catalina components. The name of each Service that is associated with a particular Server must be unique

name

31

Standard ImplementationThe standard implementation of Service is org.apache.catalina.core.StandardService. It supports the following additional attributes (in addition to the common attributes listed above):

Attribute
debug

Description
The level of debugging detail logged by this Service to the associated Logger. Higher numbers generate more detailed output. If not specified, the default debugging detail level is zero (0).

32

Nested Components
The only components that may be nested

inside a Service element are one or more Connector elements, followed by exactly one Engine element

33

The Context Container


The Context element represents a web application, which is run within a particular virtual host The web application used to process each HTTP request is selected by Catalina based on matching the Request URI against the context path of each defined Context. Once selected, that Context will select an appropriate servlet to process the incoming request, according to the servlet mappings defined in the web application deployment descriptor file (which MUST be located at /WEB-INF/web.xml within the web app's directory hierarchy). You may define as many Context elements as you wish, nested within a Host element in conf/server.xml. Each such Context MUST have a unique context path, which is defined by the path attribute In addition, you MUST define a Context with a context path equal to a zerolength string. This Context becomes the default web application for this virtual host, and is used to process all requests that do not match any other Context's context path.

34

Common Attributes All implementations of Context support the following attributes


Attribute
className

Description
Java class name of the implementation to use. This class must implement the org.apache.catalina.Context interface. If not specified, the standard value (defined below) will be used. Set to true if you want cookies to be used for session identifier communication if supported by the client (this is the default). Set to false if you want to disable the use of cookies for session identifier communication, and rely only on URL rewriting by the application The Document Base (also known as the Context Root) directory for this web application, or the pathname to the web application archive file (if this web application is being executed directly from the WAR file).
35

cookies

docBase

Common Attributes All implementations of Context support the following attributes


Attribute
path

Description
The context path of this web application, which is matched against the beginning of each request URI to select the appropriate web application for processing. All of the context paths within a particular Host must be unique. If you specify a context path of an empty string (""), you are defining the default web application for this Host, which will process all requests not assigned to other Contexts

36

The Engine Container


The Engine element represents the entire request processing machinery associated with a particular Catalina Service. It receives and processes all requests from one or more Connectors, and returns the completed response to the Connector for ultimate transmission back to the client Exactly one Engine element MUST be nested inside a Service element, following all of the corresponding Connector elements associated with this Service..
37

Common Attributes All implementations of Engine support the following attributes:


Attribute
defaultHost

Description
The default host name, which identifies the Host that will process requests directed to host names on this server, but which are not configured in this configuration file. This name MUST match the name attributes of one of the Host elements nested immediately inside Logical name of this Engine, used in log and error messages

name

38

Nested Components
You can nest one or more Host elements inside this Engine element,

each representing a different virtual host associated with this server. At least one Host is required, and one of the nested Hosts MUST have a name that matches the name specified for the defaultHost attribute, listed above You can nest at most one instance of the following utility components by nesting a corresponding element inside your Engine element
Logger - Configure a logger that will receive and process all log messages for this Engine, plus messages from Connectors associated with this Engine in the surrounding Service. In addition, this Logger will log messages from subordinate Hosts and Contexts, unless overridden by a Logger configuration at a lower level. Realm - Configure a realm that will allow its database of users, and their associated roles, to be shared across all Hosts and Contexts nested inside this Engine, unless overridden by a Realm configuration at a lower level.
39

The Host Container


The Host element represents a virtual host,

which is an association of a network name for a server (such as "www.mycompany.com" with the particular server on which Catalina is running

40

Common Attributes
All implementations of Host support the following attributes:

Attribute
appBase

Description
The Application Base directory for this virtual host. This is the pathname of a directory that may contain web applications to be deployed on this virtual host. You may specify an absolute pathname for this directory, or a pathname that is relative to the $CATALINA_HOME directory. Network name of this virtual host. One of the Hosts nested within an Engine MUST have a name that matches the defaultHost setting for that Engine.

name

41

Nested Components
You can nest one or more Context elements inside this Host

element, each representing a different web application associated with this virtual host. In addition, you can nest a single DefaultContext element that defines default values for subsequently deployed web applications. You can nest at most one instance of the following utility components by nesting a corresponding element inside your Host element Logger - Configure a logger that will receive and process all log messages for this Host, plus messages from Contexts associated with this Host . Realm - Configure a realm that will allow its database of users, and their associated roles, to be shared across all Contexts nested inside this Host

42

References
How Tomcat Works: A Guide to Developing Your Own Java Servlet Container
(Kurniawan & Deck)

J2EE 1.4: The Big Picture


(Haugland, Cade, & Orapallo)

Java Extreme Programming Cookbook


(Burke & Coyner)

The Apache Jakarta Tomcat Website


(http://jakarta.apache.org/tomcat/)

Tomcat-Einfuehrung.pdf
43

Vous aimerez peut-être aussi