New Features in Servlets 2.5


This article presents new features introduced in the Servlet 2.5. Servlet 2.5 is bundled with Java EE 5.0 edition and it has lot of new features. This version has major changes since it has to support the Java 5.0 version for all the J2EE technologies. One of the notable changes will be supporting Generics and Annotations. Using annotations in Java EE environment has simplified the developement process by eliminzating the much need for XML deployment descriptors.

EJB 3.0 is example for how it is simple compare to its previous versions. In the same way other technologies also have significent updates. One particular technology is Servlet and it is evloving very quickly. This Servlet 2.5 has few changes and the next version Servlet 3.0 has very exciting features like Web Fragments. The minimum requirement for the Servlet 2.5 is JDK 5.0 or above. Lets look into the Servlet 2.5 features in this article.

also read:

The following are the list of notable features in servlet 2.5:

  • Support for Annotations
  • Deployment descriptor changes
  • Removed restriction on error handling

2.Annotations in Servlet 2.5

Annotations are a new language feature provided as part of JSR 175 (A Metadata Facility for the Java Programming Language). One of the major change in the Servlet 2.5 is its full support for the Java 5.0 version. The minimum requirement for Servlet 2.5 is the JDK 5.0 installed in your system. Servlet 2.5 introduces annotations programming in the Servlets technology.

The annotations has to be loaded into the containers memory while server start up. This will take up more time since the container has to scan all the files in the classpath including jar files to find all the annotations. There is option to diable annotations by using the metadata-complete attribute in the web.xml file. If you set this value as “true”, then the container will ignore all the annotations in the Servlet files. If you specify as “false” or ignore that, by default it will search for the annotations declaration. Look into the folloing sample code:

<web-app xmlns="" version="2.5" metadata-complete="true">

By default container will look into folders WEB-INF/classes and WEB-INF/lib for loading the servlets. Optionally container can search on the other location also. We will look into the details of few annotation in the next sections. The following are the few annotations introduced in the Servlet 2.5:

  • @DeclaresRoles
  • @EJB
  • @EJBs
  • @Resource
  • @Resources
  • @PostConstruct
  • @PreDestroy
  • @PersistanceContext
  • @PersistanceUnit


EJB 3.0 components can be injected to a Servlet class by using the @EJB annotation. In the deployment descriptors this can be declared using the ejb-ref and ejb-local-ref elements. @EJB annotation can be used infront of a field to inject any EJB 3.0 reference to that field. Like the following example:

 @EJB private LoginSample loginSample;


@EJBs annotation can be used for declraing more than one EJB in single line of declaration. For example:



@Resource annotation used for declaring any of the resources like datasource, messaging destination,etc. @Resource annotation can be used for class,method or a field. Look into the following example:

 @Resource private javax.sql.DataSource mysqlDs;


@Resources annotation is used for declaring more than one @Resource in a single line. For example:

 @Resources({@Resource(name="mysqlDs" type=javax.sql.DataSource),
 @Resource(name="factory" type=javax.jms.ConnectionFactory) })


The @PostConstruct annotation is declared on a method that does not take any arguments, and must not throw any checked expections. The return value must be void. The method MUST be called after the resources injections have been completed and before any lifecycle methods on the component are called.

 @PostConstruct public void postConstruct() { }


The @PreDestroy annotation is declared on a method of a container managed component. The method is called prior to component being reomvoed by the container.

 @PreDestroy public void preDestroy() { }

3.Error Handling

Servlet 2.5 removed a few restrictions around error handling and session tracking. Now it has removed the restriction that the <error-page> could not call the setStatus() method to alter the error code that triggered them. In session tracking, Servlet 2.5 eased a rule that a servlet called by RequestDispatcher include() couldn’t set response headers.

4.Servlets 2.5 web.xml changes

Servlets 2.5 has added few changes on the web.xml file. It is not major changes, but even though gives more
flexibility on mapping the filters and servlets. The two changes are in the filter-mapping and servlet-mapping elements.


In the previous version you are allowed to use only the single url-pattern inside the servlet-mapping. This has the restriction or drawback if you want to map more than one url for the single servlet. Servlet 2.5 updated this element so that you can add more than one url-pattern for the single servlet mapping. Look into the following example code:


The above snippet adds three url-pattern inside the single servlet-mapping element. This is more useful when you have the requirement to add three different url’s to be mapped for a single servlet-name element.


Like the above changes in the servlet-mapping, filter-mapping has two changes. First one is introducing the wildcard(*) for filter-mapping.

	  <filter-name>Java Filter</filter-name>

In the above example, all the requests will be redirected to the particular filter name. Another change is multiple entry in the
filter-mapping. It is same as the servlet mappintg, we can add multiple entries in the filter-mapping.

	  <filter-name>Game Filter</filter-name>

The above code is example for multiple entries in the filter-mapping.

also read:


This article explains what are the new features in Servlet 2.5 version. The main goal of this release is to provide Java 5.0 support and to start using the features like annotations and generics. In the next versions we can see there will be more changes.

Servlets 2.5 Reference

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!