Servlet 3.0 Annotation Example in Java

In the following tutorial we will show you how to create a servlet 3.0 annotation example. The Java Servlet API 3.0 introduces a new package called javax.servlet.annotation. The javax.servlet.annotation provides annotation types which can be used for annotating a servlet class. This is first post of series of Java Servlet Tutorial. This series tutorial will provide you full knowledge about Servlet 3.0.

Table of content:
1. Introduction to Servlet annotation
2. Project structure
3. Maven Dependency
4. Servlet 3.0 Annotation
5. Deploy war file on application server

Servlet annotation
The annotation types introduced in Servlet 3.0 are:

Now we start to build Servlet 3.0 Annotation Example.

Project structure
The project structure should be such as the figure below:

Servlet 3.0 Annotation Example
Servlet 3.0 Annotation Example

Note: This example is using servlet 3.0 annotation configuration so the servlet is not required to add a web.xml.

Maven Dependency
We should add depedency for servlet such as below.

Servlet Container or Application Server has built in servlet support so we need to set the scope is provided.

Servlet 3.0 Annotation

The main focus of this example is the AnnotationServletDemo servlet. You see the @WebServlet annotation that is used to define a Servlet Component in a web application. The value attribute is required and maps the servlet to an url pattern. Here the value is “/example”.

Deploy war file on application server.

After push the file war and run application server we access the address http://localhost:8080/annotation-servlet3/example. The result should be such as the figure below.

Servlet 3.0 Annotation Example
Servlet 3.0 Annotation Example

That’s all. Now you could base on this example to build your own servlet example. Welcome any opinion from your side. You can also find other servlet example in the series of Java Servlet Example.

Download complete source code, please click link below

Servlet-3-Annotation-Example.zip (43 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons