Saturday 14 June 2014

Servlets : ServletRequestListener Demo



















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

Click the below Image to Enlarge

ServletRequestListenerDemo Project Dir Structure

HitCountServlet.java
import java.io.IOException;
import java.io.PrintWriter;

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

public class HitCountServlet 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("<h2> This is target Servlet </h2>");
        out.print("<h2>the no of hits for this application is "
                + MyServletRequestListener.count);

    }

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

}

MyServletRequestListener.java
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

public class MyServletRequestListener implements ServletRequestListener
{

    public static int count = 0;

    @Override
    public void requestInitialized(ServletRequestEvent servletRequestEvent)
    {
        count++;

        System.out.println("\n###################################\n");

        System.out.println("requestInitialized method has been called in "
                + this.getClass().getName());

        ServletRequest servletRequest = servletRequestEvent.getServletRequest();

        System.out.print(servletRequest + " is Created or Initialized:");

        System.out.println("\n#####################################\n");

        /*
         * if request is created or initialized, based on that if you want to
         * perform any operation then you can do it here.
         */

    }

    @Override
    public void requestDestroyed(ServletRequestEvent servletRequestEvent)
    {
        System.out.println("\n###################################\n");

        System.out.println("requestDestroyed method has been called in "
                + this.getClass().getName());

        ServletRequest servletRequest = servletRequestEvent.getServletRequest();

        System.out.print(servletRequest + " destroyed:");

        System.out.println("\n#####################################\n");

        /*
         * if request is destroyed , based on that if you want to perform any
         * operation then you can do it here.
         */

    }

}

web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    metadata-complete="true" version="3.0">
    <display-name>ServletRequestListenerDemo</display-name>
    <description>
        This is a simple web application with a source code organization
        based on the recommendations of the Application Developer's Guide.
    </description>

    <servlet>
        <servlet-name>HitCountServlet</servlet-name>
        <servlet-class>HitCountServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HitCountServlet</servlet-name>
        <url-pattern>/hitCount</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>MyServletRequestListener</listener-class>
    </listener>


</web-app>

index.html
<html>
<body>
    <a href="hitCount">hitCount</a>
</body>
</html>

Environment Used 

JDK version :1.7.0_51
Tomcat version : 7.0.50 

To Download ServletRequestListenerDemoApp Project Click the below link

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

See also:

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

    Post a Comment