Jetty :: ServletContextHandler#setContextPath(..) and how to associate Servlets with specific file extensions

StackOverflow https://stackoverflow.com/questions/19276717

Question

I have the following code in Jetty:

ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
contextHandler.setContextPath("/a");
ServletHolder holder = new ServletHolder(MyServlet.class);
contextHandler.addServlet(holder, "/b/*");

what is the difference between "/a" in the call to setContextPath and "/b/*" in the call to addServlet ? Are these paths concatenated to decide which requests MyServlet will serve?

Also, is it possible to associate a servlet with a specific file extension? I.e. by looking at the "endsWith" part, so to speak, of a URI and so dispatching URIs ending in, e.g. ".xsd" to a specific Servlet? Or is dispatching entirely based on "startsWith" logic?

Was it helpful?

Solution

It should conform basically to the servlet specification, where an URL consists of http://host:port/<context-root>/<servlet-path-spec>, where the Context root is defined by setContextPath and the Servlet path specification is defined by the second parameter of addServlet. So the servlet in your example would serve all URLs starting with http://host:port/a/b/.

The Servlet path specification does also allow to define mappings to file extensions, with the notation "*.xsd" in your example, see section 12.2 in the linked specification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top