• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sessions... confused  RSS feed

 
chris
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,
I'm developing an e-shop (comprised of servlets, html and jsp pages) and I've been stuck on sessions for more than 10 days going round in circles, always to come to a dead end. I wish some of you guys would shed some light on the matter...

My first questions is:
Can I keep track of more customers loggin-in from the same machine? For instance, Cust1 logs in and adds some items in his basket, closes browser and leaves the computer. Cust2 logs in, adds some items in his basket and closes browser. At this point, if Cust1 came back and logged in with his details, will I be able to retrieve his session (associated with a basket) ?

what i've been doing so far is create a session for any customer who logs in by setting the session's name to the customer's email address (E.G. johnsmith@mail.com 1F3382G2CN3680) the session code in the customerLogin servlets is the following:


HttpSession session = request.getSession(true);

//checks for an existing session for a specific user
ShoppingBasket basket = (ShoppingBasket)session.getAttribute(loginName);

if(basket == null){
basket = new ShoppingBasket();
session.setAttribute(loginName, basket);
System.out.println("NEW BASKET CREATED�);
}else{
System.out.println("BASKET ALREADY EXISTS�);
}

// print session contents
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
System.out.println("THE SESSION NAME IS: " + name + " it�s value is: " + value);
}

while my logic seemed to work at first, I'm now unable to track customers when they move to other pages, as I cannot assume only one customer could have logged in from the same computer (there might be more sessions open e.g. cust1-dd67s890f0d8, cust2-sd9f76dsf555asd etc.). The only solution would be to ask for login-name and password on every page... which is impracticable

Is there a way around this problem? Or am I missing something out?

Thanks
Chris
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could accomplish this with a combination of cookies and a persisted cart, usually an entity EJB but it could be a serialized object as well. If you used an EJB the primary key would be the sessionID which would be returned with the cookie every time the user comes back to your site
 
Sylven Yip
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.
1.in your example,cus1 left the computer but does not close browser.if he come back(of cause sent at least 1 request) in time(session maxInteval),his session in container did not destroyed,so you can access the session everywhere,his basket did not changed either.

Originally posted by Chris Repu:I'm now unable to track customers when they move to other pages

2.that is impossible....session is just used to do this(track session for ervey page),it must be something wrong in your application,please check it carefully.
[ October 01, 2007: Message edited by: Sylven Yip ]
[ October 01, 2007: Message edited by: Sylven Yip ]
 
chris
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sylven, thanks for your effort.
I might have been a bit unclear on the previous posting, the fact is that I'm rather new to programming and even explaing the problem is difficult.

Using the code above, sessions are created for each customer that logs in. They are available everywhere, I agree. My problem is identifying two or more customers who used the same computer to access the same e-shop (within the maxInterval).

let's say aCustomer logs in and adds a product to the basket, the following code would be required to either create a new session or use an existing one, where loginName would be the customer's email address.

//check for an existing session for a specific user
ShoppingBasket basket = (ShoppingBasket)session.getAttribute(loginName);
if(basket == null){
basket = new ShoppingBasket();
session.setAttribute(loginName, basket);
System.out.println("CustomerLogin: ----------------> NEW BASKET just created, the value basket is:" + basket.toString());
}else{
System.out.println("CustomerLogin: ----------------> BASKET EXISTS the value basket is:" + basket.toString());
}

The problem is, how do I know the loginName unless I ask for it again, everytime a customer adds a product?


Initially I thought there would be an easy way to "forward" say the loginName from page to page (is there?) so to leave a trail from the login page.... maybe I failed implementing the login page?... I'm totally lost any help would be immensely appreciated.


Originally posted by Sylven Yip:
hi.
1.in your example,cus1 left the computer but does not close browser.if he come back(of cause sent at least 1 request) in time(session maxInteval),his session in container did not destroyed,so you can access the session everywhere,his basket did not changed either.


2.that is impossible....session is just used to do this(track session for ervey page),it must be something wrong in your application,please check it carefully.
[ October 01, 2007: Message edited by: Sylven Yip ]

[ October 01, 2007: Message edited by: Sylven Yip ]
 
chris
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Ku:
You could accomplish this with a combination of cookies and a persisted cart, usually an entity EJB but it could be a serialized object as well. If you used an EJB the primary key would be the sessionID which would be returned with the cookie every time the user comes back to your site


Thanks Michael,
I would appreciate if somebody could post some links to examples of how this is done, or explain it in a little more detail as I cannot find any relevant examples in the web.

Last night I had another idea. Currently I'm using servlets for log-in, addItemToBasket etc. and redirecting the customer to .jsp pages using the following code.

request.getRequestDispatcher( jspPage ).forward(request,response);

Now, I'm thinking to develop the whole eshop just by using servlets that dynamically generate html code on the basis of the customer request. Although it may not be the most elegant solution, I think it's doable... at the same time I'll be able to to leave a trail of the customer...

Regards
Chris
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The EJB example would be too lengthly to post. You would have to read thru the J2EE tutorials to get an idea of how to do this.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EJBs are not necessary to implement server-side persistence and in fact, I'd argue against adopting them unless they are already in use.

You can persist the session info to a database or to a flat file are any other mechanisms you'd like. The point is, once a session goes out of scope, unless you've persisted its information, it's gone.
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your session can go away but your cart can stay. In the EJB example of which I spoke, the cart was saved to a DB using the session ID (the session that existed when It was created) as the primary key to the table. The cart entry itself is a BLOB. The primary key was saved in a cookie.You can implement the same logic without an EJB

When the user comes back, the cookie will let you access the cart by using the session ID from tje cookie as the primary key to look up the BLOB. Cleanup was done on the cart when the user logged out of the site.
 
chris
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody,
I may have solved the problem... actually i'm looking into it right now. I'll let you know how it goes as soon as I'm done.

The main problem I had was to keep track of the customer across the website (please bear in mind that I'm quite new to programming). I'm now thinking that I can get the session ID-value pair even from a JSP page (not sure I can do that though, I've posted this question on the JSP forum). If so, I could store the basket's content in a DB using the customers email address as the session identifier. This data can then be retrieved every time the customer logs in... Does it make any sense? I hope so...

Thanks for your help
Chris
 
Sylven Yip
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you mean you want to track multiple customer in the same PC and the same browser?

first let's think of how session implemented.
we track session by cookies or URLencoding,cookie that store the session have just one copy in one browser.
so,i think, if a new session create in the same browser,the original cookie represent the session will be coverd,so you can't track the original session anymore,beacuse it was destroyed.

so,may be we can consider about another way of session tracking---URL encoding(or you can call URL rewrite)by bring the username(or just jsessionid generate by web container) with URL.

please don't use database just for momently store,it's a bad habbit.it will limit your application's efficiency.database is always a precious resource.
[ October 01, 2007: Message edited by: Sylven Yip ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!