Jetty 11未检测到Servlet [英] Jetty 11 Doesn't Detect Servlets
本文介绍了Jetty 11未检测到Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用Jetty部署本地服务器的示例项目here。
我使用mvn package exec:java
命令运行本地服务器,它工作得很好。它加载HTML文件以及来自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>io.happycoding</groupId>
<artifactId>app-engine-hello-world</artifactId>
<version>1</version>
<properties>
<!-- App Engine currently supports Java 11 -->
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty.version>9.4.35.v20201120</jetty.version>
<!-- Project-specific properties -->
<exec.mainClass>io.happycoding.ServerMain</exec.mainClass>
<googleCloudProjectId>YOUR_PROJECT_ID_HERE</googleCloudProjectId>
</properties>
<dependencies>
<!-- Java Servlets API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!-- Jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
<version>${jetty.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Copy static resources like html files into the output jar file. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-web-resources</id>
<phase>compile</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>
${project.build.directory}/classes/META-INF/resources
</outputDirectory>
<resources>
<resource><directory>./src/main/webapp</directory></resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- Package everything into a single executable jar file. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${exec.mainClass}</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- App Engine plugin for deploying to the live site. -->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<projectId>${googleCloudProjectId}</projectId>
<version>1</version>
</configuration>
</plugin>
</plugins>
</build>
</project>
ServerMain.java
package io.happycoding;
import java.net.URL;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
/**
* Starts up the server, including a DefaultServlet that handles static files,
* and any servlet classes annotated with the @WebServlet annotation.
*/
public class ServerMain {
public static void main(String[] args) throws Exception {
// Create a server that listens on port 8080.
Server server = new Server(8080);
WebAppContext webAppContext = new WebAppContext();
server.setHandler(webAppContext);
// Load static content from inside the jar file.
URL webAppDir =
ServerMain.class.getClassLoader().getResource("META-INF/resources");
webAppContext.setResourceBase(webAppDir.toURI().toString());
// Enable annotations so the server sees classes annotated with @WebServlet.
webAppContext.setConfigurations(new Configuration[]{
new AnnotationConfiguration(),
new WebInfConfiguration(),
});
// Look for annotations in the classes directory (dev server) and in the
// jar file (live server)
webAppContext.setAttribute(
"org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
".*/target/classes/|.*\.jar");
// Handle static resources, e.g. html files.
webAppContext.addServlet(DefaultServlet.class, "/");
// Start the server! 🚀
server.start();
System.out.println("Server started!");
// Keep the main thread alive while the server is running.
server.join();
}
}
HelloWorldServlet.java
package io.happycoding.servlets;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html;");
response.getWriter().println("<h1>Hello world!</h1>");
}
}
再说一次,这个运行得很好.只要我在使用Jetty 9。
但是,如果我更新pom.xml
文件以将jetty.version
设置为11.0.0
,那么我的servlet URL突然会得到404。我在控制台中没有看到任何错误。
要使Servlet在Jetty 11中再次工作,我需要对代码进行哪些更改?
(我之所以问,是因为我收到了很多学生项目的可靠更新,这些项目都停止工作了,我正试图用尽可能少的摩擦来解决它们。)
推荐答案
Jetty 11基于雅加达Servlet5.0,它是雅加达EE 9的一部分。
雅加达EE 9已将";Big Bang";(它们的名称,不是我的)更改为命名空间和打包,现在不再是javax.servlet.*
。jakarta.servlet.*
。
Jetty 11中确实没有查找javax.servlet.*
的内容。
一些快速历史记录.
- Oracle拥有Java EE。
- Oracle生产Java EE 7。
- Oracle决定不再创建/管理EE。
- Oracle将所有EE捐赠给Eclipse基金会。
- Oracle未授予Eclipse Foundation在此新EE现实中使用";java";或&javax";的权限。
- 由于法律原因,Eclipse Foundation将其重命名为";Jakarta EE";。
- Eclipse Foundation发布Jakarta EE 8&q;实质上只是出于法律原因重命名了Java EE 7(尚未更改软件包命名空间)
- 出于法律原因,Eclipse Foundation将所有打包从
javax.<spec>
重命名为jakarta.<spec>
。 - Eclipse Foundation发布雅加达EE 9&q;实质上只是雅加达EE 8&q;,但名称空间发生了变化(这就是上面提到的大爆炸&)
(请注意,我浏览了这些步骤之间发生的许多其他事情)
javax.servlet.*
已死亡,存活时间jakarta.servlet.*
。
Jetty维护以下版本(当前)
- Jetty 9.4.x-Servlet 3.1/Java EE 7(javax.servlet)
- Jetty 10.x-Servlet 4.0/Jakarta EE 8(javax.servlet)
- Jetty 11.x-Servlet 5.0/Jakarta EE 9(jakarta.servlet)
javax.servlet
和jakarta.servlet
在Jetty版本中共存。(我们已经尝试过了,Servlet规范的复杂性使其无法用于HTTPSession、RequestDispatcher、动态Servlet/过滤注册等)
我们所能期望的最好结果(已经有几个项目开始这样做了,都是阿尔法质量的ATM)是某种工具,它可以自动更新您的JAR和/或新打包的源代码,然后在基于雅加达的服务器上运行。
这篇关于Jetty 11未检测到Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文