This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes Bean values won't change on form submit 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 » JSP
Bookmark "Bean values won Watch "Bean values won New topic
Author

Bean values won't change on form submit

Andre Solberg
Greenhorn

Joined: Sep 16, 2010
Posts: 13
Hi,

When a user is logged in, a user object is placed in session. This way I can access all attributes. I want to have a form where the user can the attributes.

The form:


The servlet that is called:

The current values display in the form. After changing any of the fields and pressing submit, I expect to see the new values from the servlet, but it just prints out the old values. I have no idea why. I obviously have setters in the User class. I do the same thing in user registration, which works perfectly. The only difference is that in the registration form, I instantiate the object with a <jsp:useBean>, but it should be working when accessing an existing object from the session too, right??

Maybe I'm wrong, or missing something obvious, it should be easy to get this working, but I've been stuck here for too long....

Any suggestions?
Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60773
    
  65

Andre Solberg wrote:After changing any of the fields and pressing submit, I expect to see the new values from the servlet

And just why would you expect that? If you haven't done anything to update the properties of the bean, why would you expect them to be updated?

Grab the info from the request and update the bean properties.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60773
    
  65

P.S. Scriptlets in the JSP? In 2010?
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

setAttribute method of HttpSession is never used in the code, means User object in the session scope not changed/updated.
Andre Solberg wrote:The only difference is that in the registration form, I instantiate the object with a <jsp:useBean>
User object in the session scope must have changed/updated, may be by using jsp:setproperty tag(i guess the scope attribute in jsp useBean tag must have the value as session).

Difference between these two is, first one takes a copy of User object from session and updates copied User object 's attribute and updates it into session; Second one directly uses the User object from session.

I am little late


A.A.Anbarasu
Andre Solberg
Greenhorn

Joined: Sep 16, 2010
Posts: 13
Oh ok, I see I've misunderstood........................

I handle the registration in a separate jsp instead of servlet. I forgot about the <jsp:setProperty name="user" property="*"/> within the <jsp:useBean>.
I guess I just thought there was some magic that automatically filled the bean with the values from the fields as long as the setters where right ...

But when doing this in a servlet, does that mean I have to manually fill in each and every property from the request and put in in the bean? I can't put the <jsp:setProperty> in before I submit or in the servlet? If so, then damn...

Thanks for helping!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60773
    
  65

Check out Jakarta Commons BeanUtils.populate().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bean values won't change on form submit
 
Similar Threads
Extracting data from a bean
How to get pair values using getParameterNames?
strange experience with trying to invalidate a session
problem with prepared statement
User filter