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 theJava 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.5has 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.
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"&gT; </web-app&gT;
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 @EJBannotation. 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 anyEJB 3.0 reference to that field. Like the following example:
2.2.@EJBs
@EJBs annotation can be used for declraing more than one EJB in single line of declaration. For example:
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:
2.4.@Resources
@Resources annotation is used for declaring more than one @Resource in a single line. For example:
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.
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.
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.5eased a rule that a servlet called by RequestDispatcher include() couldn’t set response headers.
No comments:
Post a Comment