提问者:小点点

使用Jetty maven插件部署战争


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

这是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!”)。

我已经花了好几天时间想弄清楚我做错了什么。你们是我最后的希望了!


共1个答案

匿名用户

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

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