Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
HttpSessionListener example 
 

Before going into the details of the SessionListener we should firstly know about the sessions. As we know that Http protocol is a "stateless" protocol. The term stateless means that it can't persist the information. It can't remember the previous transac

 

HttpSessionListener example

                         

Before going into the details of the SessionListener we should firstly know about the sessions. As we know that Http protocol is a "stateless" protocol. The term stateless means that it can't persist the information. It can't remember the previous transactions. Whenever a client makes a request for any resources to the server, the server receives the request and processes the request and sends back the response. After sending the response the server closes the connection and forgets about the previous requests. Whenever a client sends any request to the server, the server treats each request as a new request. To remove this we have been provided the facility of the session. In session tracking whenever a client sends a request to the server then server creates a unique id for that request and sends back the unique id to the client along with the response object, now whenever a client sends a request to the server it also sends a unique id with it so that the server can know from where the request is coming. 

Listeners listen the events. Whenever any event occurs it is listened by the listener. The listener will be controller by the web servers. 

HttpSessionListener is an interface which extends java.util.EventListener class. The main purpose of this listener is to notify whenever there is a change in the list of active sessions in a web application 

This interface has  two methods:

  1. sessionCreated(HttpSessionEvent event): It will notify when the session is created.
  2. sessionDestroyed(HttpSessionEvent event): It will notify when the session gets invalidated.

In the above methods we can see that we have used HttpSessionEvent as the parameter of both the methods. This class has only one method getSession() which returns the current session.

The code of the program is given below:

Make the entry of this file in the deployment descriptor file that is web.xml

import javax.servlet.*;
import javax.servlet.http.*;

public class ListenerSession
implements HttpSessionListener {
public ListenerSession() {
}
public void sessionCreated(HttpSessionEvent sessionEvent) {
// Get the session that was created
HttpSession session = sessionEvent.getSession();
// Store something in the session, and log a message
try {
System.out.println("Session created: "+session);
session.setAttribute("dog", "labrador");
session.setAttribute("name", "Diana");
} catch (Exception e) {
System.out.println("Error in setting session attribute: " + 
e.getMessage());
}
}
public void sessionDestroyed(HttpSessionEvent sessionEvent) {
// Get the session that was invalidated
HttpSession session = sessionEvent.getSession();
// Log a message
System.out.println("Session invalidated: "+session);
System.out.println("The breed of the dog is: " + session.getAttribute("dog"));
System.out.println("The name of the dog is : " + session.getAttribute("name"));
}
}

 

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ServletListenerSession extends HttpServlet{
public void doGet(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
HttpSession session= request.getSession();
String str = (String)session.getAttribute("dog");
String dogName = (String)session.getAttribute("name");
pw.println("The breed of the dog is " + str);
pw.println("The name of the dog is " + dogName);
}
}

The output of the example is given below:

Download this example.

                         

» View all related tutorials
Related Tags: c database error data input servlet user screen display name this message ai tab check if example with exists to

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

I tried this example but faced a problem, I'm not getting the values session.getAttribute("dog") as null in SessionDestroyed method after execution.

System.out.println("The breed of the dog is: " + session.getAttribute("dog")); // shows the value.

System.out.println("The name of the dog is : " + session.getAttribute("name"));
}

Posted by Rohini on Tuesday, 05.6.08 @ 15:47pm | #58625

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.