【问题标题】:Deploy war with Jetty maven plugin使用 Jetty maven 插件部署战争
【发布时间】:2014-08-20 00:21:39
【问题描述】:

我正在尝试部署我的“Hello world!”使用“mvn jetty:run-war”到 Jetty 的 servlet。 这是我的项目结构:

这里是 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.ekropotin.test</groupId>
<artifactId>webapp_jetty_test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>webapp_jetty_test</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>8.1.12.v20130726</version>
            <configuration>
                <webAppSourceDirectory>${project.basedir}</webAppSourceDirectory>

                <scanIntervalSeconds>10</scanIntervalSeconds>

                <webApp>
                    <descriptor>WEB-INF/web.xml</descriptor>
                    <contextPath>/hello</contextPath>
                </webApp>

                <stopKey>foo</stopKey>
                <stopPort>9999</stopPort>

            </configuration>
        </plugin>
    </plugins>
</build>

App.java:

package com.ekropotin.test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;

public class App implements Servlet {

    private ServletConfig config;

    public void init(ServletConfig config) throws ServletException {
        this.config = config;
    }

    public void destroy() {
    }

    public ServletConfig getServletConfig() {
        return config;
    }

    public String getServletInfo() {
        return "A Simple Servltet";
    }

    public void service(ServletRequest request, ServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><head>");
        out.println("<title>A Sample Servlet!</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello, World!</h1>");
        out.println("</body></html>");
        out.close();
    }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">

    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.ekropotin.test.App</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

问题是当我在浏览器中运行“mvn jetty:run-war”并打开“0.0.0.0:8080/hello”时,我得到以下信息:

而不是 servlet 执行结果(“Hello, World!”)。

我花了几天时间试图找出我做错了什么。你们是我最后的希望!

【问题讨论】:

    标签: java xml maven jetty


    【解决方案1】:

    嗯,我终于找到了解决办法。

    实际上我的 servlet 可以通过这个地址获得:“0.0.0.0:8080/hello/hello”。

    【讨论】:

      猜你喜欢
      • 2011-10-20
      • 1970-01-01
      • 2014-01-19
      • 2013-12-27
      • 2012-12-28
      • 1970-01-01
      • 2014-04-23
      • 2015-09-30
      • 1970-01-01
      相关资源
      最近更新 更多