5

I'm looking at an application that uses Jetty, and it has a lot of different related objects:

service = new Server(Integer.valueOf(System.getenv("PORT")));
final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
MyAppServlet myAppServlet = new MyAppServlet();
ServletHolder myAppServletServletHolder = new ServletHolder(myAppServlet);
final String serviceName = 'abc';
servletContextHandler.addServlet(myAppServletServletHolder, ("/"+ serviceName));
service.setHandler(servletContextHandler);

So it seems that the hierarchy is:

Server
  ServletContextHandler
    ServletHolder
      Servlet

The meaning of a Server is obvious, and it seems that a Servlet is the script that implements the actual application.

But the meaning of ServletContextHandler is less clear. Can you give a simple explanation that doesn't assume much background in the Java ecosystem, only general programming experience? Is it for the purpose of sharing configuration variables across different applications (servlets)? What would be some useful applications of that?

Finally, I have zero clue why we need a ServletHolder instead of just giving the Servlet to the ServletContextHandler.

The following is a little related but I don't think very closely. It only has one class overlap with this question: What's the difference between a ServletHandler and a ServletContextHandler in Jetty?

Stephen
  • 8,508
  • 12
  • 56
  • 96

1 Answers1

4

Server - the server itself. (yep, should be obvious)

ServletContextHandler - the context, provides the scope as defined in javax.servlet.ServletContext

You can have [0..n] ServletContextHandler instances.
They must not be mapped to the same context path.

Responsible for:

  • The context path (What is this context mapped to?) -

Examples:

/ for root

Request to http://machine.com/foo will hit that ServletContext and handle whatever is mapped within it for the resource /foo

/app for app specific

Request to http://machine.com/app/bar will hit the ServletContext at /app and handle whatever is mapped within it for the resource /bar

  • Attributes that belong to that ServletContext
  • Base Resource location - where all of your content is located (see: https://stackoverflow.com/a/39019797/775715)
  • List of Servlets
  • List of Filters
  • List of Servlet spec Listeners
  • Holds your Servlet spec Session Configuration
  • Holds your Servlet spec Authentication Configuration
  • Welcome File Configuration
  • Context specific Request Dispatcher support
  • Error Handling configuration
  • Gzip Response Compression support
  • etc ...

ServletHolder - the configuration for a specific servlet

Each Servlet can be configured for:

  • Url-pattern it's interested in
  • Init-Parameter map
  • Is it initialized at start? if so, in which order?

Note: there is also a FilterHolder equivalent for Filters

Servlet - the low level endpoint resource to process your request and generate a response

This handles the raw request using standard Servlet behaviors.

Filter - a component before the Servlet that can participate in the request/response handling.

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Can you clarify which parts are defined by the Servlet specification and which are defined by Jetty? – Basil Bourque Apr 27 '18 at 17:27
  • That's a gray area. Nearly all decisions made are influenced in some way by the Servlet spec. If it matters, pay attention to the Servlet API classes/packages, that will give you the hints on what's what. With the exception of the Server and "Gzip Response Compression support", everything I listed above is part of the Servlet Spec either directly or indirectly. – Joakim Erdfelt Apr 27 '18 at 19:45
  • And just so you know, this is the tip of the Servlet iceberg, an introduction to the classes you specifically asked about. There's lots more to get familiar with. – Joakim Erdfelt Apr 27 '18 at 19:47