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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
HttpSessionListener 
 

Before going into the details of the SessionListener we should firstly know about the sessions.

 

HttpSessionListener

                         

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 MySessionListener
implements HttpSessionListener {
public MySessionListener() {
}
public void sessionCreated(HttpSessionEvent sessionEvent) {
// Get the session 
HttpSession session = sessionEvent.getSession();
try {
System.out.println("Session created: "+session);
session.setAttribute("foo","bar");
} 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 name is: " + session.getAttribute("foo"));
}
}

 

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

public class ServletSessionListener 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("foo");
pw.println("The name is " + str);
}
}

The output of the program is given below:

 

Download this example.

                         

» View all related tutorials
Related Tags: c http com ide session list stl server orm resources process form binding transactions https object io connection processes make

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

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

catch (Exception e) {
System.out.println("Error in setting session attribute: "
}+ e.getMessage());

it should be:

catch (Exception e) {
System.out.println("Error in setting session attribute: "
+ e.getMessage());
}

Posted by Saurabh K. Srivastava on Tuesday, 03.18.08 @ 18:11pm | #53196

sir/mam
this side is realy very helpful for the java persion i m very thankful of u providing
a very easy and effective material.

Posted by pankaj on Tuesday, 07.24.07 @ 17:53pm | #21837

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.