<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> </head> <body> <table cellpadding="0" cellspacing="8" border="0" width="20%"> <tbody> <tr> <td valign="middle" bgcolor="#ccccff"><small><a href="index.html"> BACK</a></small></td> <td valign="middle" bgcolor="#ccccff"><small><a href="index.html"> INDEX</a></small></td> <td valign="middle" bgcolor="#ccccff"><small><a href=".."> EXIT</a></small></td> <td valign="middle" bgcolor="#ccccff"><small><a href="HttpServer.html"> NEXT</a></small></td> </tr> </tbody> </table> <br> <h1><font color="#000000">The Basics</font></h1> <big><big><b><a name="Introduction"></a>Introduction</b></big></big><br> <br> <big><b>What is Jetty</b></big><br> <h1></h1> <div align="left"> Jetty is both an HTTP Server (like <a href="http://httpd.apache.org"> apache</a> ) and a Servlet Container (like <a href="http://jakarta.apache.org/tomcat/"> tomcat</a>) running as a single Java component. It can be used stand-alone to deploy your static content, servlets, JSPs and web applications. Alternatively, it can be embedded into your Java application to add HTTP and Servlet capabilities.<br> <br> <br> <big><b>Lightweight or Standards Based</b></big><br> <br> Jetty can be used at two different levels: as the core HttpServer and the complete Jetty Server. The former provides an HTTP server with the ability to serve static content and servlets, whilst the latter supports richer configuration capabilities and the deployment of standard web applications. <br> <br> The advantages of the <a href="#http"> HttpServer</a> is that it is lightweight and embeddable and is highly customizable.<br> <br> If you need standard Servlet Container support for the development and deployment of web applications, then use the enhanced Server (referred to usually as the <a href="#webapp">Jetty Server</a>). This has the added benefit of better support for configuration via XML. Skip down to an example of a web app deployment <a href="#webapp">here</a>.<br> <br> </div> <br> <big><font size="+1"><big><b><a name="http"></a>Introduction to the HttpServer</b></big></font></big> <p align="left"> The <a href="/javadoc/org/mortbay/http/HttpServer.html"> org.mortbay.http.HttpServer</a> class provides a core HTTP server that listens on specified ports and accepts and handles requests. </p> <p align="left">The server is configured by method calls on the <a href="/javadoc/org/mortbay/http/package-summary.html"> Java API</a> . This code example creates a simple server listening on port 8080 and serving static resources (files) from the location <font face="Courier New, Courier, monospace">./docroot</font>:</p> <p align="left"> </p> <div align="left"> </div> <p align="left"> </p> <table cellpadding="2" cellspacing="2" border="0" width="80%" align="center"> <tbody> <tr> <td valign="top"><tt>HttpServer server = new HttpServer();</tt><br> <tt> SocketListener listener = new SocketListener();</tt><br> <tt> listener.setPort(8080);</tt><br> <tt> server.addListener(listener);</tt><br><br> <tt> HttpContext context = new HttpContext();</tt><br> <tt> context.setContextPath("/");</tt><br> <tt> context.setResourceBase("./docroot/");</tt><br> <tt> context.addHandler(new ResourceHandler());</tt><br> <tt> server.addContext(context);</tt><br><br> <tt> server.start();</tt><br> <br> <b>Code Example: Creating a trivial HTTP server</b><br> </td> </tr> </tbody> </table> <tt><br> </tt><br> <br> The server is made stand-alone by placing the above code in the body of a <tt>main</tt> method (the HttpServer class itself has a <tt>main</tt> that can be used as an example). To use the server as a component within an application, include the above lines at an appropriate location within the application code. <p> 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. <P> 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. <P> More detailed information on the HttpServer class, including setting up port listeners, contexts and handlers is to be found in the section <a href="HttpServer.html"> HTTP Server</a>.<br> <br> <br> <br> <p> <font size="+1"><b><a name="webapp"></a><big>Introduction to the Web Application Server</big></b></font></p> <p> The <a href="/javadoc/org/mortbay/jetty/Server.html">org.mortbay.jetty.Server</a> class extends <a href="/javadoc/org/mortbay/http/HttpServer.html"> org.mortbay.http.HttpServer</a> with XML configuration capabilities and a J2EE compliant Servlet Container. </p> <p>The following code example demonstrates the creation of a server, listening on port 8080 to deploy a web application located in the directory .<tt>/webapps/myapp:</tt><br> </p> <tt><br> </tt> <table cellpadding="2" cellspacing="2" border="0" width="80%" align="center"> <tbody> <tr> <td valign="top"><tt>Server server = new Server();<br> SocketListener listener = new SocketListener();<br> listener.setPort(8080); server.addListener(listener);<br> server.addWebApplication("/","./webapps/myapp/");<br> server.start();<br> </tt><br> <b>Code Example: Creating a Web Application Server</b><br> </td> </tr> </tbody> </table> <br> <p>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: <table cellpadding="2" cellspacing="2" border="0" width="80%" align="center"> <tbody> <tr> <td valign="top"><small><PRE> <?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> </PRE></small> <b>XML Example: Configuring a Web Application Server</b><br> </td> </tr> </tbody> </table> To run Jetty with this XML file, execute this command: <blockquote><tt>java -jar start.jar myserver.xml<br> </tt></blockquote> To read more about Jetty as a web application server, click <a href="Server.html"> here</a>.<br> To skip to more information on the XML file format click <a href="XmlConfiguration.html"> here</a>.<br> <p> <table cellpadding="0" cellspacing="8" border="0" width="20%"> <tbody> <tr> <td valign="middle" bgcolor="#ccccff"><small><a href="index.html"> BACK</a></small></td> <td valign="middle" bgcolor="#ccccff"><small><a href="index.html"> INDEX</a></small></td> <td valign="middle" bgcolor="#ccccff"><small><a href=".."> EXIT</a></small></td> <td valign="middle" bgcolor="#ccccff"><small><a href="HttpServer.html"> NEXT</a></small></td> </tr> </tbody> </table> <p> </p> </body> </html>