Devoxx 08: Major Servlet 3.0 features

Martin Marinschek divided his talk in two main topics: Servlet 3.0 and JSF 2.0.

The first part of the talk showed us the new features planned for Servlet 3.0.

What follows is based on the Public Review draft.
So be sure to check if the following still applies to the final Servlet 3.0 version.

Modularization of the web.xml settings

Right now the web.xml is one big configuration file. In Servlet 3.0 it’s possible to modularize it into multiple files.
These files are called “web-fragment.xml” files and they are merged together on application initialization.
Because these fragments can conflict with each other, the specification has specified rules for conflict resolution. (we could take a deeper look at this in a future blog post).

Martin also said that fragment ordering is not defined; this eg. could mean that a filter you want to be executed first may have to be repeated in multiple fragments.

Annotation support in your servlets

A few annotations were added that you can use in your servlets:

@WebServlet

This annotation is used to define a Servlet component in a web application. The urlPatterns or the value attribute on the annotation MUST be present. (value is for 1 URL mapping, urlPatterns for multiple URL mappings)
Classes annotated with @WebServlet class MUST extend javax.servlet.http.HttpServlet class except when applied on a JAX-RS / JAX-WS endpoint.

Example:

@WebServlet(name=”MyServlet”, urlPatterns={“/foo”, “/bar”})
public class SampleUsingAnnotationAttributes extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res) {
}
}

Other optional attributes for the @WebServlet annotation are: supportAsync and asyncTimeout

@ServletFilter

This annotation is used to define a Filter in a web application. Classes annotated with @ServletFilter MUST implement javax.servlet.Filter

@InitParam

This annotation is used to specify Init Parameters for a servlet or a filter

@WebServletContextListener

The WebServletContextListener annotation is used to annotate a context listener to get events for various operations on the particular web application context.
Classes annotated with @WebServletContextListener MUST implement javax.servlet.ServletContextListener

Asynchronous requests

Sometimes a filter and/or servlet is unable to complete the processing of a request without waiting for a resource or event before generating a response. For example, a servlet may need to wait for an available JDBC connection, for a response from a remote web service, for a JMS message, or for an application event, before
proceeding to generate a response. Waiting within the servlet is an inefficient operation as it is a blocking operation that consumes a thread and other limited resources. Frequently a slow resource such as a database may have many threads blocked waiting for access and can cause thread starvation and poor quality of service  for an entire web container.

Servlet 3.0 introduces the ability for asynchronous processing of requests so that the thread may return to the container and perform other tasks.

When asyncSupported is set to true in the filter/servlets annotation the application can start asynchronous
processing in a separate thread by calling the HttpServletRequest.startAsync method passing it a
reference to the request and response objects.

After I’ve played with the asynchronous requests I’ll post some code samples to Java Blog to explain how exactly it works.

2 thoughts on “Devoxx 08: Major Servlet 3.0 features”

  1. “After I’ve played with the asynchronous requests I’ll post some code samples to Java Blog to explain how exactly it works.”

    -Please do, I am anxious to see working examples of Async. request operations. This is a huge requirement that I have, right now it’s practically impossible…

    Thanks for the update…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.