Updating Session Variables
Hello, first time posting here =)
I have a JSP app in wich i have some session variables i declare after you authenticate, wich contains several information (catalogs and access) used during the life of the session, and my question is...
How can i update these variables, in case that something changes in the catalogs or access information of that user gets updated without having to finish the session in the app and login again?
I've been said that it's not possible, but i needed to ask you guys anyway, so
any help would be really appreciated.
Thanks in advance!
Adrian
# 1
Here i m sending u a sample code i.e i used for update the session varibles
package servlets;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class SessionListener implements HttpSessionListener,HttpSessionAttributeListener{
public void sessionCreated(HttpSessionEvent se) {
System.out.println("Session Created ");
}
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session=se.getSession();
String username=(String)session.getAttribute("username");
if(username==null || username.length()==0) return;
File f=new File(Settings.getProperty("sessionDir"),"session-"+username+session.getId()+".xml");
if(f.exists()){
f.delete();
}
System.out.println("Session Destroyed "+(String)session.getAttribute("username"));
}
public void attributeAdded(HttpSessionBindingEvent be) {
HttpSession session=be.getSession();
String username=(String)session.getAttribute("username");
if(username==null || username.length()==0) return;
System.out.println("Session attributed added "+username);
}
public void attributeRemoved(HttpSessionBindingEvent be) {
HttpSession session=be.getSession();
String username=(String)session.getAttribute("username");
if(username==null || username.length()==0) return;
System.out.println("Session attributed removed "+username);
}
public void attributeReplaced(HttpSessionBindingEvent be) {
HttpSession session=be.getSession();
String username=(String)session.getAttribute("username");
System.out.println("Session attributed updated "+username);
}
}
In web.xml
<listener>
<listener-class>
servlets.SessionListener
</listener-class>
</listener>
# 2
Retrieve the attribute from the session, change it's value and put it back. Its done all the time.
//assume there is an Item object in session called item
//and you now need to update a property of that object
HttpSession session = request.getSession(false);
Item thinggy = (Item)session.getAttribute("item");
thinggy.setXXX(value);
In this case, you dont even need to put it back in the session. Sometimes you may have a new object. All you have to do then is bind the object to the session with the same name.
cheers,
ram.