What are the differences between Servlet 2.5 and 3?

JavaServletsJakarta Ee

Java Problem Overview


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!

Java Solutions


Solution 1 - Java

UPDATE

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:

<servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>my.server.side.stuff.MyAwesomeServlet</servlet-class>
    <init-param>
        <param-name>configFile</param-name>
        <param-value>config.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/path/to/my/servlet</url-pattern>
</servlet-mapping>

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

@WebServlet(name="myServlet",
    urlPatterns={"/path/to/my/servlet"},
    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:

<filter>
    <filter-name>myFilter</filter-name>
    <filter-class>my.server.side.stuff.MyAwesomeServlet</filter-class>
</filter>
<filter-mapping>
    <filter-name>myFilter</filter-name>
    <url-pattern>/path/to/my/filter</url-pattern>
</filter-mapping>

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:

<listener>
    <listener-class>my.server.side.stuff.MyAwesomeListener</listener-class>
</listener>

The same using annotations:

@WebServletContextListener
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 {

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

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

        // ...
    }

}

Servlets 3:

@WebServlet(name="myServlet",
             urlPatterns={"/mySlowProcess"},
             asyncSupported=true) // asyncSupported MUST be specified for
                                  // servlets that support asynchronous
                                  // processing
public class MyAwesomeServlet extends HttpSerlvet {

    @Override
    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
        delegateExecutionToProcessingThread(myAsyncContext);

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

}

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

public class MyProcessingObject {

    public void doSlowProcess() {

        // ...

        runSlowProcess();
        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.

Solution 2 - Java

Servlet 3.0 has not yet been released, but it looks like it's very close. The most important changes in 3.0 are: Pluggability, Ease of development, Async Servlet, Security. Whether or not these are important to you is impossible for me to say.

The most significant of these is probably the support for asynchronous Servlets. Here's an article that describes this in detail. The full specification can be downloaded here.

Solution 3 - Java

As Don mentioned, the main areas of improvements and additions are:

  • Pluggability (modularizing of web.xml)
  • Ease of development (annotations, generics, convention over configuration)
  • Async servlet support (for comet style programming, async web proxy, async web services)
  • Security enhancements (programmatic login/logout)
  • Others (HttpOnly Cookie, Session tracking, EJB in WAR file)

Check out the Javaone 2008 presentation "Java Servlet 3.0 API: What's new and exciting" for details.

Solution 4 - Java

This link will give enough info on Servlet 3

Servlet 3 supports annotation to eliminate web.xml

@WebServlet
@WebServletContextListener
@ServletFilter
@InitParam

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMax A.View Question on Stackoverflow
Solution 1 - JavamorganoView Answer on Stackoverflow
Solution 2 - JavaDónalView Answer on Stackoverflow
Solution 3 - JavaPascal ThiventView Answer on Stackoverflow
Solution 4 - JavaRavi ParekhView Answer on Stackoverflow