ix.util.http
Class HttpServer

java.lang.Object
  extended by ix.util.http.HttpServer

public class HttpServer
extends java.lang.Object

An HTTP 1.1 server that can serve static content (files) and also supports servlets.

This is intended as a convenient class that makes it easy to do the things we most want to do, rather than a class that maximises functionality and configurability.

Example:

   import javax.servlet.ServletException;
   import javax.servlet.http.HttpServlet;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;

   import java.io.IOException;

   import ix.util.http.HttpServer;

   public class Example {
       public static void main(String[] argv) {
           HttpServer server = new HttpServer(8080);
           server.setDocRoot("myfiles", "files");
           server.setLogPath("logs", "myapp");
           server.addServlet(new HelloServlet(), "/hello/*");
           server.start();
       }
   }

   public class HelloServlet extends HttpServlet {

       protected void doGet(HttpServletRequest req,
                            HttpServletResponse resp)
                 throws ServletException, IOException {
           resp.setContentType("text/plain");
           resp.setStatus(HttpServletResponse.SC_OK);
           resp.getWriter().println("Hello!");
       }

   }
 

See Also:
HttpObjectServlet, HttpStringServlet, HelloServlet

Constructor Summary
HttpServer(int port)
          Construct a server that listens on the specified port.
 
Method Summary
 void addServlet(javax.servlet.http.HttpServlet servlet, java.lang.String pathSpec)
          Add a servlet serving URLs that match the specified path.
 void addServlet(java.lang.String pathSpec, javax.servlet.http.HttpServlet servlet)
           
 java.lang.String getBaseUrl()
          Returns a base URL that will produce requests to this server.
 java.lang.String getDocPath()
          Returns the base path of the URLs that correspond to the files being served as static content.
 java.lang.String getDocRoot()
          Returns the name of the directory, if any, that contains the files being served as static content.
 int getServerPort()
          Returns the port this server is listening on.
 void join()
           
static void main(java.lang.String[] argv)
          Main program for testing.
protected  org.mortbay.jetty.servlet.ServletHolder makeStaticContentServlet()
           
 void parseDocRoot(java.lang.String spec, java.lang.String defaultFilePath)
          Interprets the 'spec' as docRoot[:docPath] and tells this server to serve files from the tree below docRoot and to use /docPath in the corresponding URLs.
 void setDocRoot(java.lang.String docRoot, java.lang.String docPath)
          Tells this server to serve files from the tree below the specified root directory and to use the specified path in the corresponding URLs.
 void setLogPath(java.lang.String dir, java.lang.String prefix)
          Tells this server to write an NCSA-format request log in the specified directory.
 void start()
          Starts the server running.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpServer

public HttpServer(int port)
Construct a server that listens on the specified port. If the port is 0, the operating system will be asked for a free port. After the server has been started, the port number can be obtained by calling getServerPort().

Method Detail

main

public static void main(java.lang.String[] argv)
Main program for testing. The effect is similar to running I-Serve, but there's no GUI or log file. However, the "http-root-directory" and "http-server-port" parameters can be used.


getServerPort

public int getServerPort()
Returns the port this server is listening on.


getBaseUrl

public java.lang.String getBaseUrl()
Returns a base URL that will produce requests to this server. It will have the form
    http://serverHost:serverPort/
 


getDocRoot

public java.lang.String getDocRoot()
Returns the name of the directory, if any, that contains the files being served as static content.

See Also:
setDocRoot(String docRoot, String docPath)

getDocPath

public java.lang.String getDocPath()
Returns the base path of the URLs that correspond to the files being served as static content.

See Also:
setDocRoot(String docRoot, String docPath)

setDocRoot

public void setDocRoot(java.lang.String docRoot,
                       java.lang.String docPath)
Tells this server to serve files from the tree below the specified root directory and to use the specified path in the corresponding URLs.

If this method has not been called before the server has been started, no files will be served. Otherwise, file docRoot/... will be available as URL http://serverHost:serverPort/docPath/....

See Also:
parseDocRoot(String docRoot, String defaultFilePath)

parseDocRoot

public void parseDocRoot(java.lang.String spec,
                         java.lang.String defaultFilePath)
Interprets the 'spec' as docRoot[:docPath] and tells this server to serve files from the tree below docRoot and to use /docPath in the corresponding URLs.

This method calls setDocRoot(String, String); see that method for more information.


setLogPath

public void setLogPath(java.lang.String dir,
                       java.lang.String prefix)
Tells this server to write an NCSA-format request log in the specified directory. The log file's name will have the form prefix_yyyy_mm_dd.log.

A log will be created only if this method is called before the server has been started.


makeStaticContentServlet

protected org.mortbay.jetty.servlet.ServletHolder makeStaticContentServlet()

addServlet

public void addServlet(javax.servlet.http.HttpServlet servlet,
                       java.lang.String pathSpec)
Add a servlet serving URLs that match the specified path. For example:
   server.addServlet(new HelloServlet("Hello!"), "/hello/*");
 
This method can be called while the server is running and should cause the servlet to become available for handling requests.


addServlet

public void addServlet(java.lang.String pathSpec,
                       javax.servlet.http.HttpServlet servlet)

start

public void start()
Starts the server running.


join

public void join()