Click here to watch in Youtube : https://www.youtube.com/watch?v=9ciciMjc8es
Click the below Image to Enlarge
HttpSessionAttributeListenerDemo Project Dir Structure
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class HttpSessionAttributeTestServlet 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"); HttpSession httpSession = request.getSession(); sleep(); httpSession.setAttribute("CompanyName", "Wipro"); sleep(); httpSession.setAttribute("CompanyName", "Infosys"); sleep(); httpSession.removeAttribute("CompanyName"); } private void sleep() { try { Thread.sleep(10000); } catch( InterruptedException e ) { // TODO Auto-generated catch block e.printStackTrace(); } } public void destroy() { System.out.println("-----------------------------------------"); System.out.println(" destroy method is called in " + this.getClass().getName()); System.out.println("-----------------------------------------"); } }
MyHttpSessionAttributeListener.java
import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener { @Override public void attributeAdded( HttpSessionBindingEvent httpSessionBindingEvent ) { System.out.println("\n###################################\n"); System.out.println("attributeAdded method has been called in " + this.getClass().getName()); System.out.println("Newly added Attribute Name =" + httpSessionBindingEvent.getName() + ",value = " + httpSessionBindingEvent.getValue()); System.out.println("\n#####################################\n"); /* * if particular Attribute is added, based on that if you want to * perform any operation then you can do it here. */ } @Override public void attributeRemoved( HttpSessionBindingEvent httpSessionBindingEvent ) { System.out.println("\n###################################\n"); System.out.println("attributeRemoved method has been called in " + this.getClass().getName()); System.out.println("Removed Attribute Name =" + httpSessionBindingEvent.getName() + ",value = " + httpSessionBindingEvent.getValue()); System.out.println("\n###################################\n"); /* * if particular Attribute is Removed, based on that if you want to * perform any operation then you can do it here. */ } @Override public void attributeReplaced( HttpSessionBindingEvent httpSessionBindingEvent ) { System.out.println("\n###################################\n"); System.out.println("attributeReplaced method has been called in " + this.getClass().getName()); System.out.println("Replaced Attribute Name =" + httpSessionBindingEvent.getName() + ",value = " + httpSessionBindingEvent.getValue()); System.out.println("\n###################################\n"); /* * if particular Attribute is Replaced, based on that if you want to * perform any operation then you can do it here. */ } }
<?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>HttpSessionAttributeListenerDemo</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>HttpSessionAttributeTestServlet</servlet-name> <servlet-class>HttpSessionAttributeTestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HttpSessionAttributeTestServlet</servlet-name> <url-pattern>/listenerTest</url-pattern> </servlet-mapping> <listener> <listener-class>MyHttpSessionAttributeListener</listener-class> </listener> </web-app>
index.html
<html> <body> <a href="listenerTest">listenerTest</a> </body> </html>
JDK version :1.7.0_51
Tomcat version : 7.0.50
To Download HttpSessionAttributeListenerDemoApp Project Click the below link
https://sites.google.com/site/javaee4321/servlets/HttpSessionAttributeListenerDemoApp.zip?attredirects=0&d=1
See also:
thank you so much sir......
ReplyDelete