从 Jetty 6 迁移到 Jetty 8

发布于 2025-01-05 16:31:29 字数 579 浏览 0 评论 0原文

我在简单的应用程序中使用 jetty6 作为嵌入式 servlet 容器。我决定将其更新到 Jetty 8。 在jetty 6中启动服务器非常简单:

Server server = new Server(8080);
Context context = new Context(server, "/", Context.SESSIONS);
context.addServlet(MyServlet.class, "/communication-service");
server.start();

但在Jetty8中不起作用。 不幸的是我找不到这个版本的任何简单的例子。无法实例化 Context 并出现错误,

an enclosing instance that contains
    org.eclipse.jetty.server.handler.ContextHandler.Context is required

因为它现在是一个内部类,而且也没有这样的构造函数。

大多数示例针对 6 号和 7 号码头。 您能否提供如何在 jetty 8 启动 servlet 的简单示例?

I use jetty6 in simple application as embedded servlet container. I decided to update it to Jetty 8.
In jetty 6 it was pretty simple to start the server:

Server server = new Server(8080);
Context context = new Context(server, "/", Context.SESSIONS);
context.addServlet(MyServlet.class, "/communication-service");
server.start();

but it doesn't work in Jetty8.
Unfortunately I can't find any simple example for this version. Can't instantiate Context with error

an enclosing instance that contains
    org.eclipse.jetty.server.handler.ContextHandler.Context is required

because now it is an inner class and also no such constructor.

Most examples are for jetty 6 and 7.
Could you please provide simple example how to start servlet at jetty 8?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼忆七猫命九 2025-01-12 16:31:29

这是与您的代码等效的 Jetty 8。它仍然像以前一样简单,但 API 略有变化。

如果这对您不起作用,那么您可能遇到了类路径问题 - Jetty 8 被分成许多独立的 jar 文件,您将需要其中许多文件。至少你需要:

  • jetty-continuation
  • jetty-http
  • jetty-io
  • jetty-security
  • jetty-server
  • jetty-servlet
  • jetty-util
  • servlet-api

如果你有这些 jar,那么这段代码应该可以正常工作:

package test;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;

public class Jetty8Server {
    public static class MyServlet extends HttpServlet {
        protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
            response.setContentType("text/plain");
            response.getWriter().write(getClass().getName() + " - OK");
        }
    }
    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        handler.setContextPath("/"); // technically not required, as "/" is the default
        handler.addServlet(MyServlet.class, "/communication-service");
        server.setHandler(handler);
        server.start();
    }
}

This is the Jetty 8 equivalent to your code. It's still just as simple as it was before, however the API has changed slightly.

If this isn't working for you, then you probably have a classpath issue - Jetty 8 is separated into a lot of independent jar files, and you will need a number of them. At the very least you need:

  • jetty-continuation
  • jetty-http
  • jetty-io
  • jetty-security
  • jetty-server
  • jetty-servlet
  • jetty-util
  • servlet-api

If you have those jars, then this code should work fine:

package test;

import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;

public class Jetty8Server {
    public static class MyServlet extends HttpServlet {
        protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
            response.setContentType("text/plain");
            response.getWriter().write(getClass().getName() + " - OK");
        }
    }
    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        handler.setContextPath("/"); // technically not required, as "/" is the default
        handler.addServlet(MyServlet.class, "/communication-service");
        server.setHandler(handler);
        server.start();
    }
}
-小熊_ 2025-01-12 16:31:29

Jetty 现在是 Eclipse 的一部分。文档此处适用于 Jetty 7,但声称它应该适用于 Jetty 8。有一个示例在页面末尾使用 servlet。

Jetty is nowadays part of Eclipse. The documentation here is for Jetty 7 but claims it should work for Jetty 8. There's an example of using servlets towards the end of the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文