Jetty:servlet 与处理程序

发布于 2024-10-02 19:31:43 字数 144 浏览 0 评论 0原文

我正在尝试理解码头。

请告诉我:

  1. 什么时候使用 Servlet 和 Handlers 更好?

  2. 我可以将连接器与 Servlet 一起使用来实现“每个请求线程模型”吗?

I am trying to understand Jetty.

Tell me please:

  1. When is it better to use Servlets and when Handlers?

  2. Can I use Connectors with Servlets for "thread-per-request model"?

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

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

发布评论

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

评论(2

霓裳挽歌倾城醉 2024-10-09 19:31:43

在 Jetty 中,处理程序处理通过连接器传入的请求。其中一个处理程序(特别是 ServletHandler)允许 Jetty(大部分)支持 servlet。 Servlet 是一个可移植的 Java EE 概念,因此如果您在 Jetty 中使用 servlet,则可以以更可移植的方式设计您的应用程序。另一方面,它们可能会带来一些开销,因此您可能希望直接实现一个处理程序来处理通过连接器传入的请求。

如果您在 Jetty 中使用 servlet,则可以依赖 servlet 安全模型、会话支持等。如果您的应用程序不需要这样做,那么您最好实现一个非常简单的处理程序。

In Jetty, Handlers handle requests that are coming through Connectors. One of the Handlers, specifically ServletHandler, allows Jetty to (mostly) support servlets. Servlet is a portable Java EE concept, so you can design your application in a more portable way if you use servlets in Jetty. On the other hand, they are likely to bring some overhead, so you might want to implement a Handler directly that would handle requests coming via Connectors.

If you are using servlets in Jetty, you can rely on the servlet security model, on the session support, etc. If this is unnecessary for your application, you might be better off implementing a very simple handler.

怀念你的温柔 2024-10-09 19:31:43

我在玩它时发现了一个有趣的观察。 在实际之后添加处理程序(有特殊的用例)

server.start()

我有一个基于码头的应用程序(dropwizard.io),在这里我计划使用 org.eclipse.jetty.servlet.ServletContextHandler.insertHandler(HandlerWrapper handler) ) 如果服务器已经启动,它只会抛出 illegalStateException: STARTED 。因为:

public void setHandler(Handler handler) {
        if (isStarted())
            throw new IllegalStateException(STARTED);
        //..

但是如果是 org.eclipse.jetty.servlet.ServletContextHandler.addServlet(ServletHolder servlet,String pathSpec) ,它会将您的 servlet 添加到现有的 servlet 集合和所有内容中会起作用的。

One interesting observation I found when played with it. I had a jetty-based application(dropwizard.io) and here I was planning to add handler after actual(there was special use-case for it)

server.start()

using org.eclipse.jetty.servlet.ServletContextHandler.insertHandler(HandlerWrapper handler) it just throws illegalStateException: STARTED if the server already started. Because of:

public void setHandler(Handler handler) {
        if (isStarted())
            throw new IllegalStateException(STARTED);
        //..

But in case of org.eclipse.jetty.servlet.ServletContextHandler.addServlet(ServletHolder servlet,String pathSpec) it will add your servlet to existing servlet collection and everything will work.

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