blob: e66e45be43a2f004b3c88da23a03ac030ee4da6b [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter&nbsp;21.&nbsp;Embedding</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><meta name="keywords" content="jetty, servlet, servlet-api, cometd, http, websocket, eclipse, maven, java, server, software"><link rel="home" href="index.html" title="Jetty"><link rel="up" href="jetty-dev-guide.html" title="Part&nbsp;IV.&nbsp;Jetty Development Guide"><link rel="prev" href="jetty-dev-guide.html" title="Part&nbsp;IV.&nbsp;Jetty Development Guide"><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"><link rel="stylesheet" href="css/highlighter/foundation.css"><script src="js/highlight.pack.js"></script><script>
hljs.initHighlightingOnLoad();
</script><link type="text/css" rel="stylesheet" href="css/font-awesome/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.3.28.v20191105</span></td><td style="width: 50%"></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&nbsp;21.&nbsp;Embedding</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jetty-dev-guide.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a>&nbsp;</td><th width="60%" align="center">Part&nbsp;IV.&nbsp;Jetty Development Guide<br><a accesskey="p" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></th><td width="20%" align="right">&nbsp;<a accesskey="n" href="embedding-jetty.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></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/">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 for sponsored feature development
</p></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="advanced-embedding"></a>Chapter&nbsp;21.&nbsp;Embedding</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><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 class="toc"><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 <a class="link" href="http://central.maven.org/maven2/org/eclipse/jetty/aggregate/jetty-all/9.3.28.v20191105/jetty-all-9.3.28.v20191105-uber.jar" target="_top"><code class="literal">jetty-all.jar</code></a> using <code class="literal">curl</code>) or a browser.</p><div class="blockquote"><blockquote class="blockquote"><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="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><i class="fa fa-asterisk" aria-hidden="true"></i> Note</h3><p>The central Maven repository has started to aggressively reject/deny access to the repository from the <code class="literal">wget</code> command line tool (due to abusive use of the tool by some groups).
The administrators of the central maven repository have stated that the recommended command line download tool is now curl.</p></div></blockquote></div><p>Use curl as follows:</p><div class="screenexample"><pre class="screen">&gt; mkdir Demo
&gt; cd Demo
&gt; curl -o jetty-all-uber.jar http://central.maven.org/maven2/org/eclipse/jetty/aggregate/jetty-all/9.3.28.v20191105/jetty-all-9.3.28.v20191105-uber.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 Embedding Jetty 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="/Users/joakim/Code/Jetty/jetty.project-9.3.x/jetty-documentation/../examples/embedded/src/main/java/org/eclipse/jetty/embedded/HelloWorld.java" target="_top">download</a> or create in an editor the file <code class="literal">HelloWorld.java</code> with the following content:</p><pre 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"><code>//
// ========================================================================
// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.embedded;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
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
{
// Declare response encoding and types
response.setContentType("text/html; charset=utf-8");
// Declare response status code
response.setStatus(HttpServletResponse.SC_OK);
// Write back response
response.getWriter().println("&lt;h1&gt;Hello World&lt;/h1&gt;");
// Inform jetty that this request has now been handled
baseRequest.setHandled(true);
}
public static void main( String[] args ) throws Exception
{
Server server = new Server(8080);
server.setHandler(new HelloWorld());
server.start();
server.join();
}
}</code></pre></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">&gt; mkdir classes
&gt; javac -d classes -cp jetty-all-uber.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">&gt; java -cp classes:jetty-all-uber.jar org.eclipse.jetty.embedded.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" style="list-style-type: disc; "><li class="listitem">Follow the examples in <a class="link" href="embedding-jetty.html" title="Embedding Jetty">Embedding Jetty</a> to better understand the jetty APIs.</li><li class="listitem">Explore the complete <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/" target="_top">Jetty javadoc</a></li><li class="listitem">Consider using <a class="link" href="maven-and-jetty.html" title="Chapter&nbsp;23.&nbsp;Maven and Jetty">Jetty and Maven</a> to manage your Jars and dependencies.</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="jetty-dev-guide.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="jetty-dev-guide.html"><i class="fa fa-chevron-up" aria-hidden="true"></i> Top</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="embedding-jetty.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Part&nbsp;IV.&nbsp;Jetty Development Guide&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></td><td width="40%" align="right" valign="top">&nbsp;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/eclipse/jetty.project">Contribute to this documentation at
<span class="website"><i class="fa fa-github" aria-hidden="true"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2019-11-05)</i></span></div></p></body></html>