简体   繁体   English

找不到Jetty Servlet上下文

[英]Jetty Servlet context not found

I am running Jetty with 2 handlers, webapp and servlet, and 2 contexts, "/" and "/d/*". 我正在使用2个处理程序(webapp和servlet)以及2个上下文“ /”和“ / d / *”运行Jetty。

Webapp loads fine, while I get "404 Not found" when trying to go to servlet. Webapp加载正常,而尝试转到servlet时显示“ 404 Not found”。

Here is the code I use: 这是我使用的代码:

public static void main(String[] args){

    // The simple Jetty config here will serve static content from the webapp directory
    String webappDirLocation = "src/main/webapp/";

    // The port that we should run on can be set into an environment variable
    // Look for that variable and default to 8080 if it isn't there.
    String webPort = System.getenv("PORT");
    if (webPort == null || webPort.isEmpty()) {
        webPort = "7777";
    }
    Server server = new Server(Integer.valueOf(webPort));
    HandlerCollection handlerCollection = new HandlerCollection();

    // Add the GUI part
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setDescriptor(webappDirLocation + "/WEB-INF/web.xml");
    webapp.setResourceBase(webappDirLocation);
    handlerCollection.addHandler(webapp);

    // Add the API part
    ServletContextHandler apiContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
    apiContext.setContextPath("/d");
    apiContext.addServlet(new ServletHolder(new DownloaderServlet()), "/*");
    handlerCollection.addHandler(apiContext);

    // Add both handlers to the server
    server.setHandler(handlerCollection);
    try {
        server.start();
        server.join();
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

Servlet code: Servlet代码:

@SuppressWarnings("serial")
@WebServlet
public class DownloaderServlet extends HttpServlet {

public DownloaderServlet(){}

@Override
protected void doGet(HttpServletRequest request, 
        HttpServletResponse response)
                throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("<html><body><h1>My Servlet</h1></body></html>");
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}
}

Web.xml: Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

I think this is a bug in Jetty. 我认为这是Jetty中的错误。 Feedback is welcome. 欢迎反馈。 Either way adding servlet handler before webapp handler solved the problem: 无论哪种方式,在webapp处理程序解决之前都可以添加servlet处理程序:

HandlerCollection handlerCollection = new HandlerCollection();
// Add the API part
ServletContextHandler apiContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
apiContext.setContextPath("/d");
apiContext.addServlet(new ServletHolder(new DownloaderServlet()), "/*");
handlerCollection.addHandler(apiContext);
// Add the GUI part
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setDescriptor(webappDirLocation + "/WEB-INF/web.xml");
webapp.setResourceBase(webappDirLocation);
handlerCollection.addHandler(webapp);
// Add both handlers to the server
server.setHandler(handlerCollection);

This example was helpful in my efforts to debug this issue. 示例对调试此问题很有帮助。

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

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