首页 / 操作系统 / Linux / Jetty和Maven HelloWorld
Apache Maven是一个软件项目管理和理解工具。基于项目对象模型(POM)内容,Maven能够通过信息中心管理一个项目构建、报告和文档。它是一个理想的工具用来构建Web应用项目。这项目可以使用Jetty Maven插件在部署模式下运行Web应用。你能使用Maven来构建嵌入式Jetty应用程序和标准的基于Web应用。Maven权威指南_中文完整版清晰PDF http://www.linuxidc.com/Linux/2014-06/103690.htm为了理解使用Jetty构建和运行的基本操作,首先阅读:1) Jetty HelloWorld教程http://wiki.eclipse.org/Jetty/Tutorial/Jetty_HelloWorld2) 嵌入Jetty教程http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty使用Jetty作为嵌入式服务器 http://www.linuxidc.com/Linux/2013-07/86983.htmJetty 源码分析 http://www.linuxidc.com/Linux/2013-10/90986.htmJetty安装学习并展示 http://www.linuxidc.com/Linux/2014-05/101993.htmJetty在Eclipse中的安装 http://www.linuxidc.com/Linux/2013-10/90991.htmLinux(RedHat 5.8)下 安装Jetty 部署 使用 http://www.linuxidc.com/Linux/2014-10/108342.htm
使用Maven配置嵌入式Jetty
Maven使用约定优先于配置,因此最好使用Maven的项目结构,正如Maven推荐的。你能使用Archetypes快速设置Maven项目,但是对于本教程,我们将手动的设置结构:mkdir JettyMavenHelloWorldcd JettyMavenHelloWorldmkdir -p src/main/java/org/example创建HelloWorld类
使用编辑器创建一个文件src/main/java/org/example/HelloWorld.java,内容如下:package org.example;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;import javax.servlet.ServletException;import java.io.IOException;import org.eclipse.jetty.server.Server;import org.eclipse.jetty.server.Request;importorg.eclipse.jetty.server.handler.AbstractHandler; public class HelloWorld extendsAbstractHandler{ public void handle(String target, Request baseRequest, HttpServletRequestrequest, HttpServletResponseresponse) throws IOException, ServletException { response.setContentType("text/html;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); baseRequest.setHandled(true); response.getWriter().println("<h1>HelloWorld</h1>"); } public static void main(String[] args) throws Exception { Server server = new Server(8080); server.setHandler(new HelloWorld()); server.start(); server.join(); }}创建POM描述
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.0http://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>9.0.2.v20130417</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> 注:使用9.0.2.v20130417版本可以找到下面的类:importorg.eclipse.jetty.server.handler.AbstractHandler; 但是使用Jetty的最新版本9.2.3.v20140905无法导入该类。更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2014-11/109573p2.htm