• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Updating bean object through servlet

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marcus Hathaway:

This doesn't work though.



What do you mean when you say "it doesn't work"?
What happens?
Does it throw an exception?
 
Marcus Hathaway
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ben,

This doesn't work because i get a Null Pointer Exception if i try to do something from the newly created User object. In the example code below the exception would occur in u.setEmail("Whatever");

User u = (User)session.getAttribute("userBean");
u.setEmail("Whatever");

Basically what i'm trying to ascertain is if the above code should ever work? If the above way is a common method for refering to an object from another servlet then i'm on the right tracks and will start figuring out alternatives to the problems i am facing. But for instance, i was hoping someone might say......well you can't refer to an object like that...or that�s a stupid way of doing it because of....you should refer to it like...blah blah blah....
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code will NPE when there is no User bean in the session. Otherwise, it will not. You'll need to track down why there's no bean in the session if you expceted there to be one.

With regards to the rest of your logic, you seem to be trying to do too much in the JSPs. Think of the JSPs as pure view components that don't do anything but show you the results of processing performed elsewhere.

In an upcoming article in the JavaRacnh Journal I will be addressing patterns for web application structure, but in the mean-time you might want to research the "Model 2" pattern for web apps.
 
Marcus Hathaway
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi bear thanks for your reply. I look forward to the upcoming article. I am actually trying to separate everything though and this is whats giving me the problems. For instance if i wanted to just update the object and database direct from the JSP this would be a lot easier as all i would need to do is use a scriplet and the beans set property.

Is it possible the line:

User u = (User)session.getAttribute("userBean");

is not working in the servlet because...well how does the servlet know what attribute userBean is? I'm convinced this bean is working and has a session scope....as various JSP's will use properties of the bean to display the user name on every page.

<jsp:useBean id="userBean" class="myPackage.User" scope="session"/> is the only line in the JSP page about this bean (other than to display the user's name).

Is the servlet line:
User u = (User)session.getAttribute("userBean");
enough to be able to access this bean?

Should it matter where that useBean line is placed within the JSP (i'm using at the top of the page and not in the form section where the submit procedure takes place).

I have been trying loads now for two days and still no joy

I'm sure there must be a way of passing the bean object from a JSP to the serlvet without having to script within the JSP?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marcus Hathaway:
I look forward to the upcoming article.



Now available.


User u = (User)session.getAttribute("userBean");

is not working in the servlet because...well how does the servlet know what attribute userBean is?



It looks it up by name. This is the same mechanism that the JSP will use under the covers when using <jsp:useBean>. If you are familiar with java.util.Map, you can think of the scoped variables as entries in the Map keyed by their name.


I'm convinced this bean is working and has a session scope



Unless you are having a weird problem with your session, the same scoped variables should be available to JSPs and to the servlets.

Dumb, but necessary, question: the JSPs and the servlet are in the same web application, right?
[ March 22, 2006: Message edited by: Bear Bibeault ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic