File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes request.setattribute vs. form.set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "request.setattribute vs. form.set" Watch "request.setattribute vs. form.set" New topic
Author

request.setattribute vs. form.set

Matthew Anderson
Greenhorn

Joined: Jul 03, 2007
Posts: 9
If I have a form bean, named "NameForm" and a form property/text field named "text1". I can use nameForm.set("text1", somestring) in the action servlet to set the property of the text field. Isn't request.setattribute the same, or is there a difference?

/C
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Code Exception,

I've already communicated with you that the name you are currently displaying does not meet the guidelines of the JavaRanch Naming Policy. In particular, the name you display must be a first and last name, and must not be obviously fictitious. Please change the Publicly Displayed Name in your user profile so that it meets the guidelines.

Compliance with this policy is not optional and failure to update your profile could result in its being deleted.
[ July 17, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Matthew Anderson
Greenhorn

Joined: Jul 03, 2007
Posts: 9
Name change: done.

Can anyone help me with my question above?

Thanks,
Matt
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Matthew,

Thanks for taking the time to change your profile.

Originally posted by Code Exception:
I can use nameForm.set("text1", somestring) in the action servlet to set the property of the text field. Isn't request.setattribute the same, or is there a difference?

No, they're not at all the same. When you use request.setAttribute, you're putting a reference to an object in the HTTPServletRequest object, which is a completely different thing than setting a property on a form bean. You also retrieve the result differently.

Example 1: Property in DynaActionForm
in Action class
nameForm.set("text1", somestring);
In JSP:
<bean:write name="nameForm" property="text1" />

Example 2: Attribute of Request
in Action class:
request.setAttribute("text1", somestring);
in JSP:
<bean:write name="text1" scope="request" />
Matthew Anderson
Greenhorn

Joined: Jul 03, 2007
Posts: 9
Thanks for your detailed reply, that was exactly what I needed.

/Matt
Matthew Anderson
Greenhorn

Joined: Jul 03, 2007
Posts: 9
After looking through some code, I realized it's not crystal clear yet. The "object" in request.setAttribute(...) is created in the action class upon request and is there given an attribut/content, which in turn is used in the jsp? Why can't this be done with a DynaFormBean?
It's a bit difficult to know the differences and usages between property, attribute, parameter and name.

Hope you can help me with this,
Matt
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Matthew Anderson:
Why can't this be done with a DynaFormBean?

The point is that you can use either method in your Action class to create data and put it in a place so that the JSP will have access to it. While either method works, in a Struts application it's generally preferable to put data in the ActionForm bean rather than as an attribute of the request. It just keeps things more organized. Besides, some Struts custom tags only work with an ActionForm bean property and not with a request attribute.
Annie Jones
Greenhorn

Joined: Nov 05, 2007
Posts: 10
Maybe I'm not understanding, but how is putting a reference in the HTTPServletRequest object a completely different thing than setting a property on a form bean? Doesn't the form bean end up in the HTTPServletRequest object too?





quote:
--------------------------------------------------------------------------------
Originally posted by Code Exception:
I can use nameForm.set("text1", somestring) in the action servlet to set the property of the text field. Isn't request.setattribute the same, or is there a difference?
--------------------------------------------------------------------------------


No, they're not at all the same. When you use request.setAttribute, you're putting a reference to an object in the HTTPServletRequest object, which is a completely different thing than setting a property on a form bean. You also retrieve the result differently.

Example 1: Property in DynaActionForm
in Action class
nameForm.set("text1", somestring);
In JSP:
<bean:write name="nameForm" property="text1" />

Example 2: Attribute of Request
in Action class:
request.setAttribute("text1", somestring);
in JSP:
<bean:write name="text1" scope="request" />

Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Annie Jones:
Doesn't the form bean end up in the HTTPServletRequest object too?

Not necessarily. The form bean will be in either request scope or session scope, depending on how you configured the Action in the struts-config.xml file. While it is true that the form bean itself is in some scope and therefore is similar to placing some other object in request or session scope, the Struts API calls are different in each case, as I already explained in my previous post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: request.setattribute vs. form.set