Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

How do I set up and run Tomcat on Macintosh OS X?

See TomcatOnMacOS

How do I set up and run Tomcat on Solaris 10?

See TomcatOnSolaris10

How do I set up multiple sites sharing the same war application/war file?

...

The following excellent article was written by Mladen Turk. He is a Developer and Consultant for JBoss Inc in Europe, where he is responsible for native integration. He is a long time commiter for Jakarta Tomcat Connectors, Apache Httpd and Apache Portable Runtime projects.

...

These steps are in no particular order ... 1.#0

  1. Read the Tomcat FAQ
  2. Read the Tomcat RELEASE NOTES - there is something about Linux in it
  3. First look at the stack traces. I hope a stack trace was produced before the failure aborted the JVM process. After you get a few stack traces, see if a pattern appears. Trace back to source code if needed.
  4. Patch (or unpatch!) the operating system as needed.
  5. Patch (or unpatch!) the JVM (Java Virtual Machine).
  6. Linux Problem? - read the RELEASE NOTES!
  7. Look at commercial vendor support for other servlet engines. Sometimes the problem is universal regardless of servlet engine and may be a JVM/OS/application code issue
  8. Search Google for web pages - maybe someone else had this problem. I'll bet they did.
  9. Search Google news groups
  10. If the JVM is from a commercial vendor, (eg: IBM, HP) check their release notes and news groups
  11. Using a database? Make sure JDBC type 4 drivers are used. Check their release notes.
  12. Tweak JVM memory parameters. Setting memory too high can be as bad as having memory too low. If your memory settings are set too high, Java 1.3 JVMs may freeze while waiting for the entire garbage collection to finish. Also if the JVM has too much memory, if may be starving other resources on the machine which are needed which may be causing unforeseen exceptions. In a nutshell, throwing more memory doesn't always solve the problem!
  13. Turn off the Java JIT compiler. See the Java Docs on how to do this.

...

Threads from the tomcat-user list

Using VeriSign:

...

For more information on each, check your system documentation.

How do I get direct access to a Tomcat Realm?

...

