【问题标题】:How to make the simplest HelloWorld servlet with maven without web.xml如何在没有 web.xml 的情况下使用 maven 制作最简单的 HelloWorld servlet
【发布时间】:2019-07-30 05:12:23
【问题描述】:

作为一个新手,我创建了一个 HelloWorld servlet:

package com.rx.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/helloworld")
public class HelloWorld extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    PrintWriter writer = response.getWriter();
    writer.write("<html><body>Hello World</body</html>");
  }
}

然后我的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>rx.helloworld</groupId>
<artifactId>simpleservlet</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>simpleservlet Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<war.name>simpleservlet</war.name>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<servlet.version>4.0.1</servlet.version>
</properties>
<dependencies>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>${servlet.version}</version>
  <scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${war.name}</finalName><!-- name of the bundled project when it is finally built -->
<plugins>
    <plugin>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-maven-plugin</artifactId>
      <version>9.3.9.v20160517</version>
      <configuration>
        <httpConnector>
          <!--host>localhost</host-->
        </httpConnector>
      </configuration>
    </plugin>
</plugins>
</build>
</project>

我从 servlet 规范文档中读到,其中告诉了

如果 Web 应用程序不包含任何内容,则不需要包含 web.xml Servlet、Filter 或 Listener 组件或正在使用注解来声明它们。在 换句话说,仅包含静态文件或 JSP 页面的应用程序不需要 存在的 web.xml。

我确实使用了@WebServlet,所以没有包括web.xml

完整的代码库是here

问题:但是当我尝试使用命令mvn jetty:run 使用jetty 运行它时,我根本找不到我的servlet。如何解决这个问题?

【问题讨论】:

  • Jetty 将在 version 10 支持 servlet 4.0。但是,web.xml 在 servlet 3.1.0 中也是可选的。您的 servlet 在http://localhost:8080/helloworld 没有响应?

标签: maven servlet-4


【解决方案1】:

最终自己想通了,说漏掉了maven-war-plugin里面的一个配置:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
</plugin>

failOnMissingWebXml应该配置false,不是默认值

【讨论】:

    猜你喜欢
    • 2011-06-29
    • 2018-07-18
    • 2020-03-27
    • 2019-04-23
    • 1970-01-01
    • 2015-06-26
    • 2012-05-11
    • 2013-07-19
    • 2019-11-28
    相关资源
    最近更新 更多