我正在用球衣容器运行嵌入式码头。我正在测试是否可以前往“ / hello”以读取可以请求分派的servlet,另一个网址“ / entry / test”可以进入我的入口点类。它们中只有一个根据最后添加的处理程序来工作。
我试图推理出类似于:embedded jetty server does not run both servlet and webapp
ServletContextHandler contextHandler = new ServletContextHandler(
ServletContextHandler.SESSIONS);
contextHandler.setContextPath("/*");
Server jettyServer = new Server(8980);
WebAppContext webappcontext = new WebAppContext();
File warPath = new File( System.getProperty("user.dir"), "src/main/resources");
webappcontext.setWar(warPath.getAbsolutePath());
webappcontext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed",
"false");
webappcontext.addServlet(new ServletHolder(new MyServlet()), "/hello");
ServletHolder jerseyServlet = contextHandler.addServlet(
org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter( "jersey.config.server.provider.classnames",
EntryPoint.class.getCanonicalName());
HandlerList handlerCollection = new HandlerList();
//handlerCollection.setHandlers(new Handler[] { contextHandler,webappcontext });
handlerCollection.setHandlers(new Handler[] { webappcontext,contextHandler });
// Based on which handler was added last, i am able to view /hello or /entry/test
jettyServer.setHandler(handlerCollection);
try {
jettyServer.start();
jettyServer.join();
} finally {
jettyServer.destroy();
}
/ hello显示我的servlet页面,/ entry / test查看入口点类内容
答案 0 :(得分:0)
根据您的情况,无需混合使用WebAppContext
和ServletContextHandler
。
使用其中一个。
WebAppContext
),请使用WEB-INF/web.xml
。如果您需要对类进行运行时字节码/注释扫描(用于@WebServlet
之类的话)。ServletContextHandler
。此外,请勿在Jersey配置中使用url模式"/*"
,这几乎意味着Jersey将接受请求。如果可以的话,将Jersey划分为自己的网址格式。一种常见的方法是让"/api/*"
由Jersey处理,其余的由您想在Servlet容器中使用的任何东西处理。如果采用这种方法,请确保对Servlet规范如何与上下文路径,URL模式,路径规范,路径信息等一起使用有扎实的了解,因为这将帮助您正确地浏览Jersey配置。
您所拥有的示例,但仅使用ServletContextHandler
Server jettyServer = new Server(8980);
ServletContextHandler contextHandler = new ServletContextHandler(
ServletContextHandler.SESSIONS);
// Context-paths are not patterns, they are prefixes
contextHandler.setContextPath("/");
// Setup where static files are found
File warPath = new File(System.getProperty("user.dir"), "src/main/resources");
Resource warResource = new PathResource(warPath);
contextHandler.setBaseResource(warResource);
// Add your Jersey setup
// BWEE! BWEE! do not use "/*" for your url-pattern, if you want to do things
// in your servlet container that is not Jersey.
String jerseyUrlPattern = "/api/*";
ServletHolder jerseyServlet = contextHandler.addServlet(
org.glassfish.jersey.servlet.ServletContainer.class, jerseyUrlPattern);
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",
EntryPoint.class.getCanonicalName());
// Add your custom Servlets
contextHandler.addServlet(MyServlet.class, "/hello");
// Add DefaultServlet to serve static files (servlet spec requirement)
// always added last in your ServletContext.
// always named "default" for this specific servlet setup (per spec)
ServletHolder defaultHolder = new ServletHolder("default", DefaultServlet.class);
defaultHolder.setInitParameter("dirAllowed", "true");
// always at url-pattern "/" for this specific servlet (per spec)
contextHandler.addServlet(defaultHolder, "/");
HandlerList handlers = new HandlerList();
handlers.addHandler(contextHandler);
handlers.addHandler(new DefaultHandler()); // used to show config errors
jettyServer.setHandler(handlers);
try
{
jettyServer.start();
jettyServer.join();
}
finally
{
jettyServer.stop(); // use STOP not .destroy()
}