Jetty/Tutorial/Jetty and Maven HelloWorld
Contents
Introduction
Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information. It is an ideal tool to build a web application project and such projects can use the Jetty/Feature/Jetty Maven Plugin to run the web application in development mode.
Maven can be used both to build embedded jetty appliations and standards based webapplications.
Details
To understand the basic operations of building and running against jetty, first review:
Embedded Jetty with Maven
Maven uses convention over configuration, so it is best to use the project structure as recommended by maven. Archetypes can be used to quickly setup maven projects, but for this tutorial, we will setup the structure manually:
mkdir JettyMavenHelloWorld cd JettyMavenHelloWorld mkdir -p src/main/java/org/example
The HelloWorld class
Use an editor to create the file src/main/java/org/example/HelloWorld.java with the following contents:
package org.example; 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 { 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(); } }
The POM descriptor
The pom.xml file declares the project name and it's dependencies. Use and editor to create the file pom.xml with the following contents:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>hello-world</artifactId> <version>0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Jetty HelloWorld</name> <dependencies> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>7.0.1.v20091125</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.1</version> <executions> <execution><goals><goal>java</goal></goals></execution> </executions> <configuration> <mainClass>org.example.HelloWorld</mainClass> </configuration> </plugin> </plugins> </build> </project>
Building and Running embedded HelloWorld
The HelloWorld class can now be compiled and executed with the commands
mvn clean compile exec:java
You can point your browser to http://localhost:8080 to see the hello world page. You can observe what maven is doing for you behind the scenes by using the mvn dependency:tree</tr> command which reveals the transitive dependency resolved and downloaded as:
> mvn dependency:tree [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'dependency'. [INFO] ------------------------------------------------------------------------ [INFO] Building Jetty HelloWorld [INFO] task-segment: [dependency:tree] [INFO] ------------------------------------------------------------------------ [INFO] [dependency:tree {execution: default-cli}] [INFO] org.example:hello-world:jar:0.1-SNAPSHOT [INFO] \- org.eclipse.jetty:jetty-server:jar:7.0.1.v20091125:compile [INFO] +- javax.servlet:servlet-api:jar:2.5:compile [INFO] +- org.eclipse.jetty:jetty-continuation:jar:7.0.1.v20091125:compile [INFO] \- org.eclipse.jetty:jetty-http:jar:7.0.1.v20091125:compile [INFO] \- org.eclipse.jetty:jetty-io:jar:7.0.1.v20091125:compile [INFO] \- org.eclipse.jetty:jetty-util:jar:7.0.1.v20091125:compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4 seconds [INFO] Finished at: Tue Feb 16 16:19:08 EST 2010 [INFO] Final Memory: 11M/68M [INFO] ------------------------------------------------------------------------
Standard Web app with Jetty and Maven
The above example shows how a hello world example can be run as an embedded jetty handler. The following example shows how a standard webapp can be developed with maven and jetty. The maven structure first needs to be created:
mkdir JettyMavenHelloWarApp cd JettyMavenHelloWebApp mkdir -p src/main/java/org/example mkdir -p src/main/webapp/WEB-INF
Creating some static content
A web application can contain static content, so create the file <tt>src/main/webapp/index.html with the following content:
Invalid language.
You need to specify a language like this: <source lang="html4strict">...</source>
Supported languages for syntax highlighting:
4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, otj, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic
<h1>Hello World Webapp</h1> <a href="/hello">Hello Servlet</a>