File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes get fresh data from database using HttpSessionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "get fresh data from database using HttpSessionListener" Watch "get fresh data from database using HttpSessionListener" New topic
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: 1510
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get fresh data from database using HttpSessionListener