| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>Chapter 19. Embedding</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><meta name="keywords" content="jetty, servlet-api, cometd, http, spdy, eclipse, maven, java, software"><link rel="home" href="index.html" title="Jetty : The Definitive Reference"><link rel="up" href="advanced.html" title="Part III. Advanced Topics Guide"><link rel="prev" href="writing-custom-handlers.html" title="Writing Custom Handlers"><link rel="next" href="embedding-jetty.html" title="Embedding Jetty"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><script type="text/javascript" src="js/shCore.js"></script><script type="text/javascript" src="js/shBrushJava.js"></script><script type="text/javascript" src="js/shBrushXml.js"></script><script type="text/javascript" src="js/shBrushBash.js"></script><script type="text/javascript" src="js/shBrushJScript.js"></script><script type="text/javascript" src="js/shBrushSql.js"></script><script type="text/javascript" src="js/shBrushProperties.js"></script><script type="text/javascript" src="js/shBrushPlain.js"></script><link type="text/css" rel="stylesheet" href="css/shCore.css"><link type="text/css" rel="stylesheet" href="css/shThemeEclipse.css"><link type="text/css" rel="stylesheet" href="css/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small"> |
| Version: 9.0.6.v20130930</span></td><td style="width: 50%"><script type="text/javascript"> (function() { |
| var cx = '016459005284625897022:obd4lsai2ds'; |
| var gcse = document.createElement('script'); |
| gcse.type = 'text/javascript'; |
| gcse.async = true; |
| gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + |
| '//www.google.com/cse/cse.js?cx=' + cx; |
| var s = document.getElementsByTagName('script')[0]; |
| s.parentNode.insertBefore(gcse, s); |
| })(); |
| </script><gcse:search></gcse:search></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 19. Embedding</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="writing-custom-handlers.html"><i class="icon-chevron-left"></i> Previous</a> </td><th width="60%" align="center">Part III. Advanced Topics Guide<br><a accesskey="p" href="index.html"><i class="icon-home"></i> Home</a></th><td width="20%" align="right"> <a accesskey="n" href="embedding-jetty.html">Next <i class="icon-chevron-right"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/support.jsp">Contact the core Jetty developers at |
| <span class="website">www.webtide.com</span></a></h5><p> |
| private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... |
| scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery |
| </p></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="advanced-embedding"></a>Chapter 19. Embedding</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="advanced-embedding.html#jetty-helloworld">Jetty Embedded HelloWorld</a></span></dt><dt><span class="section"><a href="embedding-jetty.html">Embedding Jetty</a></span></dt><dt><span class="section"><a href="embedded-examples.html">Embedded Examples</a></span></dt></dl></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jetty-helloworld"></a>Jetty Embedded HelloWorld</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="advanced-embedding.html#downloading-jars">Downloading the Jars</a></span></dt><dt><span class="section"><a href="advanced-embedding.html#writing-helloworld-example">Writing a HelloWorld Example</a></span></dt><dt><span class="section"><a href="advanced-embedding.html#compiling-helloworld-example">Compiling the HelloWord example</a></span></dt><dt><span class="section"><a href="advanced-embedding.html#running-handler-and-server">Running the Handler and Server</a></span></dt><dt><span class="section"><a href="advanced-embedding.html#next-steps">Next Steps</a></span></dt></dl></div><p>This section provides a tutorial that shows how you can quickly |
| develop embedded code against the Jetty API.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="downloading-jars"></a>Downloading the Jars</h3></div></div></div><p>Jetty is decomposed into many jars and dependencies to achieve a |
| minimal footprint by selecting the minimal set of jars. Typically it is |
| best to use something like Maven to manage jars, however this tutorial |
| uses an aggregate Jar that contains all of the Jetty classes in one Jar. |
| You can manually download the aggregate jetty-all Jar and the servlet api |
| Jar using <a class="link" href="http://www.gnu.org/software/wget/" target="_top">wget</a> or |
| similar command (for example, <a class="link" href="http://curl.haxx.se/" target="_top">curl |
| </a>) or a browser. Use wget as follows:</p><div class="screenexample"><pre class="screen">mkdir Demo |
| cd Demo |
| wget -O jetty-all.jar -U none http://repo1.maven.org/maven2/org/eclipse/jetty/aggregate/jetty-all/9.0.0-RC1/jetty-all-9.0.0-RC1.jar |
| wget -O servlet-api.jar -U none http://repo1.maven.org/maven2/org/eclipse/jetty/orbit/javax.servlet/3.0.0.v201112011016/javax.servlet-3.0.0.v201112011016.jar</pre></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="writing-helloworld-example"></a>Writing a HelloWorld Example</h3></div></div></div><p>The <a class="link" href="">Embedding Jetty</a> section |
| contains many examples of writing against the Jetty API. This tutorial |
| uses a simple HelloWorld handler with a main method to run the server. You |
| can either <a class="link" href="http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/plain/examples/embedded/src/main/java/HelloWorld.java" target="_top">download</a> |
| or create in an editor the file <code class="filename">HelloWorld.java</code> with |
| the following content:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false"> |
| <![CDATA[import javax.servlet.http.HttpServletRequest; |
| import javax.servlet.http.HttpServletResponse; |
| import javax.servlet.ServletException; |
| import java.io.IOException; |
| import org.eclipse.jetty.server.Server; |
| import org.eclipse.jetty.server.Request; |
| import org.eclipse.jetty.server.handler.AbstractHandler; |
| |
| public class HelloWorld extends AbstractHandler { |
| |
| @Override |
| public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { |
| response.setContentType("text/html;charset=utf-8"); |
| response.setStatus(HttpServletResponse.SC_OK); |
| baseRequest.setHandled(true); |
| response.getWriter().println("<h1>Hello World</h1>"); |
| } |
| |
| public static void main(String[] args) throws Exception { |
| Server server = new Server(8080); |
| server.setHandler(new HelloWorld()); |
| server.start(); |
| server.join(); |
| } |
| } |
| ]]> |
| </script></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="compiling-helloworld-example"></a>Compiling the HelloWord example</h3></div></div></div><p>The following command compiles the HelloWorld class:</p><div class="screenexample"><pre class="screen">javac -cp servlet-api.jar:jetty-all.jar HelloWorld.java</pre></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="running-handler-and-server"></a>Running the Handler and Server</h3></div></div></div><p>The following command runs the HelloWorld example:</p><div class="screenexample"><pre class="screen">java -cp .:servlet-api.jar:jetty-all.jar HelloWorld</pre></div><p>You can now point your browser at <a class="link" href="http://localhost:8080/" target="_top">http://localhost:8080</a> to see your |
| hello world page.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="next-steps"></a>Next Steps</h3></div></div></div><p>To learn more about Jetty, take these next steps:</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>Follow the examples in <a class="link" href="embedding-jetty.html" title="Embedding Jetty">Embedding |
| Jetty</a> to better understand the jetty APIs.</p></li><li class="listitem"><p>Explore the complete <a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/" target="_top">jetty |
| javadoc</a></p></li><li class="listitem"><p>Consider using<a class="link" href="maven-and-jetty.html" title="Chapter 21. Maven and Jetty"> Jetty and |
| Maven</a> to manage your Jars and dependencies.</p></li></ul></div></div></div></div><script type="text/javascript"> |
| SyntaxHighlighter.all() |
| </script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="writing-custom-handlers.html"><i class="icon-chevron-left"></i> Previous</a> </td><td width="20%" align="center"><a accesskey="u" href="advanced.html"><i class="icon-chevron-up"></i> Top</a></td><td width="40%" align="right"> <a accesskey="n" href="embedding-jetty.html">Next <i class="icon-chevron-right"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Writing Custom Handlers </td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="icon-home"></i> Home</a></td><td width="40%" align="right" valign="top"> Embedding Jetty</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout"> |
| See an error or something missing? |
| <span class="callout"><a href="http://github.com/jetty-project/jetty-documentation">Contribute to this documentation at |
| <span class="website"><i class="icon-github"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2013-10-30T13:20:36-05:00)</i></span></div></p><script xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" type="text/javascript"> |
| var _gaq = _gaq || []; |
| _gaq.push(['_setAccount', 'UA-1149868-7']); |
| _gaq.push(['_trackPageview']); |
| |
| (function() { |
| var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; |
| ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; |
| var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); |
| })(); |
| </script></body></html> |