Hello, i wondered if anybody could offer comments on the design of a simple problem below:
Image a
java bean User. When someone regsiters on the site a new User object is created. A
servlet will process all details entered on the web page registration form using User's setter methods. Finally it will call the method addDatabase() to add the user to the databse. If someone logs in the a call will be made to the database that will populate the User bean object.
The user bean is set as a session attribute. On every page the header displays the user's name using ${userBean.name}.
Now imagine the user has changed their email. They enter their new email on a form which then goes to a new update servlet. The update servlet wants to set the user's new email from the form. So something like: user.setEmail("whatever")....although i guess this could be done from the
jsp page using the userBean setProperty. However, a method needs to be called on the user object.......updateUserDetails() that will connect to the database to perform the update. I don't want to use scripting in my JSP page in this instance.
The user object is first created in the servlet when the person registers or logs in like:
User usr = new User();
The bean is set in this serlvet for the whole session like: session.setAttribute("userBean", usr);
However, how can another serlvet get access to the usr object?
I thought something like:
User u = (User)session.getAttribute("userBean");
Then i thought i could do stuff like:
u.setEmail("Whatever");
u.updateUserDetail();
This doesn't work though. Am i missing some tricks here? Is my design poor for solving such a problem....what would be a more professional way of doing it? How can an object like User be passed into another servlet?