Monday 8 September 2014

HttpSessionIdListener using WebListener

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

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

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 a0aa37d5d8c230188f7fa6fd092b
Info:   Changing Session id
Info:   Old Session id is a0aa383754166094164a4e01b63b
Info:   Changing Session id
Info:   Old Session id is a0aa38b1b00dcdc3029c2e8188c8



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment