[英]Jetty Servlet context not found
我正在使用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>
我認為這是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);
此示例對調試此問題很有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.