Max A. Max A. - 1 year ago 92
Java Question

What are the differences between Servlet 2.5 and 3?

I'm rolling J2EE code that adheres to Servlet 2.5 and I'm wondering what are the major differences between 2.5 and 3. Pointers to official Sun docs and personal experiences are most appreciated.

If I shouldn't be concerning myself with 3 for the time being, just say so. Thanks!

Answer Source


Just as an update and to be more explicit, these are the main differences between servlets 2.5 and 3 (I'm not trying to be exhaustive, I'm just mentioning the most interesting parts):

Annotations to declare servlets, filters and listeners (ease of development)

In servlets 2.5, to declare a servlet with one init parameter you need to add this to web.xml:



In servlets 3, web.xml is optional and you can use annotations instead of XML. The same example:

    initParams={@InitParam(name="configFile", value="config.xml")})
public class MyAwesomeServlet extends HttpServlet { ... }

For filters, you need to add this in web.xml in servlets 2.5:


Equivalent using annotations in servlets 3:

@ServletFilter(name="myFilter", urlPatterns={"/path/to/my/filter"})
public class MyAwesomeFilter implements Filter { ... }

For a listener (in this case a ServletContextListener), in servlets 2.5:


The same using annotations:

public class MyAwesomeListener implements ServletContextListener { ... }

Modularization of web.xml (Pluggability)

  • In servlets 2.5 there is just one monolithic web.xml file.
  • In servlets 3, each "loadable" jar can have a web-fragment.xml in its META-INF directory specifying servlets, filters, etc. This is to allow libraries and frameworks to specify their own servlets or other objects.

Dynamic registration of servlets, filters and listeners at context initialization time (Pluggability)

In servlets 3, a ServletContextListener can add dynamically servlets, filters and listeners using the following methods added to SevletContext: addServlet(), addFilter() and addListener()

Asynchronous support

Example: say that some servlet container has five threads in its thread pool, and there is a time-consuming process to be executed per request (like a complex SQL query).

  • With servlets 2.5 this servlet container would run out of available threads if it receives five requests at the same time and the five available threads start doing the process, because the threads wouldn't return until service() (or doGet(), doPost(), etc.) is executed from start to end and returns a response.

  • With servlets 3.0, this long-time process can be delegated to another thread and finish service() before sending the response (the response now will be sent by the latest thread). This way the thread is free to receive new responses.

An example of asynchronous support:

Servlets 2.5:

public class MyAwesomeServlet extends HttpSerlvet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        // ...

        // no async support, thread will be free when runSlowProcess() and
        // doGet finish

        // ...


Servlets 3:

             asyncSupported=true) // asyncSupported MUST be specified for
                                  // servlets that support asynchronous
                                  // processing
public class MyAwesomeServlet extends HttpSerlvet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) {

        // an AsyncContext is created, now the response will be completed
        // not when doGet finalizes its execution, but when
        // myAsyncContext.complete() is called.
        AsyncContext myAsyncContext = request.startAsync(request, response);

        // ...

        // myAsyncContext is passed to another thread

        // done, now this thread is free to serve another request


// ... and somewhere in another part of the code:

public class MyProcessingObject {

    public void doSlowProcess() {

        // ...

        myAsyncContext.complete(); // request is now completed.

        // ...



The interface AsyncContext also has methods to get the request object, response object and add listeners to notify them when a process has finished.

Programmatic login and logout (security enhancements)

In servlets 3, the interface HttpServletRequest has been added two new methods: login(username, password) and logout().

For more details, have a look at the Java EE 6 API.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download