A Brief Introduction to Servlet Annotation in Java


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.


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


Syntax of applying non-marker annotation

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


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


Class Annot
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

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.



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


Describe each Retention Policy in detail


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.


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.


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)
@interface Override 
   {  }

2. @WebServlet annotation is defined as follows

@Target (ElementType:TYPE)
@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.


public boolean isAnnotationPresent (Class AnnotationClass);

2. getAnnotation()

Used to obtain an object of the specified annotation.


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.


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.


Step 2

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


Step 3

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


Step 4

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


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.


Step 6

Now provide code for it.


import java.io.*;

import javax.servlet.*;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.*;

import javax.servlet.*;


public class ServletDemo extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {


        PrintWriter out = response.getWriter();


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





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.



This Servlet is deployed using annotation.