No Format
public class WebEditor
extends HttpServlet
{
 ...
	        public void doGet
	(
		        (
                HttpServletRequest poHTTPRequest,
		                HttpServletResponse poHTTPResponse
	)
	        )
        throws IOException, ServletException
	{
		poHTTPResponse.setContentType("text/html");

		ServletOutputStream out =       {
                poHTTPResponse.setContentType("text/html");

                ServletOutputStream out = poHTTPResponse.getOutputStream();

		                out.println("<html>");
		                out.println("<body>");
		                out.println("<head>");
		                out.println("<title>WebEditor Test $Revision: 1.6 $</title>");
		                out.println("</head>");
		                out.println("<body>");
		                out.println("<h3>WebEditor Test $Revision: 1.6 $</h3>");
		                out.println("<hr />");

		                // Backup the streams
		PrintStream   oStdOutBackup = System            PrintStream oStdOutBackup = System.out;
		                PrintStream oStdErrBackup = System.err;

		                // Redired STDOUT and STDERR to the ServletOuputStream
		                System.setOut(new PrintStream(out));
		                System.setErr(new PrintStream(out));


		try
		{
			// ... call compiler here that produces
			// tons of STDOUT/STDERR messages ...
		}
		catch(Exception e)
		{
			                try
                {
                        // ... call compiler here that produces
                        // tons of STDOUT/STDERR messages ...
                }
                catch(Exception e)
                {
                        out.println(e.toString());
		}

		                }

                // Restore original STDOUT and STDERR
		                System.setOut(oStdOutBackup);
		;
                System.setErr(oStdErrBackup);

		                out.println("<hr />");
		                out.println("</body>");
		                out.println("</html>");
	        }
}

A few caveats arise, as for instance while the System.out and System.err are redirected as per above, no logging of these is done to files. You will need more legwork to do to make the additional logging. It is important to backup and restore the original streams as the above example does. Also, notice the use of getOutputStream():
when this method is called, the getWriter() method can no longer be used in the same response object.

...

Is there a way to allow normal user(non-root) to start/stop the tomcat server. Tried assigning permission, did not work. Read thru some articles, stated that only root has permission to port below 1025. How can i allow a non-root user to do so ? thks in adv. (smile)

  • malathi ranjit singh----

...

One way is to put Apache httpd with mod_jk before your Tomcat servers, and use ports >=1024 in the Tomcat(s). Since we do it that way, it's the only way I know.

...

Create a file called foo.c with this content (replace "/path/startupscript" with the tomcat startup script):

#include <unistd.h>
#include <stdlib.h>

Wiki Markup
int main( int argc, char \*argv\[\] )
 \{
        if ( 

  • if ( setuid(

...

  • 0

...

  • )

...

  • !=

...

  • 0

...

  • )

...

  • perror(

...

  • "setuid()

...

  • error"

...

  • );

...

  • printf( "Starting ${APPLICATION}\n" );

...

  • execl( "/bin/sh", "sh", "/path/startupscript", 0 );

...

  • return 1;

}

Run the following as root (replacing tmp with whatever you want the startup script to be and replacing XXXXX with whatever group you want to be able to start and stop tomcat:

...

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

...

additional information can be found at:
http://tomcat.apache.org/tomcat-5.5-doc/ssi-howto.html

...

Basically, this works just as described in http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html: Within your application, you are using the standard JNDI and JMS API calls. In web.xml (the container independent application descriptor), you specify resource references (stub resources). And in context.xml (the container specific application descriptor), you are actually configuring the JMS connection.

...

  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\webapps\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.

  1. Add a line to your c:\Program Files\Apache Software Foundation\Tomcat 5.5\conf\tomcat-users.xml file so that you have a user who has admin role. For example, add this line just before the last line (containing </tomcat-users>) of the file:
    • <user username="admin" password="makesomethingup" roles="admin,manager"/>
  2. Restart Tomcat.
  3. Now when you visit _http://localhost:8080/admin_ you should see a page that asks for a user name and password. If you still see the "no longer loaded" error message in your browser, you must either force a full reload of the web page (in Firefox, hold down Shift key while clicking on the Reload button) or just restart your browser completely.

...

Ideally this should be incorporated into org.apache.catalina.util.ServerInfo by some committer. In addition to the shutdown command they should add commands like status (UP or DOWN) and uptime in the await method of org.apache.catalina.core.StandardServer

No Format

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import javax.xml.parsers.DocumentBuilderFactory;
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 ();
    }
  }
}

Where and how do I set memory related settings to improve Tomcat performance?

...

For other parameters , go to * http://wiki.apache.org/tomcat/FAQ/Memory

...

* http://wiki.apache.org/tomcat/OutOfMemory

and Google and Yahoo are your friends.

...

Congratulations. You have created and tested a first web application (traditionally called "mywebapp"), users can access it via the URL "http://myhost.company.com/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://myhost.company.com" ?

Wiki Markup
The pages and code of your "mywebapp" application currently reside in (CATALINA_BASE)/webapps/mywebapp/. In a standard Tomcat installation, you will notice that under the same directory (CATALINA_BASE)/webapps/, 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://myhost.company.com\[:port\]". The trick is to put your application in it's 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)/webapps/mywebapp/ directory, toward this (CATALINA_BASE)/webapps/ROOT directory. In other words, what was previously .../mywebapp/WEB-INF should now be .../ROOT/WEB-INF (and not .../ROOT/mywebapp/WEB-INF).

...

Restart Tomcat and you're done.
Call up "http://myhost.company.com/" and enjoy.

...

  • delete the ROOT directory
  • name your war file "ROOT.war" (capitals mandatory)
  • drop the ROOT.war file directly in the /webapps directory.
    Tomcat will automatically deploy it.

For more information about this topic in general, consult this page :
The Context Container

Addendum 2 : If for some reason you want another method..

If, for some reason, you do not want to deploy your application under the CATALINA_BASE/webapps/ROOT subdirectory, or you do not want to name your war-file "ROOT.war", then read on. But you should first read this :
The Context Container and make sure you understand the implications.

...

If you have Tomcat running in a console

*NIX: Press CRTL-\
Microsoft Windows: press CRTL-BREAK

...