简体   繁体   中英

Jetty embedded: JSP and Servlets together?

I have an application with embedded Jetty 6.1.26. Servlet 2.5. Below is my server configuration.

The problem is, that when I try to have JSPs and Servlets together, it does not work. I either have one or the other working, based on whether I have server.addHandler() or server.setHandler() in the code below.

By "does not work" I mean that Jetty returns 404, but otherwise it looks fine, even Jetty log shows the configuration went fine - see http://pastebin.com/PzbEx0qc (that was with addHandler(), JSP was not working).

The URLS requested are
http://localhost:17283/jars?mvnPath= ... and
http://localhost:17283/jsp/index.jsp .

Thanks, Ondra

Server server = new Server( PORT );
Context ctx = new Context( server, "/", Context.NO_SECURITY | Context.SESSIONS );


final String WEBAPP_RESOURCES_PATH = "org/jboss/qa/mavenhoe/web/jsp";
final String JSP_CONTEXT_PATH = "/jsp";

// For localhost:port/jsp/index.html and whatever else is in the directory...
final URL warUrl = this.getClass().getClassLoader().getResource(WEBAPP_RESOURCES_PATH);
final String warUrlString = warUrl.toExternalForm();
    WebAppContext webAppContext = new WebAppContext(warUrlString, JSP_CONTEXT_PATH);
webAppContext.setAttribute("jarIndex", jarIndex);
server.addHandler( webAppContext );


// .jar's download.
final ServletHolder mavenhoeSH = new ServletHolder(new JarFinderServlet(this.jarIndex));
ctx.addServlet( mavenhoeSH, "/jars" );


final ServletHolder shutdownSH = new ServletHolder(new JettyShutdownServlet( server ));
shutdownSH.checkServletType();
ctx.addServlet( shutdownSH, "/shutdown" );

Each path component should be handled by its own context and make sure you use ContextHandlerCollection for multiple contexts.

ContextHandlerCollection contexts = new ContextHandlerCollection();

contexts.setHandlers(new Handler[] { jspContext, servletContext });

server.setHandler(contexts);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM