我正在尝试将 Jetty 8 (8.1.18.v20150929) 嵌入到 Java (jdk1.7.0_67) 应用程序中。我有以下代码:
public static final String HTTP_PATH = "/session";
public static final int HTTP_PORT = 9995;
// Open the HTTP server for listening to requests.
logger.info("Starting HTTP server, Port: " + HTTP_PORT + ", Path: "
+ "/session");
httpServer = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(HTTP_PORT);
connector.setHost("localhost");
httpServer.addConnector(connector);
TestHttpHandler handler = new TestHttpHandler(this);
ContextHandler ch = new ContextHandler();
ch.setContextPath(HTTP_PATH);
ch.setHandler(handler);
httpServer.setHandler(ch);
try {
httpServer.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我的处理程序作为测试非常基本:
public void handle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
logger.debug("Handling");
}
如果我运行应用程序,然后使用 CURL 向http://localhost:9995/session发送 GET 请求,那么它会返回 200 状态,但没有调试输出。
如果我访问http://localhost:9995/session2,我会收到 404 错误。
我在网上阅读了很多示例,但由于某种原因,我似乎无法让处理程序正常工作。难道我做错了什么?谢谢