2

I am developing a jsf application.I have used a session listener in my application.Whenever i login then i am creating the sesssion & whenever i logout i am destroying the session.Now the problem is when i login then session create event is not triggred but when i logout session destroyed event is triggred just after that session started event is also triggred.I want to know how can i overcome this issue.

Session Listener

public class SessionListener implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("Session started");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("Session destroyed");

    }

}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>GameLottery</display-name>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Production</param-value>
  </context-param>
  <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>

    </welcome-file-list>
    <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>resources.application</param-value>
    </context-param>
    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>
    <listener>
        <listener-class>com.example.util.SessionListener</listener-class>
    </listener>


</web-app>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
techieGeek
  • 33
  • 3
  • The `sessionCreated()` method is invoked, when an HTTP session is created. The session creation had already happened long before you logged in that you must have observed on the console. There is no JSF-related thing here. – Tiny Jun 13 '15 at 12:57

1 Answers1

3

Whenever i login then i am creating the sesssion

There's a major conceptual misunderstanding here. It isn't you who is creating the session. It's the server who does that all by itself. You are merely putting the object representing the logged-in user in the session. The session can at this point already be created long before. The moment when that happens can be seen in server log, coming from the stdout of your listener.

If you want to have a global hook on whether the object representing the logged-in user is being added to the session, or is being removed from the session, then you should let the object implement HttpSessionBindingListener.

E.g.

public class User implements HttpSessionBindingListener {

    // ...

    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        // User is added to session (i.e. you're performing "login").
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        // User is removed from session (i.e. you're performing "logout").
    }

}

So, when you do getSessionMap().put("user", user), then the valueBound() will be invoked. And when you do getSessionMap().remove("user", user) or do invalidateSession(), or the session simply expires, then the valueUnbound() will be invoked.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @safwanHijazi: What is the reason for these questions? Please create real questions if you want answer – Kukeltje Jun 13 '15 at 11:53