Hi,
I am trying to learn, how to use Tomcat 4.03 from the book "Apache Jakarta Tomcat" by
James Goodwill.
The book does not explain things in much detail.
Chapter 6 is about embedding Tomcat, it gives the following server.xml file and
EmbeddedTomcat.java file
It does not explain what directories I need to create?
Where do I put the files server.xml and EmbeddedTomcat.java?
It gives a number of jar files I need to copy to the application. Copy to where?
Please help.
<Server port="8005" shutdown="SHUTDOWN" debug="0">
<Service name="Tomcat-Standalone">
<Connector className="org.apache.catalina.connector.http.HttpConnector"
port="8080" minProcessors="5" maxProcessors="75"
enableLookups="true" redirectPort="8443"
acceptCount="10" debug="0" connectionTimeout="60000"/>
<Engine name="Standalone" defaultHost="localhost" debug="0">
<Host name="localhost" debug="0" appBase="webapps" unpackWARs="true">
<Context path="/examples" docBase="examples" debug="0"
reloadable="true">
</Context>
</Host>
</Engine>
</Service>
</Server>
package chapter6;
import java.net.URL;
import org.apache.catalina.Connector;
import org.apache.catalina.Context;
import org.apache.catalina.Deployer;
import org.apache.catalina.Engine;
import org.apache.catalina.Host;
import org.apache.catalina.logger.SystemOutLogger;
import org.apache.catalina.startup.Embedded;
import org.apache.catalina.Container;
public class EmbeddedTomcat {
private String path = null;
private Embedded embedded = null;
private Host host = null;
/**
* Default Constructor
*
*/
public EmbeddedTomcat() {
}
/**
* Basic Accessor setting the value of the context path
*
* @param path - the path
*/
public void setPath(String path) {
this.path = path;
}
/**
* Basic Accessor returning the value of the context path
*
* @return - the context path
*/
public String getPath() {
return path;
}
/**
* This method Starts the Tomcat server.
*/
public void startTomcat() throws Exception {
Engine engine = null;
// Set the home directory
System.setProperty("catalina.home", getPath());
// Create an embedded server
embedded = new Embedded();
embedded.setDebug(5);
embedded.setLogger(new SystemOutLogger());
// Create an engine
engine = embedded.createEngine();
engine.setDefaultHost("localhost");
// Create a default virtual host
host = embedded.createHost("localhost", getPath() + "/webapps");
engine.addChild(host);
// Create the ROOT context
Context context = embedded.createContext("",
getPath() + "/webapps/ROOT");
host.addChild(context);
// Create the examples context
Context examplesContext = embedded.createContext("/examples",
getPath() + "/webapps/examples");
host.addChild(examplesContext);
// Install the assembled container hierarchy
embedded.addEngine(engine);
// Assemble and install a default HTTP connector
Connector connector = embedded.createConnector(null, 8080, false);
embedded.addConnector(connector);
// Start the embedded server
embedded.start();
}
/**
* This method Stops the Tomcat server.
*/
public void stopTomcat() throws Exception {
// Stop the embedded server
embedded.stop();
}
/**
* Registers a WAR
*
* @param contextPath - the context path under which the
* application will be registered
* @param url - the URL of the WAR file to be registered.
*/
public void registerWAR(String contextPath, URL url) throws Exception {
if ( contextPath == null ) {
throw new Exception("Invalid Path : " + contextPath);
}
String displayPath = contextPath;
if( contextPath.equals("/") ) {
contextPath = "";
}
if ( url == null ) {
throw new Exception("Invalid WAR : " + url);
}
Deployer deployer = (Deployer)host;
Context context = deployer.findDeployedApp(contextPath);
if (context != null) {
throw new Exception("Context " + contextPath + " already Exists!");
}
deployer.install(contextPath, url);
}
/**
* removes a WAR
*
* @param contextPath - the context path to be removed
*/
public void unregisterWAR(String contextPath) throws Exception {
Context context = host.map(contextPath);
if ( context != null ) {
embedded.removeContext(context);
}
else {
throw new Exception("Context does not exist for named path : "
+ contextPath);
}
}
public static void main(String args[]) {
try {
EmbeddedTomcat tomcat = new EmbeddedTomcat();
tomcat.setPath("c:/EmbeddedTomcat");
tomcat.startTomcat();
Thread.sleep(100000);
tomcat.stopTomcat();
System.exit(0);
}
catch( Exception e ) {
e.printStackTrace();
}
}
}