简体   繁体   English

从Jetty 6迁移到Jetty 8

[英]Migration from Jetty 6 to Jetty 8

I use jetty6 in simple application as embedded servlet container. 我在简单的应用程序中使用jetty6作为嵌入式servlet容器。 I decided to update it to Jetty 8. In jetty 6 it was pretty simple to start the server: 我决定将它更新到Jetty 8.在jetty 6中启动服务器非常简单:

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. 但它在Jetty8中不起作用。 Unfortunately I can't find any simple example for this version. 不幸的是,我找不到这个版本的任何简单示例。 Can't instantiate Context with error 无法实例化具有错误的Context

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? 大多数例子都是针对jetty 6和7.你能提供一个简单的例子来说明如何在jetty 8上启动servlet吗?

This is the Jetty 8 equivalent to your code. 这是Jetty 8等同于您的代码。 It's still just as simple as it was before, however the API has changed slightly. 它仍然像以前一样简单,但API略有改变。

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. 如果这不适合你,那么你可能有一个类路径问题 - Jetty 8被分成很多独立的jar文件,你需要它们中的一些。 At the very least you need: 至少你需要:

  • jetty-continuation 码头延续
  • jetty-http 码头-HTTP
  • jetty-io 码头-IO
  • jetty-security 码头安全
  • jetty-server 码头服务器
  • jetty-servlet 码头-的servlet
  • jetty-util 码头-UTIL
  • servlet-api 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();
    }
}

Jetty is nowadays part of Eclipse. Jetty现在是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. 这里的文档适用于Jetty 7,但声称它应该适用于Jetty 8.这是一个在页面末尾使用servlet的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM