Servlet Http Session Binding Listener project

Click here to download eclipse supported ZIP file




 

    
/**
 
 */
package com.cv.session;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

/**
 @author Chote
 
 */
public class Dog implements HttpSessionBindingListener   {

  private String breed;

  public Dog() {

  }

  public Dog(String arg) {
    System.out.println("dog obj created===" + arg);
    breed = arg;

  }

  public String getBreed() {
    System.out.println("dog obj return==="+breed);
    return breed;
  }

  @Override
  public void valueBound(HttpSessionBindingEvent arg0) {
    System.out.println("valueBound is called==="+arg0);
    
  }

  @Override
  public void valueUnbound(HttpSessionBindingEvent arg0) {
    System.out.println("valueUnbound is called==="+arg0);
    
  }
}


 

    
package com.cv.session;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class MySessionAttributeListener implements HttpSessionAttributeListener {
  private static final long serialVersionUID = 1L;

  /**
   @see HttpServlet#HttpServlet()
   */
  public MySessionAttributeListener() {
    super();
    System.out.println("MyServletContextListener obj created===");
  }

  @Override
  public void attributeAdded(HttpSessionBindingEvent arg0) {

    System.out.println("attributeAdded obj added to the session...");
  }

  @Override
  public void attributeRemoved(HttpSessionBindingEvent arg0) {
    System.out.println("attributeRemoved obj added to the session...");

  }

  @Override
  public void attributeReplaced(HttpSessionBindingEvent arg0) {
    System.out.println("attributeReplaced obj added to the session...");
  }

}


 

    
package com.cv.session;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;



public class MySessionListener implements HttpSessionListener {
  private static final long serialVersionUID = 1L;

  /**
   @see HttpServlet#HttpServlet()
   */
  public MySessionListener() {
    super();
    System.out.println("MyServletContextListener obj created===");
  }

  @Override
  public void sessionCreated(HttpSessionEvent arg0) {
    System.out.println("sessionCreated===" + arg0);
    HttpSession type = arg0.getSession();
    String value = type.getServletContext().getInitParameter("context");
    System.out.println("type is===" + value);
    Dog d = new Dog(value);
    System.out.println("dog object adding to session...");
    type.setAttribute("sessAttr", d);    
    System.out.println("dog object addded to session...");
    System.out.println("===========================");
    
    Dog dog = (Dog)type.getAttribute("dogAttr");
    
    System.out.println("dog object is==="+dog);

  }

  @Override
  public void sessionDestroyed(HttpSessionEvent arg0) {
  System.out.println("sessionDestroyed is called...");

  }

}


 

    
package com.cv.session;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

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

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  /**
   @see HttpServlet#HttpServlet()
   */
  public TestServlet() {
    super();
    // TODO Auto-generated constructor stub
  }

  /**
   @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doGet(HttpServletRequest request,
      HttpServletResponse responsethrows ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    out.println("  <BODY>");
    out.print("    This is ");
    out.print(this.getClass() "<br>");

    PrintWriter aPW = response.getWriter();

    // String scontext = getServletConfig().getInitParameter("init");

    HttpSession httpSession =  request.getSession();    
    Dog dog = (DoghttpSession.getAttribute("sessAttr");
    System.out.println(dog.getBreed());
    aPW.print(dog.getBreed());
    out.flush();
    out.close();

  }

  /**
   @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
   *      response)
   */
  protected void doPost(HttpServletRequest request,
      HttpServletResponse responsethrows ServletException, IOException {
    doGet(request, response);
  }

}


 

    
package com.cv.servlet.session.listener;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 *
 */
public class Dog implements HttpSessionBindingListener {

  private static final Logger LOGGER = Logger.getLogger(Dog.class);

  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Dog() {
  }

  public Dog(String name) {
    this.name = name;
  }

  @Override
  public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
    LOGGER.info("Entered into valueBound(--) of Dog class... ");
    Dog dog = (DoghttpSessionBindingEvent.getValue();
    if (dog != null) {
      LOGGER.info("Value of the Session attribute is : " + dog.getName());

    }
    LOGGER.info("Checking whether it is stored as session attribute : "
        + httpSessionBindingEvent.getSession().getAttribute("dog"));
    dog.setName("Tommy is the second pet name...");
    // httpSessionBindingEvent.getSession().setAttribute("dog", dog);

  }

  @Override
  public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
    LOGGER.info("Entered into valueUnbound(--) of Dog class... ");
    Dog dog = (DoghttpSessionBindingEvent.getValue();
    if (dog != null) {
      LOGGER.info("Value of the Session attribute is : " + dog.getName());

    }
  }

}


 

    
package com.cv.servlet.session.listener;


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;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class HttpSessionListenerServlet
 
 @author Chandra Vardhan
 *
 */
public class HttpSessionListenerServlet extends HttpServlet {
  
  private static final Logger LOGGER = Logger.getLogger(HttpSessionListenerServlet.class);
  
  private static final long serialVersionUID = 1L;
       
    /**
     @see HttpServlet#HttpServlet()
     */
    public HttpSessionListenerServlet() {
        super();
    }

  /**
   @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {
    LOGGER.info("Entered into doGet(--) of HttpSessionListenerServlet class... ");    
    
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    Dog dog = new Dog("Jokey");
    HttpSession session = request.getSession();
    session.setAttribute("dog", dog);
    LOGGER.info("session attribute added");        
    Dog dogAttribute =(Dog)session.getAttribute("dog");
    out.print("Value of the session attribute is : "+dogAttribute.getName());    
    session.invalidate();
  }

  /**
   @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {
    LOGGER.info("Entered into doPost(--) of HttpSessionListenerServlet class... ");    
    
    doGet(request, response);
  }

}



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Beer title here</title>
</head>
<body>
<form action="HttpSessionListenerServlet" method="post">
<input type="submit" value="Click here to continue...">
</form>
</body>
</html>



#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n




<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>httpSessionBindingListener</artifactId> <version>1.0</version> <packaging>war</packaging> <properties> <log4j.version>1.2.16</log4j.version> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> </dependencies> <groupId>com.cv.servlet.session.listener</groupId> </project>




 
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>HttpSessionListenerServlet</servlet-name> <servlet-class>com.cv.servlet.session.listener.HttpSessionListenerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HttpSessionListenerServlet</servlet-name> <url-pattern>/HttpSessionListenerServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>input.html</welcome-file> </welcome-file-list> </web-app>


No comments:

Post a Comment