Servlet Annotation Hello World Example in Eclipse

In this tutorial you will learn how to create an annotated servlet in web applications. In this example I am trying to demonstrate you the basic concepts for creating an annotated servlet using the Eclipse Helios IDE and the Tomcat7 application server.

Servlet Annotation Hello World Example in Eclipse

In this tutorial you will learn how to create an annotated servlet in web applications. In this example I am trying to demonstrate you the basic concepts for creating an annotated servlet using the Eclipse Helios IDE and the Tomcat7 application server.

Servlet Annotation Hello World Example in Eclipse

Servlet Annotation Hello World Example in Eclipse

In this tutorial you will learn how to create an annotated servlet in web applications. In this example I am trying to demonstrate you the basic concepts for creating an annotated servlet using the Eclipse Helios IDE and the Tomcat7 application server.

Because we are going to create an annotated servlet in jee6 so, to support the jee6 specifications we must have to ensure about these following requirements :

  • At least jdk6 or higher version must be installed and its path in the environment variable be set in your computer, if you haven't the jdk6 you can download from the link http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u29-download-513648.html.
  • You must have the Tomcat7 application server. If you haven't the installed Tomcat7 read complete tutorial from here.
  • At least you should have Eclipse helios or higher version because the lower version may not support the tomcat7. If you haven't download the Eclipse helios read complete tutorial from here.

If any of the above is not available to you possibly there may be a problem in creating your application. So, make sure that the above mentioned requirements are fulfilled by you.

After assured about the above preliminary requirements now we are able to create an annotated servlet in a web application. Now let's try to create an annotated servlet. You will be not required to give the servlet information into the web.xml file here. To create an annotated servlet in a web applications there is an annotation @WebServlet(started from the servlet 3.0 specification) which is defined in javax.servlet.annotation package. This annotation has the various attributes which is given at the time of servlet creation by separating comma( , ).

Example :

The directory structure of your project should be like as :

In the above image you can clearly see that the web.xml file is not provided by the IDE, so if at any stage you are required to give the information to the container by the web.xml file you can create yourself web.xml file, this file should be store in the WEB-INF folder. When we are using the servlet 3.0 specifications it depends on the element metadata-complete value by true/false whether the container will use web.xml or annotations in the web.xml descriptor. The true value of attribute specifies that the annotation will not be processed by the container and web fragments in such case all the metadata information is provided by the web.xml file to the container and the false value of attribute or the absence of element metadata-complete specifies that the annotations and web fragments will be used by the container. 

MyAnnotationServlet.java

package roseindia.net;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "MyAnnotationServlet", urlPatterns = {"/hello"})
public class MyAnnotationServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h2>Hello World Servlet Annotation Example</h2>");
out.close();
}
}

Output :

Download Source Code