Maven中约定优于配置,所以最好使用Maven建议的项目结构。你可以使用Archetypes来快速创建Maven项目,但在本教程中,我们将手工建立项目结构:
mkdir JettyMavenHelloWorld cd JettyMavenHelloWorld mkdir -p src/main/java/org/example
使用编辑器创建文件src/main/java/org/example/HelloWorld.java,并填入下面的内容:
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(); } }
pom.xml文件声明了项目名称和项目使用的依赖。使用编辑器创建文件pom.xml,并填入下面的内容:
<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> <properties> <jettyVersion>7.2.0.v20101020</jettyVersion> </properties> <dependencies> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>${jettyVersion}</version> </dependency> </dependencies> <build> <plugins> <plugin> <!-- This plugin is needed for the servlet example --> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>${jettyVersion}</version> </plugin> <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>
现在,你可以通过下面的命令编译并运行HelloWorld class:
mvn clean compile exec:java
现在你可以打开浏览器输入http://localhost:8080来看看你的hello world页面。你可以通过mvn dependency:tree命令查看Maven在幕后都为你做了什么 ,它解决了传递性依赖的问题:
> 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] ------------------------------------------------------------------------
上面的例子展示了如何运行hello world示例做为一个内嵌的Jetty handler。下面的例子展示了使用Maven和Jetty如何开发一个标准的web应用程序。首先创建Maven项目结构:
mkdir JettyMavenHelloWarApp cd JettyMavenHelloWebApp mkdir -p src/main/java/org/example mkdir -p src/main/webapp/WEB-INF
一个web应用程序可以包含静态内容,所以创建文件src/main/webapp/index.html,并填入下面内容:
<h1>Hello World Webapp</h1> <a href="/hello">Hello Servlet</a>
使用编辑器创建文件src/main/java/org/example/HelloServlet.java,并填入下面内容:
package org.example; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().println("<h1>Hello Servlet</h1>"); response.getWriter().println("session=" + request.getSession(true).getId()); } }
这个servlet必须在部署描述符中声明,所以编辑文件src/main/webapp/WEB-INF/web.xml,并填入下面内容:
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>org.example.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/hello/*</url-pattern> </servlet-mapping> </web-app>
现在使用下面命令构建并运行web应用程序:
mvn jetty:run
你可以在http://localhost:8080/hello-world/hello看到静态和动态内容。url中的应用上下文("hello-world")来自pom.xml文件的artifact ID。
你可以通过下面命令创建一个web应用存档文件(Web Application Archive简称WAR):
mvn package
由此产生的war文件在target目录中,这个文件可以部署到任何标准的servlet服务器中或部署到jetty中。