Jetty框架中Server、ServletContextHandler等组件的区别与关联技术问询
Hey there! Let's break down these Jetty components clearly, using your code snippet as a reference to make things concrete.
Let’s start with each component’s core role, then connect the dots between how they work together.
1. Server - The Core Jetty Container
Think of the Server as the "main server instance" of your Jetty app. It’s the top-level component that listens for incoming network connections on a specific port (in your code, that’s the PORT environment variable). It manages the entire lifecycle of your Jetty application—starting, stopping, handling raw connections, and routing requests to the right parts of your app. Without a Server, none of your Servlet logic would be accessible over the network.
In your code:
service = new Server(Integer.valueOf(System.getenv("PORT")));
This creates the server and tells it which port to listen for incoming traffic on.
2. Servlet - The Request-Handling Workhorse
A Servlet is where your actual business logic lives. It’s a Java EE standard component designed specifically to process HTTP requests and generate responses. Your custom MyAppServlet would override methods like doGet() or doPost() to handle GET/POST requests, interact with databases, return JSON/HTML, or whatever your app needs to do when a user sends a request.
In your code:
MyAppServlet myAppServlet = new MyAppServlet();
This instantiates your custom Servlet that does the real, request-specific work.
3. ServletHolder - The Servlet Wrapper & Manager
Jetty uses ServletHolder as a wrapper around your Servlet instance. It’s not just a container—it handles the Servlet’s lifecycle (initializing it with init() when the app starts, destroying it with destroy() when the app stops) and lets you configure Servlet-specific settings (like setting init parameters, giving the Servlet a name, etc.). It’s Jetty’s way of standardizing how Servlets are managed within the framework.
In your code:
ServletHolder myAppServletServletHolder = new ServletHolder(myAppServlet);
This wraps your MyAppServlet so Jetty can manage its lifecycle and settings consistently.
4. ServletContextHandler - The Web App Context Manager
A ServletContextHandler represents a single web application’s context (think of it as the "environment" for your web app). It manages a group of Servlets, filters, and listeners, defines whether the app supports sessions (your code uses ServletContextHandler.SESSIONS to enable session support), and sets the base path for all Servlets registered under it.
In your code:
final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); servletContextHandler.addServlet(myAppServletServletHolder, ("/"+ serviceName)...
This creates a context with session support, then registers your wrapped Servlet to handle requests starting with /abc (since serviceName is 'abc').
How They All Fit Together
The hierarchy and request flow go like this:Server → ServletContextHandler → ServletHolder → Servlet
- A single
Servercan host multipleServletContextHandlerinstances (each representing a separate web app, likemyapp.comandadmin.myapp.com). - Each
ServletContextHandlerholds multipleServletHolderinstances, each mapped to a specific URL path. - Each
ServletHolderwraps oneServletinstance, which does the actual request processing.
When a client sends a request:
- The
Serverreceives the incoming connection. - It routes the request to the correct
ServletContextHandlerbased on the request’s host/path. - The
ServletContextHandlerfinds theServletHoldermapped to the request’s URL path. - The
ServletHoldertriggers the wrappedServlet’s request-handling methods (likedoGet()). - The
Servletgenerates a response, which travels back up the chain to the client.
(Note: Your code snippet is missing the final step of attaching the ServletContextHandler to the Server—you’d normally do something like server.setHandler(servletContextHandler); before starting the server.)
内容的提问来源于stack exchange,提问作者Stephen

