【问题标题】:How can I use Jetty to serve servlets without worrying about War file layouts?如何使用 Jetty 服务 servlet 而不必担心 War 文件布局?
【发布时间】:2013-10-17 15:11:27
【问题描述】:

我有一个预先存在的 Java 应用程序,我想使用 Vaadin 公开一个 Web UI。我正在使用 Maven 进行依赖管理。

Vaadin 文档建议使用 war 文件布局,但我不想将我的代码库重新排列为标准 War 格式。

有没有一种方法可以让我以编程方式启动 Jetty 服务器并让它为 servlet 提供服务,而不必担心战争目录结构?

展示如何从 main() 方法提供 servlet 的示例代码在这里会很有帮助。

或者,如果 Jetty 以外的其他东西在这里可以更好地工作,那就太好了。

【问题讨论】:

  • 你检查过 Spring Boot 吗?
  • Spring 在这方面似乎非常有分量。这只是现有应用程序的一小部分。

标签: java maven servlets jetty vaadin


【解决方案1】:

使用 jetty 在进程中设置简单的 HTTP 服务器非常简单:

final Server httpServer = new Server(18080);
httpServer.setHandler(new AbstractHandler() {

    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                response.getWriter().write("This is the HTTP response");            
    }
});
httpServer.start();

请注意,这是基于码头 8.1.8。上面的代码没有使用 Servlets,但是很容易将它连接到任何你想要的框架。

如果您真的需要一个 servlet(也许您已经准备好了),请使用 Jetty 的 ServletContextHandler 类而不是您自己的处理程序。

【讨论】:

    猜你喜欢
    • 2016-02-23
    • 2010-12-07
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多