suprapto45
asked on
Session - setMaxInactiveInterval
Hi,
Sorry to keep asking but this week, I am a kind of "stupid" hehe :).
Now, I have disabled my users to multiple login in my J2EE Web Application. I am using timestamp to check whether he or she has logged-in to my system. Well, everything runs ok until I found one bug :(.
First, I have set my session to be invalidated within 10 mins if there is no interaction of the client. A kind of automatic invalidation. I used setMaxInactiveInterval(600 ) to do this. Now, after 10 mins, the session is invalidated but how can I detect it and remove the timestamp of my database?
In conclusion, how can I detect that the session has reached its MaxInactiveInterval and I can remove the timestamp?
Thanx and Regards
Dave
Sorry to keep asking but this week, I am a kind of "stupid" hehe :).
Now, I have disabled my users to multiple login in my J2EE Web Application. I am using timestamp to check whether he or she has logged-in to my system. Well, everything runs ok until I found one bug :(.
First, I have set my session to be invalidated within 10 mins if there is no interaction of the client. A kind of automatic invalidation. I used setMaxInactiveInterval(600
In conclusion, how can I detect that the session has reached its MaxInactiveInterval and I can remove the timestamp?
Thanx and Regards
Dave
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well,
I think I know what am I doing wrong. I just read our last discussion again aozarov and I think that I did not explore that very well. Let me try to modify my Javabean to implement HttpSessionBindingListener .
Then what does it mean by these two methods?
public void valueBound(HttpSessionBind ingEvent arg0)
{
// TODO Auto-generated method stub
}
public void valueUnbound(HttpSessionBi ndingEvent arg0)
{
// TODO Auto-generated method stub
}
Regards
Dave
I think I know what am I doing wrong. I just read our last discussion again aozarov and I think that I did not explore that very well. Let me try to modify my Javabean to implement HttpSessionBindingListener
Then what does it mean by these two methods?
public void valueBound(HttpSessionBind
{
// TODO Auto-generated method stub
}
public void valueUnbound(HttpSessionBi
{
// TODO Auto-generated method stub
}
Regards
Dave
In any class that implements the HttpSesssionListener interface.
For example...
public class AppSessionListener implements HttpSessionListener
{
public void sessionCreated(HttpSession Event e) { }
public void sessionDestroyed(HttpSessi onEvent e)
{
// Logic here...
}
}
And in your web.xml, add:
<listener>
<listener-class>your.packa ge.AppSess ionListene r</listene r-class>
</listener>
For example...
public class AppSessionListener implements HttpSessionListener
{
public void sessionCreated(HttpSession
public void sessionDestroyed(HttpSessi
{
// Logic here...
}
}
And in your web.xml, add:
<listener>
<listener-class>your.packa
</listener>
valueBound -> This will be called onced that bean is added to the session
valueUnbound -> This will be called onced that bean is removed from the session (normally because of session timeout
based on max idle time). This is the method where you want to put your DB removal code.
valueUnbound -> This will be called onced that bean is removed from the session (normally because of session timeout
based on max idle time). This is the method where you want to put your DB removal code.
ASKER
Hi OBCT,
One question....
public void sessionDestroyed(HttpSessi onEvent e)
{
// Logic here...
}
What does e used for?
Regards
Dave
One question....
public void sessionDestroyed(HttpSessi
{
// Logic here...
}
What does e used for?
Regards
Dave
ASKER
Hi aozarov,
Must it be by bean that implements HttpSessionBindingListener ? Can it be any other class? Since the object that I bind to my session is only Javabean. it should not have any business logic to add, edit or delete.
Regards
Dave
Must it be by bean that implements HttpSessionBindingListener
Regards
Dave
The parameter e is the event that has just occured. It has a few small details about the event such as the event's source (which object fired the event) and a reference to the session that has been changed.
ASKER
Okay,
I got it. Thanks OBCT and auzarov, sorry auzarov that I did not explore well.
Another question. is it possible that in the
public void sessionDestroyed(HttpSessi onEvent e)
{
// Logic here...
}
or
public void valueUnbound(HttpSessionBi ndingEvent arg0)
{
// TODO Auto-generated method stub
}
for me to get some necessary data from my JavaBean in session just before it is invalidated? because to delete the timestamp, I need to have the userid and password to query DB.
Regards
Dave
I got it. Thanks OBCT and auzarov, sorry auzarov that I did not explore well.
Another question. is it possible that in the
public void sessionDestroyed(HttpSessi
{
// Logic here...
}
or
public void valueUnbound(HttpSessionBi
{
// TODO Auto-generated method stub
}
for me to get some necessary data from my JavaBean in session just before it is invalidated? because to delete the timestamp, I need to have the userid and password to query DB.
Regards
Dave
ASKER
Wait..I think that I manage to solve it....1 minute
Regards
Dave
Regards
Dave
ASKER
I found the solution. Thanks OBCT and auzarov.
Regards
Dave
Regards
Dave
For any other users that may have this problem in the future, would you mind posting your solution?
:-)
:-)
ASKER
Absolutely. I will post it in few hours time...lunch time
ASKER
Okay. First is that you need to modify your class to implement HttpSessionListener. In this case, my class is Struts action class.
public class LoginAction extends DispatchAction implements HttpSessionListener
Then you need to have two methods within the class that implement HttpSessionListener
/* (non-Javadoc)
* @see javax.servlet.http.HttpSes sionListen er#session Created(ja vax.servle t.http.Htt pSessionEv ent)
*/
public void sessionCreated(HttpSession Event arg0)
{
// TODO Auto-generated method stub
System.out.println("Execut ing login module - sessionCreated");
}
/* (non-Javadoc)
* @see javax.servlet.http.HttpSes sionListen er#session Destroyed( javax.serv let.http.H ttpSession Event)
*/
public void sessionDestroyed(HttpSessi onEvent arg0)
{
System.out.println("Execut ing login module - sessionDestroyed");
HttpSession session = arg0.getSession();
Login login = null;
login = (Login)session.getAttribut e(Constant .USER_KEY) ;
// TODO Auto-generated method stub
LoginDao logindao = LoginDao.getInstance();
try
{
logindao.removeTimeStamp(l ogin.getEm ail(), login.getPassword());
}
catch(Exception e)
{
e.printStackTrace();
}
}
Now see, you can get the session and etc for you to modify on.
regards
Dave
public class LoginAction extends DispatchAction implements HttpSessionListener
Then you need to have two methods within the class that implement HttpSessionListener
/* (non-Javadoc)
* @see javax.servlet.http.HttpSes
*/
public void sessionCreated(HttpSession
{
// TODO Auto-generated method stub
System.out.println("Execut
}
/* (non-Javadoc)
* @see javax.servlet.http.HttpSes
*/
public void sessionDestroyed(HttpSessi
{
System.out.println("Execut
HttpSession session = arg0.getSession();
Login login = null;
login = (Login)session.getAttribut
// TODO Auto-generated method stub
LoginDao logindao = LoginDao.getInstance();
try
{
logindao.removeTimeStamp(l
}
catch(Exception e)
{
e.printStackTrace();
}
}
Now see, you can get the session and etc for you to modify on.
regards
Dave
ASKER
I did not do that. I just bind the simple object (JavaBean) to my session. Any idea aozarov?
OBCT > mm...where should I declare the method sessionDestroyed(HttpSessi
Regards
Dave