10

I'm trying to get started with an embedded Jetty server. I just want to map requests to different servlets based on the request path.

What's the difference between creating a ServletHandler and adding servlets to it as opposed to creating a ServletContextHandler and adding servlets to that?

For example:

//how is this different...
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(MyServlet.class, "/path");

//from this?
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.addServlet(MyServlet.class, "/path");
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
elanh
  • 1,401
  • 3
  • 19
  • 31
  • Jetty has nothing specifically to do with it. These classes are defined in the Servlet Specification, which is where you should be looking. Should have already looked. – user207421 Jun 09 '15 at 23:50
  • 3
    @EJP - As far as I can see, ServletHandler and ServletContextHandler are not defined in the Servlet specification (http://docs.oracle.com/javaee/6/api/javax/servlet/package-summary.html). Perhaps you are referring to ServletContext. My question is specifically about Jetty and I see no reason to remove the relevant tags or edit the title. – elanh Jun 10 '15 at 10:44
  • @EJP the ServletHandler and ServletContextHandler are classes that belong to Jetty and are typically used for those working in Embedded Jetty – Joakim Erdfelt Jun 10 '15 at 11:24

2 Answers2

14

Most Servlet's require a javax.servlet.ServletContext object to operate properly.

Using a ServletContextHandler will create and manage the common ServletContext for all of the Servlets, Filters, Sessions, Security, etc within that ServletContextHandler. This includes proper initialization, load order, and destruction of the components affected by a ServletContext as well.

Also note, that ServletHandler is considered an internal class of ServletContextHandler and is not meant to be used "in the raw" like that with Jetty. While it is technically possible, it is discouraged for all but the most naive and simplistic implementations of a Servlet.

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Follow up question: in some examples I see the `ServletContextHandler` is created with a `Server` instance (e.g. `new ServletContextHandler(server, "/");`) and sometimes without. Furthermore, I also see the `ServletContextHandler` passed to the `Server` instance using `setHandler` (`server.setHandler(contextHandler);`). What's the point of all that? Why so many options with no detailed documentation? – elanh Jun 23 '15 at 11:01
  • End result is the same, either you provide the information on the constructor, or the fields, or have the parent object wire it up for you. Lots of object LifeCycle work is going on behind the scenes to make sure things are sane. – Joakim Erdfelt Jun 23 '15 at 12:52
2

For Example you can create VirtualHosts with ServletContextHandler and you can management context easily. That means different context handlers on different ports.

Server server = new Server();
ServerConnector pContext = new ServerConnector(server);
pContext.setPort(8080);
pContext.setName("Public");
ServerConnector localConn = new ServerConnector(server);
localConn.setPort(9090);
localConn.setName("Local");

ServletContextHandler publicContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
publicContext.setContextPath("/");
ServletHolder sh = new ServletHolder(new HttpServletDispatcher());  sh.setInitParameter("javax.ws.rs.Application", "ServiceListPublic");
publicContext.addServlet(sh, "/*");
publicContext.setVirtualHosts(new String[]{"@Public"});


ServletContextHandler localContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
localContext .setContextPath("/");
ServletHolder shl = new ServletHolder(new HttpServletDispatcher()); shl.setInitParameter("javax.ws.rs.Application", "ServiceListLocal");
localContext.addServlet(shl, "/*");
localContext.setVirtualHosts(new String[]{"@Local"}); //see localConn.SetName


HandlerCollection collection = new HandlerCollection();
collection.addHandler(publicContext);
collection.addHandler(localContext);
server.setHandler(collection);
server.addConnector(pContext);
server.addConnector(localContext);
ceeleP
  • 21
  • 1