struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kris Schneider <>
Subject RE: Question regarding ActionForms.
Date Mon, 01 Dec 2003 17:10:03 GMT
One small tangent on why you might still want to call session.setAttribute. If
you ever want your app to work in a clustered/distributed environment, you
should always call session.setAttribute after modifying the state of a session
attribute. It may seem redundant:

Address address = ...;
Object customer = session.getAttribute(Constants.CUSTOMER_KEY);
PropertyUtils.setProperty(customer, "address", address);
session.setAttribute(Constants.CUSTOMER_KEY, customer);

but it's really the simplest way for a container to determine that an attribute
has been added or changed and should be replicated.

Quoting "Mainguy, Mike" <>:

> Welll,
>   Common misconception, but, technically all java calls are "by value".
> That is, there is no such thing in java as passing something "by
> reference".
> The hook is, the value passed in when something is an Object is actually a
> reference (aka a pointer) to the existing object.  
> While you can manipulate the thing that this points to, you cannot reassign
> it point to something else.  I got all bunged up about this also and I
> think
> it's a pretty important point that can easily be missed if you came into
> java from another language that allows "by reference" calls.
> By saying that objects are passed "by reference" folks are actually making
> things more complicated than they need to be...  For a really good
> explanation see
> worse is better 
> -----Original Message-----
> From: Larry Meadors [] 
> Sent: Monday, December 01, 2003 10:52 AM
> To:
> Subject: Re: Question regarding ActionForms.
> Welcome to java 101, formerly known as the struts-users mailing list.
> Object references are passed to method calls. While that reference cannot
> be
> changed, the object that it references can. On the other hand, if you
> create
> a new action form and assign it to the "form" parameter, nothing will
> happen
> because that reference cannot be changed.
> Larry
> >>> 12/01/03 8:42 AM >>>
> Hi,
> I am currently using Struts 1.0.2 and have notice something with regard to
> handling of ActionForms.
> Why is it, that in the "perform()" method of my Action I am able to modify
> the passed-in ActionForm and all modifications that I make to the
> ActionForm
> in my Action have effect on the ActionForm in the session, without the need
> to reset the modified form in the session. e.g.
> public ActionForward peform(ActionMapping mapping,
>                             ActionForm form,
>                             HttpServletRequest request,
>                             HttpServletResponse response,
>                             )throws IOException, ServletException
> {
>       MyForm myForm = (MyForm)form;
>       form.setMemberA("valA");
>       form.setMemberB("valB");
>       // why dont I have to do the following: session.setAttribute
> ("myFormName", myForm);
>       return "success";
> }
> I thought that since Java passes method arguments by value and not by
> reference, then I would have to reset my modifed copy back into the session
> for changes on the form to be visible in the form in the session ?
> Any insights on how this mechanism works would be greatly appreciated.
> Thanks,
> Sepand

Kris Schneider <>
D.O.Tech       <>

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message