3.0.1

Latest release in branch 3.0
Released 14 years ago (July 12, 2011)

Software Servlet/Jakarta
Branch 3.0
First official release version 3.0.1
First official release date 14 years ago (July 12, 2011)
Platform Java EE 6, Java SE 6
Documentation https://javadoc.io/doc/javax.servlet/javax.servlet-api/3.0.1/index.html
Download https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api/3.0.1
Servlet/Jakarta 3.0 Releases View full list

What Is New in Servlet 3.0

Servlet 3.0 was a major overhaul that shifted how we build web applications on the JVM. It introduced a programming model centered around annotations and programmatic configuration, moving away from the rigid web.xml-only approach.

Category Key Changes
New Features Annotations, Programmatic configuration, Web Fragments, Asynchronous processing, Pluggability
Improvements Ease of development, Dynamic registration, Enhanced security
Additions New annotations (@WebServlet, @WebFilter, @WebListener), ServletContext APIs

How did annotations change Servlet development?

Annotations eliminated the need for verbose XML descriptors for most common tasks. You can now declare a servlet, filter, or listener directly in the Java class file.

This means a servlet is defined with @WebServlet("/url") right above the class declaration. In practice, this drastically reduces the size and complexity of the web.xml file and makes code easier to understand and maintain.

Example

@WebServlet(urlPatterns = {"/hello"})
public class HelloServlet extends HttpServlet {
    // Servlet code
}

What are Web Fragments and pluggability?

Web Fragments allow frameworks and libraries to self-configure. A JAR file can contain a web-fragment.xml descriptor that gets automatically merged into the overall application configuration.

This matters because it enables true modularity. Frameworks like Jersey or Spring MVC can bundle their necessary servlets and filters without requiring you to manually copy-paste configuration into your main web.xml.

Why is asynchronous processing a big deal?

Asynchronous processing lets a servlet thread release itself back to the container to handle other requests while waiting for a long-running task to complete. This prevents thread starvation and improves scalability for I/O-bound operations.

You start async processing by calling request.startAsync(). This returns an AsyncContext which you use to complete the response later from another thread. It's essential for handling Comet or Ajax long polling efficiently.

Core Methods

AsyncContext aCtx = request.startAsync();
// ... pass context to another thread
aCtx.complete();

How does programmatic configuration work?

You can dynamically add servlets, filters, and listeners at runtime using the ServletContext API. This is done by implementing the ServletContainerInitializer interface.

This is powerful for framework developers. It allows for code-based configuration that can be more flexible than XML, enabling setups that adapt based on the environment or other conditions discovered at startup.

Were there any security enhancements?

Yes, Servlet 3.0 added a few security refinements. It introduced the HttpServletRequest.authenticate(), login(), and logout() methods for programmatic login.

This gives developers more control over the authentication process directly from their servlet code, complementing the existing declarative security model defined in the deployment descriptor.

FAQ

Do I still need a web.xml file in Servlet 3.0?
No, it's optional. You can define almost everything using annotations. The container will scan your classes and process the annotations automatically at deployment. You only need web.xml for overriding annotation values or for settings that have no annotation equivalent.

Can I mix annotations and web.xml?
Yes, you can. The deployment descriptor (web.xml) always takes precedence if there is a conflict with annotation configuration. This allows you to override annotation-based settings for specific environments.

How does the container discover my annotated servlets?
The Servlet container performs bytecode scanning on your application's JAR files during deployment. It looks for classes annotated with @WebServlet, @WebFilter, and @WebListener and registers them.

What is the purpose of the @WebInitParam annotation?
It is used to specify initialization parameters for a servlet or filter directly in the annotation, replacing the <init-param> XML element. For example: @WebServlet(urlPatterns="/test", initParams={@WebInitParam(name="param1", value="value1")}).

Is asynchronous support available for filters?
Yes, filters can also participate in asynchronous request processing. A filter must be explicitly declared to support async by setting the asyncSupported attribute to true in its @WebFilter annotation or in the deployment descriptor.

Releases In Branch 3.0

Version Release date
3.0.1 14 years ago
(July 12, 2011)
3.0-alpha-1 18 years ago
(April 17, 2008)