| Author |
get fresh data from database using HttpSessionListener
|
Gangadhar Reddy
Greenhorn
Joined: Aug 13, 2007
Posts: 25
|
|
[1]I have a class called 'Customer' which implements javax.servlet.http.HttpSessionBindingListener. [2]I have a simple jsp where an end user has to enter his name and then he has to submit to a servlet. [3]Inside this servlet I have a code as follows doGet( blah blah.....) { String userName = request.getParameter("UserName"); Customer cust = new Customer(username); //statement --->1 session.setAttribute("CustomerDetails", cust); /* When the container finds this statement, its going to invoke following code valueBound(HttpSessionBindingEvent event) { /* Code that connects to database and gets the data that is associated to 'userName' and its corresponding 'address' and pass those two arguments to 'Customer' constructor. Now we got fresh 'Customer' type object and we are going to associate this newly created object to existing reference namely 'cust' */ } Now my question, as you no that object 'cust' has ALREADY BEEN SET as attribute, in statement --->1, then are we sure that we set attribute with latest data from database? [ September 17, 2007: Message edited by: Bear Bibeault ]
|
 |
Amol Nayak
Ranch Hand
Joined: Oct 26, 2006
Posts: 218
|
|
Can you paste the code you have written in valueBound? I dont see the need to instantiate a Customer object and assigning the newly created object to the cust reference will not work at all. You need to modify the object that is already bound to session.
|
 |
Gangadhar Reddy
Greenhorn
Joined: Aug 13, 2007
Posts: 25
|
|
Thank you for writing in. The code what you have asked for is not available with me. You have mentioned that we need to modify the object that is already bound to session. Could you please tell me how can we modify that object it HAS ALREADY BEEN BOUND TO
|
 |
Amol Nayak
Ranch Hand
Joined: Oct 26, 2006
Posts: 218
|
|
See you can do something like this: Is this what you are looking out for?
|
 |
Bosun Bello
Ranch Hand
Joined: Nov 06, 2000
Posts: 1506
|
|
|
You are modifying an object reference, so the changes to the same object reference should be reflected.
|
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
|
 |
sudhir nim
Ranch Hand
Joined: Aug 29, 2007
Posts: 212
|
|
If you just need to replace original customer attribute. You can remove it using removeAttribute method and set new attribute.
|
[Servlet tutorial] [Servlet 3.0 Cook Book]
|
 |
Amol Nayak
Ranch Hand
Joined: Oct 26, 2006
Posts: 218
|
|
Sudhir wrote: If you just need to replace original customer attribute. You can remove it using removeAttribute method and set new attribute
This call valueBound and valueUnbound again. Be careful where you remove and add attribute.
|
 |
 |
|
|
subject: get fresh data from database using HttpSessionListener
|
|
|