歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Jetty和Maven HelloWorld

Jetty和Maven HelloWorld

日期:2017/3/1 9:37:10   编辑:Linux編程

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_HelloWorld

2) 嵌入Jetty教程

http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty

使用Jetty作為嵌入式服務器 http://www.linuxidc.com/Linux/2013-07/86983.htm

Jetty 源碼分析 http://www.linuxidc.com/Linux/2013-10/90986.htm

Jetty安裝學習並展示 http://www.linuxidc.com/Linux/2014-05/101993.htm

Jetty在Eclipse中的安裝 http://www.linuxidc.com/Linux/2013-10/90991.htm

Linux(RedHat 5.8)下 安裝Jetty 部署 使用 http://www.linuxidc.com/Linux/2014-10/108342.htm

使用Maven配置嵌入式Jetty

Maven使用約定優先於配置,因此最好使用Maven的項目結構,正如Maven推薦的。你能使用Archetypes快速設置Maven項目,但是對於本教程,我們將手動的設置結構:

mkdir JettyMavenHelloWorld

cd JettyMavenHelloWorld

mkdir -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

Copyright © Linux教程網 All Rights Reserved