com.mortbay.HTTP
Class HttpServer

java.lang.Object
  |
  +--com.mortbay.HTTP.HttpServer

public class HttpServer
extends java.lang.Object
implements javax.servlet.ServletContext

HTTP Server. This is the core class for the Jetty HTTP server. The server takes it's configuration from an instance of a class derived from HttpConfiguration. It then creates a HttpListener for each host and port described in the configuration. Requests received on those ports are converted into HttpRequests and a HttpResponse is instantiated. The request path and server configuration are used to select a handler stack (actually an array of HttpHandler instances). The request and response are passed to each handler in the stack until the request is handled (indicated by the response header fields having been written). HttpHandlers may choose to ignore, modify or completely handle a request given to them. They also may modify the response without completely handling the request. A typical stack of HttpHandlers would include:

There are other handlers for things like response filtering, request forwarding, session management defined in the com.mortbay.HTTP.Handler package. These may be configured in or the user may develop their own handlers. However, servlets should be used for user handling unless the user requires the additional functionality of HttpRequest (typically for better control of input/output of modification of the request). Exceptions raised by a Handler are handled themselves by a similar stack of ExceptionHandlers selected by request path and configuration. The configuration of the server is done via an abstract class to decouple the server from any particular configuration technology and to allow lightweight installations.

Notes

The server understands HTTP/1.1 and HTTP/1.0.

Usage

 java com.mortbay.HTTP.HttpServer [ HttpConfigurationClass ]
 

Version:
$Id: HttpServer.java,v 2.27 2000/06/02 23:41:05 gregw Exp $
Author:
Greg Wilkins

Constructor Summary
HttpServer()
          Construct, must be configured later
HttpServer(HttpConfiguration config)
          Construct and configure
 
Method Summary
 void close()
          Deprecated. Use stop
 HttpConfiguration configuration()
          Get the Configuration
 void configure(HttpConfiguration config)
          Configure and start the server Defined server properties are:
 java.lang.Object getAttribute(java.lang.String name)
          Returns an attribute of the server given the specified key name.
 java.util.Enumeration getAttributeNames()
          .
 javax.servlet.ServletContext getContext(java.lang.String url)
          Returns a ServletContext object that corresponds to a specified URL on the server.
 int getMajorVersion()
          Returns the major version of the Java Servlet API that this Web server supports.
 java.lang.String getMimeType(java.lang.String file)
          Returns the mime type of the specified file, or null if not known.
 int getMinorVersion()
          Returns the minor version of the Servlet API that this Web server supports.
 java.lang.String getRealPath(java.lang.String path)
          Applies alias rules to the specified virtual path and returns the corresponding real path.
 javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String path)
          Get a RequestDispatcher.
 java.net.URL getResource(java.lang.String path)
          Get a resource.
 java.io.InputStream getResourceAsStream(java.lang.String path)
          Get a resource as a Stream.
 java.lang.String getServerInfo()
          Returns the name and version of the Web server under which the servlet is running.
 javax.servlet.Servlet getServlet(java.lang.String name)
          Deprecated.  
 java.util.Enumeration getServletNames()
          Deprecated.  
 java.util.Enumeration getServlets()
          Deprecated. Use getServletNames & getServlet
 void join()
          join the HttpServer and all its listeners.
 void log(java.lang.Exception e, java.lang.String message)
          Deprecated.  
 void log(java.lang.String message)
          Writes a message to the servlet log file.
 void log(java.lang.String message, java.lang.Throwable th)
          Writes a message to the servlet log file.
static void main(java.lang.String[] args)
          Main
 void removeAttribute(java.lang.String name)
          .
 void setAttribute(java.lang.String name, java.lang.Object value)
          .
 void stop()
          Stop the HttpServer.
static void stopAll()
          Stop all instances of HttpServer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpServer

public HttpServer()
Construct, must be configured later

HttpServer

public HttpServer(HttpConfiguration config)
           throws java.lang.Exception
Construct and configure
Method Detail

configure

public void configure(HttpConfiguration config)
               throws java.lang.Exception
Configure and start the server Defined server properties are:
 SessionMaxInactiveInterval - Max idle time Ms before session death
 MinListenerThreads         - Min listener threads per listener
 MaxListenerThreads         - Max listener threads per listener
 MaxListenerThreadIdleMs    - Max idle time Ms before listen thread
                              death
 HTTP1_0_KeepAlive          - Boolean for using Keep-Alive with HTTP/1.0
 MimeMap		          - Property file of MIME mappings
 

configuration

public HttpConfiguration configuration()
Get the Configuration

close

public void close()
Deprecated. Use stop

Close the HttpServer and all its listeners.

stop

public void stop()
Stop the HttpServer. Configuration is cleared and the server needs to be reconfigured to be restarted. This needs to be improved.

stopAll

public static void stopAll()
Stop all instances of HttpServer.

join

public void join()
          throws java.lang.InterruptedException
join the HttpServer and all its listeners.

getServlet

public javax.servlet.Servlet getServlet(java.lang.String name)
Deprecated.  

Returns the servlet for the specified name.
Specified by:
getServlet in interface javax.servlet.ServletContext
Parameters:
name - the name of the servlet
Returns:
the Servlet, or null if not found
Throws:
javax.servlet.ServletException - if the servlet could not be initialized

getServlets

public java.util.Enumeration getServlets()
Deprecated. Use getServletNames & getServlet

Enumerates the servlets in this context (server). Only servlets that are accessible will be returned. The enumeration always includes the servlet itself.
Specified by:
getServlets in interface javax.servlet.ServletContext

getServletNames

public java.util.Enumeration getServletNames()
Deprecated.  

