Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未找到Jetty Servlet上下文_Java_Servlets_Jetty - Fatal编程技术网

Java 未找到Jetty Servlet上下文

Java 未找到Jetty Servlet上下文,java,servlets,jetty,Java,Servlets,Jetty,我运行的Jetty有两个处理程序,webapp和servlet,以及两个上下文“/”和“/d/*” Webapp加载良好,而我在尝试转到servlet时得到“404notfound” 以下是我使用的代码: public static void main(String[] args){ // The simple Jetty config here will serve static content from the webapp directory String webappD

我运行的Jetty有两个处理程序,webapp和servlet,以及两个上下文“/”和“/d/*”

Webapp加载良好,而我在尝试转到servlet时得到“404notfound”

以下是我使用的代码:

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代码:

@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 {

}
}
@SuppressWarnings(“串行”)
@WebServlet
公共类DownloaderServlet扩展了HttpServlet{
公共下载程序Servlet(){}
@凌驾
受保护的无效数据集(HttpServletRequest请求,
HttpServletResponse(响应)
抛出ServletException、IOException{
PrintWriter out=response.getWriter();
println(“我的Servlet”);
}
@凌驾
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
}
}
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>

index.html

我认为这是Jetty中的一个bug。欢迎反馈。 无论哪种方式,在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);
这有助于我调试这个问题