Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Session Object values to Java Bean

 
Oliver Moore
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating an example online store, where the shopping cart is stored as a session object, and item beans are added to it each time a user adds an item to his cart.
If I want to access the session in a jsp, I can do

and then do operations such as

This is not a problem. However, I want to write these values to a database when the order is submitted but I don't know how to access the session object in a bean. I think what I want is the equivelent of "jsp:useBean", but am unsure how to do this.
I am using struts, so I need to access the session object, and iterate through each bean within it, adding their data to the DB.
Hope that's clear Any help would be much apprieciated
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your struts action will have a HttpServletRequest object, from which you can getSession(), this returns an HttpSession object, and you'll find all your customers details there.
PS next time you know what the JSP equivalent code is, and you need an answer ASAP, then simply check the JSP's code after it is translated into a servlet, you'll find the complete java source in there, not the best way to do things but hey, it will get you instant relief right
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that a javabean does not have implicit access to the servlet/jsp container i.e. session scope, request scope objects. So you may have to pass a session reference to the bean inorder to gain access to your session object.
But, my suggestion would be that you pass your cart

com.ops.entity.Order_Form

object to a worker bean which will act as a data access object to your database. The DAO will encapsulate your database functionality.
One such method would be:

The load method will iterate through each item saving it in the database.
Hope this helps.
cj
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!