Specified by:
getServletNames in interface javax.servlet.ServletContext

getContext

public javax.servlet.ServletContext getContext(java.lang.String url)
Description copied from interface: javax.servlet.ServletContext
Returns a ServletContext object that corresponds to a specified URL on the server.

This method allows servlets to gain access to the resources located at a specified URL and obtain RequestDispatcher objects from it.

In security conscious environments, the servlet engine can return null for a given URL.

Specified by:
getContext in interface javax.servlet.ServletContext
Tags copied from interface: javax.servlet.ServletContext
Parameters:
uripath - a String specifying the URL for which you are requesting a ServletContext object
Returns:
the ServletContext object that corresponds to the named URL
See Also:
RequestDispatcher

getMajorVersion

public int getMajorVersion()
Description copied from interface: javax.servlet.ServletContext
Returns the major version of the Java Servlet API that this Web server supports. All implementations that comply with Version 2.1 must have this method return the integer 2.
Specified by:
getMajorVersion in interface javax.servlet.ServletContext
Tags copied from interface: javax.servlet.ServletContext
Returns:
2

getMinorVersion

public int getMinorVersion()
Description copied from interface: javax.servlet.ServletContext
Returns the minor version of the Servlet API that this Web server supports. All implementations that comply with Version 2.1 must have this method return the integer 1.
Specified by:
getMinorVersion in interface javax.servlet.ServletContext
Tags copied from interface: javax.servlet.ServletContext
Returns:
1

getResource

public java.net.URL getResource(java.lang.String path)
                         throws java.net.MalformedURLException
Get a resource. This is implemented with a standard URL instance that will make a socket connection back to the server. This is less than efficient and a better implementation is provided for getResourceAsStream.
Specified by:
getResource in interface javax.servlet.ServletContext
Parameters:
path - URL path of resource
Returns:
null
Throws:
java.net.MalformedURLException -  

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String path)
Get a resource as a Stream. Creates a simulated request to the local server and returns the content of the simulated response.
Specified by:
getResourceAsStream in interface javax.servlet.ServletContext
Parameters:
path - URL path of resource
Returns:
InputStream
See Also:
getResource(String path)

getRequestDispatcher

public javax.servlet.RequestDispatcher getRequestDispatcher(java.lang.String path)
Get a RequestDispatcher.
Specified by:
getRequestDispatcher in interface javax.servlet.ServletContext
Parameters:
path - URL path of resource
Returns:
null

log

public void log(java.lang.String message)
Writes a message to the servlet log file.
Specified by:
log in interface javax.servlet.ServletContext
Parameters:
message - the message to be written

log

public void log(java.lang.String message,
                java.lang.Throwable th)
Writes a message to the servlet log file.
Specified by:
log in interface javax.servlet.ServletContext
Parameters:
message - the message to be written
th - Throwable

log

public void log(java.lang.Exception e,
                java.lang.String message)
Deprecated.  

Writes an exception & message to the servlet log file.
Specified by:
log in interface javax.servlet.ServletContext
Parameters:
message - the message to be written

getRealPath

public java.lang.String getRealPath(java.lang.String path)
Applies alias rules to the specified virtual path and returns the corresponding real path. Returns null if the translation could not be performed.
Specified by:
getRealPath in interface javax.servlet.ServletContext
Parameters:
path - the real path to be translated

getMimeType

public java.lang.String getMimeType(java.lang.String file)
Returns the mime type of the specified file, or null if not known.
Specified by:
getMimeType in interface javax.servlet.ServletContext
Parameters:
file - file name whose mime type is required

getServerInfo

public java.lang.String getServerInfo()
Returns the name and version of the Web server under which the servlet is running. Same as the CGI variable SERVER_SOFTWARE.
Specified by:
getServerInfo in interface javax.servlet.ServletContext
Tags copied from interface: javax.servlet.ServletContext
Returns:
a String containing at least the servlet engine name and version number

getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Returns an attribute of the server given the specified key name. This allows access to additional information about the server not already provided by the other methods in this interface. Attribute names should follow the same convention as package names, and those beginning with 'com.sun.*' are reserved for use by Sun Microsystems. This implementation maps the attribute requests to properties of HttpConfiguration. The Jetty configuration does not adhere to the naming conventions described above.
Specified by:
getAttribute in interface javax.servlet.ServletContext
Parameters:
name - the attribute key name
Returns:
the value of the attribute, or null if not defined

getAttributeNames

public java.util.Enumeration getAttributeNames()
. This implementation maps the attribute requests to properties of HttpConfiguration. The Jetty configuration does not adhere to the naming conventions described above.
Specified by:
getAttributeNames in interface javax.servlet.ServletContext
Tags copied from interface: javax.servlet.ServletContext
Returns:
an Enumeration of attribute names
See Also:
ServletContext.getAttribute(java.lang.String)

setAttribute

public void setAttribute(java.lang.String name,
                         java.lang.Object value)
. This implementation maps the attribute requests to properties of HttpConfiguration. The Jetty configuration does not adhere to the naming conventions described above.
Specified by:
setAttribute in interface javax.servlet.ServletContext
Tags copied from interface: javax.servlet.ServletContext
Parameters:
name - a String specifying the name of the attribute
object - an Object representing the attribute to be given the name

removeAttribute

public void removeAttribute(java.lang.String name)
. This implementation maps the attribute requests to properties of HttpConfiguration. The Jetty configuration does not adhere to the naming conventions described above.
Specified by:
removeAttribute in interface javax.servlet.ServletContext
Tags copied from interface: javax.servlet.ServletContext
Parameters:
name - a String specifying the name of the attribute to be removed

main

public static void main(java.lang.String[] args)
Main