Welcome to Kode Java

Kode Java website provides beginners to Java programming some examples to use the Java API (Application Programming Interface) to develop applications. Learning from some examples will hopefully decrease the time required to learn Java.

In this website you will find a lot of examples which grouped by the Java API package. You can easily find a solution to your problem. To help you learn more we have also setup a discussion forums where everyone can share their Java knowledge.

Enjoy your study, come and visit the site regularly to find more and more examples of Java code.

--
I Wayan Saryada
Kode Java Webmaster

Java programmers recommend using Java Hosting for your scripts and JSP based websites to reach the full potential of your site and not experience any errors.

How do I capture session creation and removal events?

The Servlet specification define an HttpSessionListener interface that can be implemented if we want to listen to session creation and removal events.

package org.kodejava.example.servlet;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.Date;

public class MySessionListener implements HttpSessionListener {

    // Notification that a new session was created
    public void sessionCreated(HttpSessionEvent event) {
        HttpSession session = event.getSession();

        System.out.println("New session created  : " + session.getId());
        System.out.println("Session creation time: " + new Date(session.getCreationTime()));
    }

    // Notification that a session was invalidated
    public void sessionDestroyed(HttpSessionEvent event) {
        HttpSession session = event.getSession();

        System.out.println("Session destroyed  : " + session.getId());
    }
}

To make the listener works you need to configure in the the web.xml file. Below in a cofiguration example for our listener.

<listener>
    <listener-class>org.kodejava.example.servlet.MySessionListener</listener-class>
</listener>
Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats