What Is New in Servlet 4.0
Servlet 4.0 is a major release that aligns with HTTP/2, introducing server push and other enhancements to modernize the Java web ecosystem.
| Category | Key Changes |
|---|---|
| New Features | HTTP/2 Support, Server Push |
| API Improvements | New PushBuilder interface, Mapping API enhancements |
| Requirements | Mandates Java 8 or later |
How does HTTP/2 change Servlet development?
The core of Servlet 4.0 is full support for HTTP/2. This isn't just about performance; it changes how we think about connections. Servlets can now handle multiplexed streams over a single connection, reducing latency.
In practice, you don't need to rewrite your servlets to benefit. The container handles the HTTP/2 protocol negotiation. Your existing HttpServletRequest and HttpServletResponse objects work as before, but they now operate over a more efficient transport layer.
What is the new PushBuilder for?
Server Push is HTTP/2's killer feature, and Servlet 4.0 gives you direct access to it via the PushBuilder interface. You get it from the HttpServletRequest object and use it to proactively send resources to the client.
This is perfect for pushing critical CSS, JavaScript, or images before the client even knows to ask for them. It cuts down on those round trips that slow down page loads. You use it right in your servlet's service method.
PushBuilder pb = request.newPushBuilder();
if (pb != null) {
pb.path("styles.css").push();
}
Are there any improvements to mapping requests?
Yes, the mapping API got a useful upgrade. The new HttpServletMapping interface lets you inspect exactly how a request was matched to your servlet.
You can check the match type (e.g., exact, extension, wildcard), the pattern that was used, and the matched value. This is great for logging and for writing more dynamic servlets that need to understand their routing context.
What are the new service-ready requirements?
Servlet 4.0 containers must now support the addition of HTTP trailer fields. Trailers are headers sent after the response body, which is a feature of HTTP/2.
The spec also clarifies requirements around request/response content types and character encoding. This makes servlet behavior more predictable across different container implementations, which is something we appreciate when debugging.
FAQ
Do I need to change my existing servlets to work with Servlet 4.0?
No. All your existing servlets will continue to work unchanged. The HTTP/2 protocol handling is managed by the container. You only need to use new APIs like PushBuilder if you want to leverage new features.
Is server push automatic, or do I have to implement it?
You have to implement it. The container won't push resources automatically. You use the request.newPushBuilder() method to get a PushBuilder and explicitly define which resources to push to the client.
Can I use Servlet 4.0 with Java 11?
Yes. While the specification mandates a minimum of Java 8, it is fully compatible with Java 11 and later LTS versions. Your container (like Tomcat 9+) must be configured for the correct Java version.
What happens if a client doesn't support HTTP/2?
The servlet container automatically falls back to HTTP/1.1. Your servlet code doesn't need any conditional checks for this. The newPushBuilder() method will return null if the connection doesn't support server push.
Where can I find the official specification?
The Jakarta Servlet 4.0 specification is the definitive source. You can find the PDF and other resources at the official Jakarta EE website.