tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dustin Callaway" <calla...@sourcestream.com>
Subject SimpleStartup.java fix...
Date Thu, 20 Jan 2000 04:37:50 GMT
The following SimpleStartup.java class starts the Tomcat HttpServer from within a Java application.
This allows for servlet debugging from within an IDE. Many thanks to Jim Rudnicki for the
sample code (which I just cleaned up and simplified). Place this application in the /tomcat
directory and run it from there.


import org.apache.tomcat.server.HttpServer;
import java.net.URL;

/**
 * SimpleStartup starts the Tomcat HttpServer in a Java
 * application to allow for debugging within an IDE.
 */
public class SimpleStartup
{
  public static void main(String[] args)
  {
    try
    {
      HttpServer server = new HttpServer(8080, null, null);

      URL url = resolveURL("webpages");
      server.getContextManager().setDocumentBase(url);

      url = resolveURL("examples");
      server.addContext("/examples", url);

      server.start(); //start the server
    }
    catch (Exception e)
    {
      System.out.println("Error: " + e);
    }
  }


  private static URL resolveURL(String s) throws Exception
  {
    // if the string contains :/, then we assume that it's a real URL and do nothing
    if (s.indexOf(":/") > -1)
    {
        return new URL(s);
    }

    // otherwise, we assume that we've got a file name and
    // need to construct a file url appropriatly.
    if (s.startsWith("/"))
    {
      return new URL("file", null, s);
    }
    else
    {
      String pwd = System.getProperty("user.dir");
      return new URL("file", null, pwd + "/" + s);
    }
  }
}


That's it. Register your servlet in the web.xml file, set a breakpoint in your servlet using
an IDE (JBuilder, Visual Cafe, etc.), and invoke the servlet from a browser (execution should
stop at your breakpoint). Thanks for the tip, Jim!

Dustin Callaway
(callaway@sourcestream.com)

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message