aspose file tools*
The moose likes Servlets and the fly likes handling POST from unknown number of form elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "handling POST from unknown number of form elements" Watch "handling POST from unknown number of form elements" New topic
Author

handling POST from unknown number of form elements

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
I'm going to display a variable number of duplicate sections of a form based on the number of items that my user needs to configure, so when I go to handle the POST, I'm guessing that the easier way to parse all the request parameters is to use request.getParameterNames() and request.getParameterValues() and walk through everything manually. There's no way to use something like setProperty to auto-set all of a bean's values because really, the beans can't be set up to have a variable number of attributes, etc.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

If the names of the parameters are known in advance, then you can certainly set up your beans to have indexed properties.

If not, then a bean isn't a good choice at all.

You might also want to check out the request.getParameterMap() method as a way to obtain the entire suite of parameters.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Are beans with indexed properties available in standard J2EE, or do I have to use Struts or something like that? Thanks so much...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

JavaBeans are independent of J2EE. Grab a good book or tutorial on beans for the appropriate patterns for indexed properties.

I am assuming by your original post that multiple values can be supplied for the same parameter name?
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Whoops...no, I realized the difference in reading some more earlier...I'm just going to have to deal with an arbitrary number of pairs of parameters and values, not multiple values for each parameter.

And as for the indexed properties, I found this:
http://java.sun.com/docs/books/tutorial/javabeans/properties/indexed.html

Since I'm working with JSP/servlets and simple JavaBeans, I assume I don't need to worry about these listeners or anything that it's talking about since I don't need to be notified when any of the properties change. From what I see there, though, it seems like I don't have to get into any kinds of config files--all I have to do is declare a property that happens to be an array and then just slightly change the getter and setter methods from the norm for setProperty to work from a JSP.
[ October 20, 2004: Message edited by: Stephen Huey ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Right, but if you are not getting multiple values for a property then an indexed bean property is not the way to go.

Could you be a bit more explicit about the nature of your parameters? Are the possible parameter names known in advance? Can each have more than one value? And so on.
[ October 20, 2004: Message edited by: Bear Bibeault ]
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Ok, say the user is trying to ship an item, and on the first JSP they enter the recipient info as well as the number of items they're going to send. Then they go to the next page, and at this point my session bean is storing the info we already know. It also knows the number of items that need to have HTML generated for them on the following page. So there will be an arbitrary number of parts to the form that gets generated on that second JSP.

So if one of the parts of each one is ItemWeight, then when setProperty is called on this JSP, I want the String [] ItemWeight property in my bean to be automagically set for all the items. I understand that the bean needs to have a setter method setItemWeight(index, type) in order for this to work, but what's the correct name for each of my ItemWeight text inputs in the form so that setProperty knows how to properly handle them?

Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

so that setProperty knows how to properly handle them


Are you referring to the <jsp:setProperty> tag? Since you posted this in the Servlets forum I assumed you were handling this in Java code rather than a JSP.
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Sorry about that. Well, at the beginning of this thread, I was talking about doing all this on the servlet side with getParameterMap or something like that and then going in and setting the bean properties manually. But since I found out that indexed properties might work, I'm wondering if it's possible to make use of <jsp:setProperty>, and if so, then I'm assuming that the form elements need to be named in a certain way. Either that, or maybe they can all have the same name and setProperty just assigns them an index based on their order in the page...
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
This constraint doesn't seem to make sense, but supposedly the indexed property must have a certain Object type in order for this to work:

http://java.sun.com/products/jsp/tags/syntaxref.fm13.html#17460

"You can also use <jsp:setProperty> to set the value of an indexed property in a Bean. The indexed property must have one of the types shown in TABLE 1-1..."

None of the possible types are String, which I would need!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

I'm wondering if it's possible to make use of <jsp:setProperty>


My opinion? Mistake. Big. Huge.

Never send a JSP to do a Servlet's job. You will be banging your head against a generalized mechanism envisioned at the dawn of JSP-hood before appropriate patterns of web application structure were put into place.

Submit your form to a servlet with parameter names that make the most sense to your application and deal with them in an appropriate structure. If a bean doesn't suit your purpose, use something else. If it is truly a generalized collection of name/value pairs whose keys are not known in advance, you can't beat a Map.

Bear in mind that with request parameters that use the same name there is no guarentee of order of retrieval. So if order is important to you, you may need to think carefully about your parameter names.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: handling POST from unknown number of form elements