You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Laurence Turpin <la...@virgin.net> on 2002/03/29 12:55:45 UTC
How do you do embedded Tomcat?
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();
}
}
}
Re: How do you do embedded Tomcat?
Posted by Laurence Turpin <la...@virgin.net>.
Hi Carlos,
Thankyou for your help. The problem I still have now is where do I put the
server.xml file
At the moment it is in the C:\EmbeddedTomcat along with the compiled
EmbeddedTomcat.java file EmbeddedTomcat.class
I have tried puting the server.xml file in place of the default server.xml
in the %TOMCAT_HOME%\conf directory.
When I run the program with :
java EmbeddedTomcat
I get the error message
Exception in thread "main" java.lang.NoClassDefFoundError: EmbeddedTomcat
Its like EmbeddedTomcat.class isn't there
I copied the catalina.jar file along with a number of other jars into
c:\2sdk1.4.0\jre\lib\ext
please help
----- Original Message -----
From: "Carlos A. Díaz" <ca...@expressmail.net>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Friday, March 29, 2002 2:01 PM
Subject: RE: How do you do embedded Tomcat?
> use this directory, the example use it (see public void static
> main)
> c:/EmbeddedTomcat
>
>
> > -----Mensaje original-----
> > De: Carlos A. Díaz [mailto:calidiaz@expressmail.net]
> > Enviado el: viernes, 29 de marzo de 2002 7:55
> > Para: Tomcat Users List
> > Asunto: RE: How do you do embedded Tomcat?
> >
> >
> > > <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>
> >
> > All that information already exist in
> > CATALINA_HOME/conf/server.xml, it can be ignored :)
> >
> > I think you can put your EmbeddedTomcat.java any where because
> > EmbeddedTomcat.java isn't a servlet.
> > just compile: (in my case, w2k)
> > c:\any_directory\javac -classpath "C:\Program
> > Files\Apache Tomcat
> > 4.0\server\lib\catalina.jar" EmbeddedTomcat.java
> >
> > then c:\any_directory\java EmbeddedTomcat
> >
> > Mucha suerte!!!
> > c.
> >
> > > 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();
> > > }
> > > }
> > > }
> > >
> > >
> >
> >
> > --
> > To unsubscribe:
> <ma...@jakarta.apache.org>
> For additional commands:
> <ma...@jakarta.apache.org>
> Troubles with the list:
> <ma...@jakarta.apache.org>
>
>
>
> --
> To unsubscribe: <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>
>
--
To unsubscribe: <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>
RE: How do you do embedded Tomcat?
Posted by "Carlos A. Díaz" <ca...@expressmail.net>.
use this directory, the example use it (see public void static
main)
c:/EmbeddedTomcat
> -----Mensaje original-----
> De: Carlos A. Díaz [mailto:calidiaz@expressmail.net]
> Enviado el: viernes, 29 de marzo de 2002 7:55
> Para: Tomcat Users List
> Asunto: RE: How do you do embedded Tomcat?
>
>
> > <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>
>
> All that information already exist in
> CATALINA_HOME/conf/server.xml, it can be ignored :)
>
> I think you can put your EmbeddedTomcat.java any where because
> EmbeddedTomcat.java isn't a servlet.
> just compile: (in my case, w2k)
> c:\any_directory\javac -classpath "C:\Program
> Files\Apache Tomcat
> 4.0\server\lib\catalina.jar" EmbeddedTomcat.java
>
> then c:\any_directory\java EmbeddedTomcat
>
> Mucha suerte!!!
> c.
>
> > 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();
> > }
> > }
> > }
> >
> >
>
>
> --
> To unsubscribe:
<ma...@jakarta.apache.org>
For additional commands:
<ma...@jakarta.apache.org>
Troubles with the list:
<ma...@jakarta.apache.org>
--
To unsubscribe: <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>
RE: How do you do embedded Tomcat?
Posted by "Carlos A. Díaz" <ca...@expressmail.net>.
> <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>
All that information already exist in
CATALINA_HOME/conf/server.xml, it can be ignored :)
I think you can put your EmbeddedTomcat.java any where because
EmbeddedTomcat.java isn't a servlet.
just compile: (in my case, w2k)
c:\any_directory\javac -classpath "C:\Program Files\Apache Tomcat
4.0\server\lib\catalina.jar" EmbeddedTomcat.java
then c:\any_directory\java EmbeddedTomcat
Mucha suerte!!!
c.
> 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();
> }
> }
> }
>
>
--
To unsubscribe: <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>