Friday, 6 June 2014

Servlet with Annotation


Click here to watch in Youtube : https://www.youtube.com/watch?v=9BkjzpuJK4w

Click the below Image to Enlarge
Servlet with Annotation
AnnotationServletDemo Project Dir Structure
Servlet with Annotation

HelloServlet.java
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("/hello")
public class HelloServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    public void init() throws ServletException
    {
        System.out.println("-----------------------------------------");
        System.out.println(" Init method is called in "
                + this.getClass().getName());
        System.out.println("--------------------------------------");
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.print("<html><body>");
        out.print("<h3>Hello Servlet</h3>");
        out.print("</body></html>");

    }

    public void destroy()
    {
        System.out.println("-----------------------------------------");
        System.out.println(" destroy method is called in "
                + this.getClass().getName());
        System.out.println("-----------------------------------------");
    }

}

index.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ServletContextAttributeDemo</title>
</head>
<body>
    <form action="hello" method="GET">
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

Environment Used 

JDK version :1.7.0_51
Tomcat version : 7.0.50 

To Download AnnotationServletDemoApp Project Click the below link

https://sites.google.com/site/javaee4321/servlets/AnnotationServletDemoApp.zip?attredirects=0&d=1

See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge