【问题标题】:Embedded jetty not loading servlet or jersey servlet container.I am able to load either of them but not both嵌入式码头不加载 servlet 或 jersey servlet 容器。我可以加载其中任何一个但不能同时加载
【发布时间】:2019-04-25 12:06:04
【问题描述】:

我正在运行带有球衣容器的嵌入式码头。我正在测试我是否可以前往 '/hello' 来读取我的 servlet,它可以请求调度和另一个 url '/entry/test' 来进入我的入口点类。其中只有一个基于最后添加的处理程序起作用。

我试图推理出类似于:embedded jetty server does not run both servlet and webapp

ServletContextHandler contextHandler = new ServletContextHandler(
    ServletContextHandler.SESSIONS);
contextHandler.setContextPath("/*");

Server jettyServer = new Server(8980);

WebAppContext webappcontext = new WebAppContext();
File warPath = new File( System.getProperty("user.dir"), "src/main/resources");
webappcontext.setWar(warPath.getAbsolutePath());

webappcontext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed",
    "false");
webappcontext.addServlet(new ServletHolder(new MyServlet()), "/hello");


ServletHolder jerseyServlet = contextHandler.addServlet(
     org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);

jerseyServlet.setInitParameter( "jersey.config.server.provider.classnames",
     EntryPoint.class.getCanonicalName());

HandlerList handlerCollection = new HandlerList();
//handlerCollection.setHandlers(new Handler[] { contextHandler,webappcontext });
handlerCollection.setHandlers(new Handler[] { webappcontext,contextHandler }); 


// Based on which handler was added last, i am able to view /hello or /entry/test

jettyServer.setHandler(handlerCollection);

try {
    jettyServer.start();
    jettyServer.join();
} finally {
    jettyServer.destroy();
}

/hello 显示我的 servlet 页面和 /entry/test 查看入口点类内容

【问题讨论】:

    标签: jersey embedded-jetty


    【解决方案1】:

    在您的情况下,无需混合使用 WebAppContextServletContextHandler

    使用其中一个。

    • 如果您有正式的 WAR 文件,请使用 WebAppContext,并带有(可选)WEB-INF/web.xml。如果您需要对类进行运行时字节码/注释扫描(例如@WebServlet),请使用。
    • 如果您手动声明 Servlet(就像您现在一样),请使用 ServletContextHandler

    另外,不要在 Jersey 配置中使用 url-pattern "/*",这几乎意味着 Jersey 会接受请求。如果可以的话,将 Jersey 分成它自己的 url 模式。一种常见的方法是让 "/api/*" 由 Jersey 处理,其余的由您想在 Servlet 容器中使用的任何内容处理。如果您采用这种方法,请确保您对 Servlet 规范如何与上下文路径、url 模式、路径规范、路径信息等一起使用有充分的了解,因为这将帮助您正确导航 Jersey 配置。

    您拥有的示例,但仅使用 ServletContextHandler

    Server jettyServer = new Server(8980);
    
    ServletContextHandler contextHandler = new ServletContextHandler(
            ServletContextHandler.SESSIONS);
    // Context-paths are not patterns, they are prefixes
    contextHandler.setContextPath("/");
    
    // Setup where static files are found
    File warPath = new File(System.getProperty("user.dir"), "src/main/resources");
    Resource warResource = new PathResource(warPath);
    contextHandler.setBaseResource(warResource);
    
    // Add your Jersey setup
    // BWEE! BWEE! do not use "/*" for your url-pattern, if you want to do things
    // in your servlet container that is not Jersey.
    String jerseyUrlPattern = "/api/*";
    ServletHolder jerseyServlet = contextHandler.addServlet(
            org.glassfish.jersey.servlet.ServletContainer.class, jerseyUrlPattern);
    jerseyServlet.setInitOrder(0);
    
    jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",
            EntryPoint.class.getCanonicalName());
    
    // Add your custom Servlets
    contextHandler.addServlet(MyServlet.class, "/hello");
    
    // Add DefaultServlet to serve static files (servlet spec requirement)
    // always added last in your ServletContext.
    // always named "default" for this specific servlet setup (per spec)
    ServletHolder defaultHolder = new ServletHolder("default", DefaultServlet.class);
    defaultHolder.setInitParameter("dirAllowed", "true");
    // always at url-pattern "/" for this specific servlet (per spec)
    contextHandler.addServlet(defaultHolder, "/");
    
    HandlerList handlers = new HandlerList();
    handlers.addHandler(contextHandler);
    handlers.addHandler(new DefaultHandler()); // used to show config errors
    
    jettyServer.setHandler(handlers);
    
    try
    {
        jettyServer.start();
        jettyServer.join();
    }
    finally
    {
        jettyServer.stop(); // use STOP not .destroy()
    }
    

    【讨论】:

    • 谢谢@Joakim。它按预期工作。现在唯一的问题是 requestdispatcher 没有按预期转发到 html 中。
    • protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("And we are in servlet"); RequestDispatcher requestDispatcher = request.getRequestDispatcher("index2.html"); System.out.println("request Dispatcher = " + requestDispatcher); requestDispatcher.forward(请求,响应);
    • index2.html 放在war路径所在的同一路径中: File warPath = new File("D:/Intellij/Projects/JettyJersey/src/main/resources");资源warResource = new PathResource(warPath); contextHandler.setBaseResource(warResource);
    • 请帮助我理解为什么在浏览器上显示:HTTP 错误:404 访问/index2.html 时出现问题。原因:未找到
    • 是的,经过大量测试和调试后发现,由于某种奇怪的原因,我的资源位置被设置在 D:\ 。但是我的代码指向 --> D:/Intellij/Projects/JettyJersey/src/main/resources,因此当我将 index.html 放在 D:/ 中时,我看到正在呈现的网页。如果我进一步推理,让我发布。代码片段供参考: File warPath = new File("D:/Intellij/Projects/JettyJersey/src/main/resources");资源warResource = new PathResource(warPath); contextHandler.setBaseResource(warResource);
    猜你喜欢
    • 2019-11-08
    • 2014-12-26
    • 1970-01-01
    • 2020-02-02
    • 2014-02-05
    • 1970-01-01
    • 2013-11-02
    • 2016-10-01
    • 1970-01-01
    相关资源
    最近更新 更多