【问题标题】:Jetty Servlet context not found未找到 Jetty Servlet 上下文
【发布时间】:2014-03-28 00:08:31
【问题描述】:

我正在使用 2 个处理程序(webapp 和 servlet)以及 2 个上下文“/”和“/d/*”运行 Jetty。

Webapp 加载正常,而我在尝试转到 servlet 时收到“404 Not found”。

这是我使用的代码:

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 {

}
}

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>

【问题讨论】:

    标签: java servlets jetty


    【解决方案1】:

    我认为这是 Jetty 中的一个错误。欢迎反馈。 在 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);
    

    这个example 对我调试这个问题很有帮助。

    【讨论】:

      猜你喜欢
      • 2016-12-24
      • 2015-02-16
      • 2021-09-04
      • 2012-11-23
      • 2014-10-29
      • 1970-01-01
      • 2020-12-26
      • 2012-10-12
      • 2014-05-20
      相关资源
      最近更新 更多