New Features in Servlets 2.5

1.Introduction

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="http://java.sun.com/xml/ns/javaee" version="2.5" metadata-complete="true">
</web-app> 

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

2.1.@EJB

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;

2.2.@EJBs

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

 @EJBs({@EJB(LoginSample),@EJB(ProcessSample)})

2.3.@Resource

@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;

2.4.@Resources

@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) })

2.5.@PostConstruct

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() { }

2.6.@PreDestroy

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.

4.1.servlet-mapping

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:

<servlet-mapping>
	  <servlet-name>JavaBeatServlet</servlet-name>
	  <url-pattern>/servlets/*</url-pattern>
	  <url-pattern>/servlet/javabeat/*</url-pattern>
	  <url-pattern>/*</url-pattern>
	</servlet-mapping>

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.

4.2.filter-mapping

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

<filter-mapping>
	  <filter-name>Java Filter</filter-name>
	  <servlet-name>*</servlet-name>
	</filter-mapping>

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-mapping>
	  <filter-name>Game Filter</filter-name>
	  <url-pattern>/games/*</url-pattern>
	  <servlet-name>PlayGame</servlet-name>
	  <servlet-name>StopGame</servlet-name>
	</filter-mapping>

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

also read:

5.Summary

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

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*