Servlet annotations and their advantages over xml

In this post, you will learn:

  • Annotations and their types.
  • Limitations of XML based configuration.
  • Servlet annotations and their use.

What is an annotation?

An annotation is a class member, the purpose of which is to describe other members of the class. The literal meaning of the word annotation is: a note added to describe or clarify something. In real life we all use annotations. When you write the meaning of a word over it at the time of reading a book, you are actually annotating the word.

Why annotations are required?

A web server, framework or runtime environment which manages user defined classes, need some configuration information to manage them. Conventionally configuration information is provided in XML format e.g. in a web application, web.xml file is used to provide the configuration of servlets, listeners and filters to the web server. XML based configuration has following drawbacks:

1. A separate configuration file need to be maintained in the application.

2. More words are used to describe the configuration than the configuration itself. Such description is called verbose. e.g. in the following web.xml file configuration information is of two words (S1 & WelcomeServlet) rest of the information (<web-app>, <servlet>, <servlet-name>, <servlet-class> and their closing elements) is the description of the configuration.

<web-app>
 <servlet>
    <servlet-name>s1</servlet-name>
    <servlet-class>com.techmentro.learningpad.WelcomeServlet</servlet-class>
  </servlet>
</web-app>

Annotations were introduced in Java 5 to be an alternative to XML based configuration. Annotations remove both the drawbacks of XML based configuration.

1. Addition of the configuration directly to the class and its members in the form of annotations, eliminate the need of separate configuration file.

2. When the configuration is directly added to a class or its member then no additional description is required i.e. annotations based configuration is concise in comparison to XML based configuration.

Following diagram describes, how annotations based configuration is used by servers, frameworks and run time environments:

annotations

Description of the diagram:

1.0 – Configuration information of the class is added to the class itself with the help of annotations by the programmer.

1.1 – When the source file is compiled, annotations are stored in the class file by the compiler along with other information of the class.

1.2 – When the class is loaded by the class loader, annotations are loaded into the class object along with fields, methods and constructors.

1.3 – Server, Frameork or a run time environment obtains the annotations from the class object and uses their information to manage the class and its objects.

Types of annotations:

Annotations are of two types: Marker & Non-Marker. The difference between these two, is of parameters. Marker annotations don’t accept parameters whereas non-marker do. Parameters are used to provide additional information as part of the annotation.

Syntax of using a marker annotation:

@AnnotationName

Syntax of using a non-marker annotation:

@AnnotationName(paramName=value,…)

Limitation of annotations:

As XML based configuration has limitations similarly, annotations do have following limitations:

1. If configuration information is to be changed, classes need to modified and recompiled because annotations are class member.

2. Common configuration such as application scope parameters for which no class is defined can’t be provided through annotations.

Servlet annotations:

In 3rd release of servlet API, following annotations were introduced to be used for providing the configuration of web application components to the web servers.

1. @WebServlet –It is a non-marker annotation which is used to provide the configuration of a servlet to the server.

2. @WebFilter – It is a non-marker annotation which is used to provide the configuration of filters to the server.

3. @WebListener – It is a marker annotation which is used to mark a component as listener.

4. @WebInitParam – It is a non-marker annotation which is used to define initialization parameters for servlets and filters.

In a servlet 3 API compatible web application, configuration can be provided:

1. –Through web.xml as before.

2. – Through annotations.

3. – Through both web.xml as well as annotations.

In the next post, practical use of the servlet annotations will be demonstrated.


Its time to recap the main concepts:

  1. Annotations are class members just like attributes, methods and constructors.
  2. Annotations are used to describe other members of the class.
  3. Annotations are of two types: marker & non-marker.
  4. In servlet 3 API, support of annotation based configuration was added.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

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