BACK | INDEX | EXIT | NEXT |
The org.mortbay.http.HttpServer class provides a core HTTP server that listens on specified ports and accepts and handles requests.
The server is configured by method calls on the Java API . This code example creates a simple server listening on port 8080 and serving static resources (files) from the location ./docroot:
HttpServer server = new HttpServer(); SocketListener listener = new SocketListener(); listener.setPort(8080); server.addListener(listener); HttpContext context = new HttpContext(); context.setContextPath("/"); context.setResourceBase("./docroot/"); context.addHandler(new ResourceHandler()); server.addContext(context); server.start(); Code Example: Creating a trivial HTTP server |
The HttpServer provides a flexible mechanism for extending the capabilities of the server called "HttpHandlers". The server selects an appropriate HttpHandler to generate a response to an incoming request. The release includes handlers for static content, authentication and a Servlet Container.
Serlvets are the standard method for generating dynamic content, however the server can also be extended by writing custom HttpHandlers if servlets are insufficient or too heavyweight for your application.
More detailed information on the HttpServer class, including setting
up port listeners, contexts and handlers is to be found in the section
HTTP Server.
Introduction to the Web Application Server
The org.mortbay.jetty.Server class extends org.mortbay.http.HttpServer with XML configuration capabilities and a J2EE compliant Servlet Container.
The following code example demonstrates the creation of a server,
listening on port 8080 to deploy a web application located in the directory
./webapps/myapp:
Server server = new Server(); SocketListener listener = new SocketListener(); listener.setPort(8080); server.addListener(listener); server.addWebApplication("/","./webapps/myapp/"); server.start(); Code Example: Creating a Web Application Server |
As mentioned previously, the Jetty Server is able to be configured via XML as an alternative to cutting code. The same web application as coded above can be deployed by this XML configuration file:
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure 1.2//EN" "http://jetty.mortbay.org/configure_1_2.dtd"> <Configure class="org.mortbay.jetty.Server"> <Call name="addListener"> <Arg> <New class="org.mortbay.http.SocketListener"> <Set name"port">8080</Set> </New> </Arg> </Call> <Call name="addWebApplication"> <Arg>/</Arg> <Arg>./webapps/myapp/</Arg> </Call> </Configure>XML Example: Configuring a Web Application Server |
java -jar start.jar myserver.xmlTo read more about Jetty as a web application server, click here.
BACK | INDEX | EXIT | NEXT |