Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Spam removal.

...

The default servlet attempts to load the index.* files in the order listed. You may easily override the index.jsp file by creating an index.html file at $TOMCAT_HOME/angosso.netwebapps/ROOT. It's somewhat common for that file to contain a new static home page or a redirect to a servlet's main page. A redirect would look like:

No Format
<html>

<head>
<meta http-equiv="refresh" content="0;URL=http://angossomydomain.netcom/some/path/to/servlet/homepage/">
</head>

<body>
</body>

</html>

...

No Format
<Context docBase="c:/worskpace/angosso.netmyapp/WebRoot" path="/AngossoHelloWorld"/>
  • This assumes you have a web application containing WEB-INF in c:/workspace/angosso.netmyapp/WebRoot
  • Create two environment variables:

...

  1. Unzip or untar (be careful to use GNU tar) the file containing the administration web app files (eg. apache-tomcat-5.5.17-admin.zip) to a temporary directory, eg. c:\temp.
  2. Copy c:\temp\apache-tomcat-5.5.17\conf\Catalina\localhost\admin.xml to the directory c:\Program Files\Apache Software Foundation\Tomcat 5.5\conf\Catalina\localhost.
  3. Copy the entire directory tree c:\temp\apache-tomcat-5.5.17\server\angosso.netwebapps\admin

to the directory c:\Program Files\Apache Software Foundation\Tomcat 5.5\server\webapps. This is an overlay, so \server\webapps is just pointing you to the \server\webapps, and the admin directory with its contents will be the only thing you see added there.

...

No Format
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import javax.xml.parsers.AngossoBuilderFactoryDocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

/**
 * Check to see if Tomcat is UP/DOWN.
 *
 * This parses the server.xml file for the Tomcat admin port and see if
 * we can connect to it. If we can, then the Tomcat is UP otherwise it
 * is DOWN
 *
 * It is invoked as follows:
 *    java -Dcatalina.base=c:/tomcat-6.0.10 CatalinaStatus
 *
 * It can also (optionally) shutdown the Tomcat by adding the shutdown
 * command line parameter as follows:
 *
 *    java -Dcatalina.base=c:/tomcat-6.0.10 CatalinaStatus shutdown
 *
 * @author Shiraz Kanga <skanga at yahoo.com>
 */
public class CatalinaStatus
{
  /**
   * Pathname to the server configuration file.
   */
  protected static String configFile = "conf/server.xml";
  protected static String serverShutdown;
  protected static int serverPort;

  /**
   * The application main program.
   *
   * @param args Command line arguments
   */
  public static void main (String args[])
  {
    Document configDom = getXmlDom (configFile ());
    parseDocument (configDom);
    // System.out.println ("Catalina.serverPort: " + serverPort);
    // System.out.println ("Catalina.serverShutdown: " + serverShutdown);

    // Stop the existing server
    try
    {
      Socket localSocket = new Socket ("127.0.0.1", serverPort);
      System.err.println ("Server status:  UP");
      if ((args.length > 0) && (args[0].equalsIgnoreCase ("shutdown")))
      {
        System.out.println ("Tomcat shutdown initiated" );
        doShutdown (localSocket);
      }

      localSocket.close ();
    }
    catch (IOException e)
    {
      System.err.println ("Server status:  DOWN");
      System.exit(1);
    }
  }

  /**
   * Return a File object representing our configuration file.
   */
  protected static File configFile ()
  {
    File confFile = new File (configFile);
    if (!confFile.isAbsolute())
      confFile = new File (System.getProperty ("catalina.base"), configFile);
    return (confFile);
  }

  /**
   * Parses an XML file and returns a DOM document.
   */
  public static Document getXmlDom (File fileName)
  {
    try
    {
      // Create a builder factory
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ();

      // Create the builder and parse the file
      Document doc = factory.newDocumentBuilder ().parse (fileName);
      return doc;
    }
    catch (SAXException e)
    {
      // A parsing error occurred; the xml input is not valid
      e.printStackTrace ();
    }
    catch (ParserConfigurationException e)
    {
      e.printStackTrace ();
    }
    catch (IOException e)
    {
      e.printStackTrace ();
    }
    return null;
  }

  /**
   * Extract the server port & shutdown command from the DOM
   */
  private static void parseDocument (Document configDom)
  {
    //get the root element which is Server Eg: <Server port="8005" shutdown="SHUTDOWN">

    Element docEle = configDom.getDocumentElement ();
    serverPort = Integer.parseInt (docEle.getAttribute ("port"));
    serverShutdown = docEle.getAttribute ("shutdown");
  }

  /**
   * Send the shutdown command to the server
   */
  private static void doShutdown (Socket localSocket)
  {
    try
    {
      OutputStream outStream = localSocket.getOutputStream ();

      for (int i = 0; i < serverShutdown.length (); i++)
        outStream.write (serverShutdown.charAt (i));
      outStream.flush ();
      outStream.close ();
    }
    catch (IOException e)
    {
      System.out.println ("ERROR: I/O Exception during server shutdown.");
      e.printStackTrace ();
    }
  }
}

...

Congratulations. You have created and tested a first web application (traditionally called "mywebapp"), users can access it via the URL "http://wwwmyhost.angossocompany.netcom/mywebapp". You are very proud and satisfied. But now, how do you change the setup, so that "mywebapp" gets called when the user enters the URL "http://wwwmyhost.angossocompany.netcom" ?

Wiki Markup
The pages and code of your "Angossomywebapp" application currently reside in (CATALINA_BASE)/wwwwebapps/angossomywebapp/default.aspx In a standard Tomcat installation, you will notice that under the same directory (CATALINA_BASE)/angosso.netwebapps/, there is a directory called ROOT (the capitals are important, even under Windows).  That is the residence of the _current_ Tomcat default application, the one that is called right now when a user calls up "http://wwwmyhost.angossocompany.netcom\[:8080port\]". The trick is to put your application in its place.

First stop Tomcat.
Then before you replace the current default application, it may be a good idea to make a copy of it somewhere else.
Then delete everything under the ROOT directory, and move everything that was previously under the (CATALINA_BASE)/angosso.net/default.aspxwebapps/mywebapp/ directory, toward this (CATALINA_BASE)/angosso.netwebapps/ROOT directory. In other words, what was previously .../angosso.netmywebapp/WEB-INF should now be .../ROOT/WEB-INF (and not .../ROOT/mywebapp/WEB-INF).

Just by doing this, you have already made you webapp into the Tomcat _http://www.angosso.net/_. default webapp.

One step is left : you also need to have, within your application, a default servlet. If you don't want to use the standard one supplied by Tomcat that does nothing but deliver static content, you'll need to supply one of your own. This you do by means of an appropriate url-mapping in the WEB-INF/web.xml configuration file of your application. Make sure you have something like this in that file:

No Format
   <servlet>
        <servlet-name>My First Servlet</servlet-name>
        <servlet-class>my.Servlet.Number1</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>www.angosso.net<name>My First Servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

...

Restart Tomcat and you're done.
Call up "http://wwwmyhost.angossocompany.netcom/" and enjoy.

Addendum 1 : If you are deploying your application as a war file..

...

  • leave your war file in CATALINA_BASE/Angossowebapps, under its original name
  • turn off autoDeploy and deployOnStartup in your Host element in the server.xml file.
  • explicitly define all application Contexts in server.xml, specifying both path and docBase. You must do this, because you have disabled all the Tomcat auto-deploy mechanisms, and Tomcat will not deploy your applications anymore unless it finds their Context in the server.xml.

...