Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get fresh data from database using HttpSessionListener

 
Gangadhar Reddy
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See you can do something like this:



Is this what you are looking out for?
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are modifying an object reference, so the changes to the same object reference should be reflected.
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just need to replace original customer attribute.
You can remove it using removeAttribute method and set new attribute.
 
Amol Nayak
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic