Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

AnnotationServlet


Annotation represents the metadata. If you use annotation, deployment descriptor (web.xml file) is not required. But you should have tomcat7 as it will not run in the previous versions of tomcat. @WebServlet annotation is used to map the servlet with the specified name.

Example of simple servlet by annotation

There is given the simple example of servlet with annotation.

Simple.java

  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. @WebServlet(“/Simple”)
  9. public class Simple extends HttpServlet {
  10.     private static final long serialVersionUID = 1L;
  11.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  12.                             throws ServletException, IOException {
  13.         response.setContentType(“text/html”);
  14.         PrintWriter out=response.getWriter();
  15.         out.print(“<html><body>”);
  16.         out.print(“<h3>Hello Servlet</h3>”);
  17.         out.print(“</body></html>”);
  18.     }
  19. }
servletannotation

Comments

comments