Thursday, September 27, 2012

Tomcat's Architecture


Tomcat's Architecture and the main configuration file "server.xml"
Tomcat is an HTTP server. Tomcat is also a container that can execute Java Servlet, and converting JavaServer Pages (JSP) and JavaServerFaces (JSF) to Java Servlet. Tomcat employs a hierarchical and modular architecture as
illustrated:

Tomcat's main configuration file is the "server.xml", kept under the $CATALINA_HOME\conf directory.
Context (Application or Webapp)
A web context is a single web application (webapp). It is the lowest-level container, that you can define
components such as Realm and Valve. By default, all webapps are kept under the $CATALINA_HOME\webapps
directory (as configured in the <host> element appBase attribute.
A Java webapp may contain many types of files, such as HTML, CSS, Scripts, images, JSP, servlet, utility classes,
external library jar-files. A Java webapp must follow a strict directory structure as depicted in the Servlet/JSP
specifications. This enables deployment in a Java-capable web server (such as Apache Tomcat and Glassfish). The
resources must be kept in the correct directories and sub-directories.
The URL of a webapp, by default, is the same as the base directory name (or context root) of the webapp.

Webapp's Directory Structure
The directory structure of a webapp is as follows:


·        "ContextRoot": contains the resources that are visible and accessible by the web clients, such as HTML, CSS,Scripts and images. These resources will be delivered to the clients as it is. You could create sub-directories such as images, css and scripts, to further categories the various resources.


o   "ContextRoot\WEB-INF": This directory, although under the context root, is NOT visible to the web users. In other words, it is NOT accessible by the clients directly (for security reason). This is where you keep your application-specific configuration files such as "web.xml". It's sub-directories contain program classes, source files, and libraries.


§  "ContextRoot\WEB-INF\src": Keeps the Java program source files. It is optional but a good practice to separate the source files and classes to facilitate deployment.

§  "ContextRoot\WEB-INF\classes": Keeps the Java classes (compiled from the source codes). Classes defined in packages must be kept according to the Java package directory structure.

§  "ContextRoot\WEB-INF\lib": Keeps the libraries (jar-files), which are provided by other packages, specific and available to this webapp only.


o   "ContextRoot\META-INF\": This is also a hidden directory, to keep resources and configurations (e.g., "context.xml") related to the server. In contrast, "WEB-INF" is for resources related to this web application, independent of the server.


Webapp-Specific Configuration Files
These are the configuration files specific to a webapp: 

(a) WEB-INF\web.xml
(b) META-INF\context.xml.


You can configure a webapp in many ways: 

(a) Write a <context> element in server.xml under <Host> element,
(b) contextRoot\META-INF\context.xml
(c) conf\Catalina\localhost\webapp.xml
(d) conf\context.xml


Tomcat's Directory Structure

Tomcat installation provides these directories:

bin: for Tomcat's binary codes.

conf: global configuration applicable to all the webapps. The default installation provides:
One policy file: catalina.policy for specifying security policy.
Two properties files: catalina.properties and logging.properties,
Four configuration files: server.xml (Tomcat main configuration file), web.xml (global web application
deployment descriptors), context.xml (global Tomcat-specific configuration options) and tomcatusers.
xml (a database of user, password and role for authentication and access control).
The conf also contain a sub-directory for each engine, e.g., Catalina, which in turn contains a sub-subdirectory
for each of its hosts, e.g., localhost. You can place the host-specific context information (similar to
context.xml, but named as webapp.xml for each webapp under the host).

lib: Keeps the JAR-file that are available to all webapps. The default installation include servlet-api.jar,
jasper.jar (JSP), jasper-el.jar (EL). You may also keep the MySQL JDBC driver (mysql-connectorjava-
5.1.xx-bin.jar), and JSTL (jstl.jar and standard.jar) here.

logs: contains the engine logfile Catalina.yyyy-mm-dd.log, host logfile localhost.yyyy-mm-dd.log,
and other application logfiles such as manger and host-manager. The access log (created by the
AccessLogValve) is also kept here.

temp: temporary files used by Tomcat.

webapps: the default appBase - web applications base directory of the host localhost.

No comments:

Post a Comment