• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

get fresh data from database using HttpSessionListener

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See you can do something like this:



Is this what you are looking out for?
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are modifying an object reference, so the changes to the same object reference should be reflected.
 
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic