A Brief Introduction to Servlet Annotation in Java

Introduction

This article provides a brief introduction about annotation used in servlets. The Netbeans IDE is used for the example.

What is Annotation

Represents configuration information of classes and there members. This information is contained in the class at the time of definition. The concept of annotation was introduced by Java5 as an alternative to XML. Usually all web servers, run-time environments and frameworks require some configuration information to manage applications and classes.

Before annotation, configuration was provided to servers and frameworks in XML format. XML based configuration has the following drawbacks.

  • Verbosity (lengthy code)
  • An additional file needs to be maintained.

Annotation removes both of these problems.

The following diagram describes the use of annotation information by web-servers. Framework or run-time environment.

Fig-1.jpg

Annotation is one of the following two types:

  1. Marker annotation
  2. Non-marker annotation

Marker annotation does not carry additional information whereas non-marker annotation carries additional information.

Syntax of applying Marker annotation

@AnnotationName

Syntax of applying non-marker annotation

@AnnotationName (attribute=value,....)

Example

@Override and @Deprecated two predefined marker annotation is used to tell the compiler that we are overriding a method. The purpose of this annotation is to detect unintentional overloading in place of overriding.

@Deprecated annotation is used to tell the compiler that an annotated class or method is deprecated. When an annotated class/method is used by an application programmer the compiler generates a warning.

Example

Class Annot
{
@Override
public boolean equals(Annot x)
{
//Compiler will generate
//Error because method is
//overloaded instead of overridden.
}
}

In Servlet3.0, @WebServlet non-marker annotation is provided to provide the information of a servlet to the server.

@WebServlet (name="ServletName" url="/ServletName")
public class WelcomeServlet extends HttpServlet
{
------
------
}

How to define Annotation

1. Syntax of defining marker annotation

@interface AnnotationName
{
}

2. Syntax of defining non-marker annotation

@interface AnnotationName
{
type__________MemberName();
}

Each annotation is defined to annotate either a class or its members. At the time of defining the annotation element type is supplied for the target element that can be annotated by the annotation.

@Target predefine annotation is used to specify the target of an annotation following can be the values of this annotation.

@Target
ElementType:TYPE
ElementType:METHOD
ElementType:FIELD

etcetera

Each annotation has a section policy associated to it value of retention policy returns the scope of the annotation.

@Retention predefine annotation is used to specify the retention policy of a annotation following can be the values of this annotation

@Retention
RetentionPolicy:SOURCE
RetentionPolicy:CLASS
RetentionPolicy:RUNTIME

Describe each Retention Policy in detail

1. SOURCE

Anannotation having a SOURCE retention policy is retained only in the source file, in other words they are not transferred to the class file at the time of translation. Such annotations are used to provide the same information to the compiler @Override and @Deprecated are annotations of the SOURCE retention policy.

2. CLASS

An annotation having a CLASS retention policy is named to the class file but are not loaded into class object. Such annotations are used to provide some information to the class-loader.

3. RUNTIME

An annotation having a RUNTIME policy is moved to the CLASS file at the time of translation and to the class object at the time of class-loading. Such annotations are used to provide information to the RUNTIME environment, Web-Server, Frameworks, etcetera.

Now understand the definition of predefined annotations

1. @Override annotation is defined as

@Target (ElementType:METHOD)
@Retention(RetentionPolicy:SOURCE)
@interface Override 
   {  }

2. @WebServlet annotation is defined as follows

@Target (ElementType:TYPE)
@Retention(RetentionPolicy:RUNTIME)
@interface WebServlet
{
   String name();
   String url();
}

The following methods are added to java.lang.Class to support annotations.

1. isAnnoationPresent()

Used to determine whether an annotation is available in the class object or not.

Syntax

public boolean isAnnotationPresent (Class AnnotationClass);

2. getAnnotation()

Used to obtain an object of the specified annotation.

Syntax

public Object getAnnotation (Class AnnotationClass);

Servlet3 specification provides following Annotations

1. @WebServlet

Used to provide a Servlet information to the server.

2. @WebListener

Used to provide information to the listener to the server.

3. @WebFilter

Used to provide information of a filter to the server.

4. @InitParam

Used to provide initialization parameters to servlets and filters.

In order to use this annotation in a web application the application must be deployed in a servlet3.0 compatible web or application server.

Note:

Tomcat7 or Glassfish 3.0 provides implementation of Servlet 3.0 specification.

Let's use an example for better understanding about annotation advantages.

1. Applying annotation with servlets

In this example we create a servlet that deploys without the use of a XML file. Instead of XML we use Annotation over there.

We need to use the following procedure.

Step 1

Open the NetBeans IDE.

Fig-2.jpg

Step 2

Now select "Java web"  -> "Web application".

Fig-3.jpg

Step 3

Now type "AnnotationDemo" as your project name and click on "Next" as shown below.

Fig-4.jpg

Step 4

Select your server and Java version and click on "Finish" as shown below.

Fig-5.jpg

Step 5

Delete the default created file as "index.jsp" and create a new Servlet with the name "ServletDemo" and click on "Finish" as shown below.

Fig-6.jpg

Step 6

Now provide code for it.

ServletDemo.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.*;

import javax.servlet.*;

@WebServlet("/ServletDemo")

public class ServletDemo extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/html");

        PrintWriter out = response.getWriter();

        out.println("<html><body>");

        out.println("<h1>Welcome To Servlet</h1>");

        out.println("</body></html>");

    }

}

Fig-7.jpg

Step 7

Now our project is ready to run.

Right-click on the project menu item then choose "Run". The following output will be produced.

Fig-8.jpg

Note

This Servlet is deployed using annotation.


Similar Articles