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 Updating bean object through servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Updating bean object through servlet" Watch "Updating bean object through servlet" New topic
Author

Updating bean object through servlet

Marcus Hathaway
Ranch Hand

Joined: Jun 07, 2005
Posts: 89
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?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Marcus Hathaway
Ranch Hand

Joined: Jun 07, 2005
Posts: 89
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....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Marcus Hathaway
Ranch Hand

Joined: Jun 07, 2005
Posts: 89
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Updating bean object through servlet
 
Similar Threads
RequestDispatcher error
How to create a secure login system?
JavaBean between Servlet and JSP
How to inform the user that his session has expired
Help on HttpSessionListener