Tomcat
Sunday, September 8, 2019
10:00 PM
Tomcat 6: webapps
JDBC: it is configured in the **context.xml file of the web application**, have to specify the **factory** attribute and set the value to **org.apache.tomcat.jdbc.pool.DataSourceFactory**, META-INF/services/java.sql.Driver
==> From http://tomcat.apache.org ⇒ Select "Downloads" ⇒ "Tomcat 6.0" ⇒ "6.0.xx" ⇒ "Binary Distributions" ⇒ "Core" ⇒ "zip" ⇒ "apache-tomcat-6.0.xx.zip"
==> Unzip the above and refer to the Tomcat installed directory as $CATALINA_HOME or %CATALINA_HOME%, rename the Tomcats installed directory to a shorter name (says c:\Tomcat6).
==> Environment Variable: JAVA_HOME and set it to the JDK installed directory
==> Configuring $CATALINA_HOME\conf\server.xml :
TCP Port Number: default 8080, you may choose any number between 1024 and 65535 to run your Tomcat Test server.
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" maxThreads="400" redirectPort="8443" />
It is recommended that you store your application under $CATALINA_HOME/webapps, In this case, you do not have to write the <Context> explicitly.
##Context: A web context is simply a web application running under Tomcat Server
#<Context path="/ws" docBase="d:/workshop" reloadable="true">
#=>The "path" attribute refers to the URL Path: http://hostname:8080/ws
#=>The "docbase" attribute refers to the Document Base Directory (also known as the Context Root) in your server’s file system where you store your applications codes
#=>Use Unix-style forward slash '/' as the directory separator in the configuration file, instead of Window-sytle back slash '\'
#=>The attribute reloadable="true" asks tomcat to monitor your servlets for changes, and automatically reload the servlets if changes is detected
Configuring "conf\web.xml" and "conf\context.xml" for "Invoker Servlet"
web.xml : Enable the Directory Listing
web.xml : Enable "Invoker Servlet" and Specify its URL Mapping
context.xml : Enable privilege for "Invoker Servlet"
The purpose of the Invoker Servlet is to allow a web application to dynamically register new servlet definitions that correspond with a <servlet> element in the /WEB-INF/web.xml deployment descriptor, and execute requests utilizing the new servlet definitions.
Start/Stop the Tomcat Server
$CATALINA_HOME\bin\startup.bat
$CATALINA_HOME\bin\shutdown.bat
$CATALINA_HOME\conf\tomcat-users
Java Servlet JAR file:
Servlet API is needed to compile Java servlet, so copy as below...
$CATALINA_HOME\lib\servlet-api.jar into your JDKs extension directory $JAVA_HOME\jre\lib\ext
Alternatively, you could include the Servlet jar- file in the CLASSPATH.
Logging: Check the log files (at $CATALINA_HOME\logs)
conf/logging.properties and conf/server.xml for the access log
( if not, In that case, just put log4j.jar and log4j.properties into WEB-INF/lib and WEB-INF/classes of your web application)
Delete $CATALINA_BASE/conf/logging.properties to prevent java.util.logging generating zero length log files
# log on the host
"localhost"
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].xxx
# log on the host "localhost" for the webapp
foo
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/foo].xxx
More generally the pattern is:
org.apache.catalina.core.ContainerBase.[${engine}].[${host}].[${context}] Side note: ${context} is "/" for the root context.
Start the tomcat in the debugging mode:
Running ***catalina.bat debug*** and type "run" in the "jdb" prompt
Deploying Web Applications in Tomcat:
Create a new Context:
Create a directory under $CATALINA_HOME\webapps and move your application codes under this directory
A context will be created automatically based on name of the directory.
Create the directory structure for the new context:
Directory that your application resides is called the context root or document base directory
Create a directory called "WEB-INF" under the context root - keep your applications configuration files such as ***web.xml***.
"classes" under "WEB-INF". This is where you keep all the Java classes such as servlets.
"lib" under "WEB-INF". This is where you keep the jar files and native libraries.
"src" under "WEB-INF", for keeping the source files. - Optional
A Context is what Tomcat calls a web application.
In order to configure a Context within Tomcat a Context Descriptor is required.
-- It is simply an XML file that contains Tomcat related configuration for a Context, e.g naming resources or session manager configuration.
-- In earlier versions of Tomcat the content of a Context Descriptor configuration was often stored within Tomcat's primary configuration file server.xml but this is now discouraged (although it currently still works).
-- Context Descriptors not only help Tomcat to know how to configure Contexts but other tools such as the Tomcat Manager and TCD often use these Context Descriptors to perform their roles properly.
The locations for Context Descriptors are:
--1. $CATALINA_BASE/conf/[enginename]/[hostname]/[webappname].xml
--2. $CATALINA_BASE/webapps/[webappname]/META-INF/context.xml
-- Files in (1) are named [webappname].xml but files in (2) are named context.xml. If a Context Descriptor is not provided for a Context, Tomcat configures the Context using default values.
==> Running Tomcat as a Windows Service:
cd $CATALINA_HOME\bin
$CATALINA_HOME\bin> service install
Installing the service 'Tomcat6' ...
Using CATALINA_HOME: d:\tomcat-6.0.20
Using CATALINA_BASE: d:\tomcat-6.0.20
Using JAVA_HOME: d:\jdk1.6
Using JVM: d:\jdk1.6\jre\bin\server\jvm.dll
The service 'Tomcat6' has been installed.
$CATALINA_HOME\bin> tomcat6w
$CATALINA_HOME\bin> tomcat6w //MS//
net start tomcat6
net stop tomcat6
$CATALINA_HOME\bin> service remove
The service 'Tomcat6' has been removed
Created with Microsoft OneNote 2016.