Monday 8 September 2014

HttpSessionIdListener using Deployment Descriptor

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionIdListener;

public class SessionIdLisn implements HttpSessionIdListener {
    @Override
    public void sessionIdChanged(HttpSessionEvent event, String oldSessionId) {
        System.out.println("Old Session id is " + oldSessionId);
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
    <listener>
        <description>HttpSessionListener</description>
        <listener-class>SessionIdLisn</listener-class>
    </listener>
</web-app>

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet(urlPatterns = {"/SampleApp"})
public class SampleApp extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException{
        HttpSession mySession = req.getSession();
        
        System.out.println("Changing Session id");
        req.changeSessionId();
        
        System.out.println("Changing Session id");
        req.changeSessionId();
        
        System.out.println("Changing Session id");
        req.changeSessionId();
    }
}

Run ' SampleApp' and you can observe the below messages in server console.

Info:   Changing Session id
Info:   Old Session id is a081f69236b440d54254fa841679
Info:   Changing Session id
Info:   Old Session id is a0860d41d669c375663dd408a1f6
Info:   Changing Session id
Info:   Old Session id is a0860d5aef2d007fb478f05a39e8





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment