作为绿色手,我创建了一个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。如何解决这个问题?
答案 0 :(得分:0)
最终我自己弄清楚了,说它错过了maven-war-plugin中的配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
failOnMissingWebXml
应该配置为false
,这不是默认值