aspose file tools*
The moose likes JSP and the fly likes Jrun, jsp:setProperty, null string array elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Jrun, jsp:setProperty, null string array elements" Watch "Jrun, jsp:setProperty, null string array elements" New topic
Author

Jrun, jsp:setProperty, null string array elements

Ralph Kehmeier
Greenhorn

Joined: Apr 16, 2005
Posts: 10
Hello All;

I have a JSP web page which runs fine with Tomcat, but not with Jrun 4.0. The web page uses �jsp:setProperty� to read a bunch of textboxes all having the same name into a Javabean, where they appear as a string array. The problem occuring with Jrun is that if any of the textboxes on the web page are left empty, values from textboxes further down on the page are shifted into the empty boxes. The Javabean under Jrun doesn�t seem to recognize null string array elements. This problem also seems to occur more consistently with the Netscape browser than with IE, and more consistently with method=post than with method=get, but some users have seen the problem using IE and the JSP having method=get. Any ideas? Thanks.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Don't rely on this and give different names to each textbox.


[My Blog]
All roads lead to JavaRanch
Ralph Kehmeier
Greenhorn

Joined: Apr 16, 2005
Posts: 10
Let�s see, how to put this nicely...
Of course a solution would be to give different names to each textbox. Another solution would be to get out of the computer business altogether. But neither answer meets my needs. I have a JSP page with a variable and possibly large number of similar text boxes, so it�s impractical, inelegant, and maybe not possible to give different names to each textbox. Beyond that, your suggestion doesn�t answer my question, so I don�t learn anything.

General questions:
1) Is it supposed to be all right to use �jsp:setProperty� to read an array into a Javabean? If so, is there a right way and a wrong way to do it?
2) Why does Jrun behave differently in this case than Tomcat?
3) Is there any way to make Jrun behave like Tomcat, that is, not ignore empty string array elements?
Thank you in advance for the help.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
The issue is that there is no guaruntee on the order of request parameters being sent by the browser or received by the server. You can't assume just because they are in a nice column on your page going from top to bottom, that they will be sent/received in that same order.

It might happen that the parameters get sent in that fashion, but unfortunately you can't count on it.
It depends on
- the browser sending the parameters in your expected order.
- the server receiving them all in order, and then putting them into the parameterValues array in that same order.

That would explain why you get different results using get/post and in different browsers, and different servers.

On top of that, the <jsp:setProperty> tag is documented that it ignores null and empty string, and treats it the same as the property not being there.

Sorry, but the only way to guaruntee that the order of the parameters being sent from the page, is as other posters have indicated, to be able to individually identify each one by name.
However all is not lost. There are people who have travelled this path before, and come up with solutions.

One common solution is the jakarta commons BeanUtils package.
http://jakarta.apache.org/commons/beanutils/.
It provides methods to populate beans from maps. It is used extensively in Struts to do the underlying request parameter -> java object conversions.

Basically if you can call your fields
name[0], name[1], name[2]... name[n]
You can then use the BeanUtils.populate(bean, request.getParameterMap()) method to populate the array in your bean.


Sorry, if its not what you wanted to hear. But its the truth as far as I know it.

Cheers,
evnafets
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Straight from the horse's mouth (JSP 2.0 Spec):

If the param is not set in the Request object, or if it has the
value of "", the jsp:setProperty action has no effect (a noop).


Note, that it does not specifically say what should happen to values inside the range of an indexed property.

So both behaviors could possibly be construed as correct according to the Spec.

Doesn't help, does it?

So what are you specifically doing that relies on the behavior? Will Stefan's "pseudo-indexed parameter" suggestion do it for you?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Beyond that, your suggestion doesn�t answer my question, so I don�t learn anything.


[DELETED nasty comments]

I'm suggesting you to be nice next time,
and hope you've learnt something
[ March 01, 2006: Message edited by: Satou kurinosuke ]
Ralph Kehmeier
Greenhorn

Joined: Apr 16, 2005
Posts: 10
Thank you for your comments, Messrs. Evans and Bibeault. Concerning what the JSP 2.0 Specification says about a parameter with value of "", I think the horse is referring to a simple parameter, as opposed to an indexed parameter. The specification is apparently vague concerning indexed parameters with empty elements, an impression shared by Mr. Isaacs at http://www.mail-archive.com/tomcat-dev@jakarta.apache.org/msg01128.html.

Here�s how matters lay out, as I see it:
1) The JavaServer Pages Specification, Version 2.0, provides for indexed parameters: �Simple and indexed properties can be set using jsp:setProperty.�
3) �When assigning values to indexed properties the value must be an array�. (ibid.)
4) There is no way to guarantee that the user submit an indexed parameter (string array) without empty elements.
5) If empty array elements are dropped by the browser or server, then elements of the indexed parameter (string array) shift around randomly.
6) Of what possible use to anyone is an array with elements that shift around randomly??

It may be that the BeanUtils.populate method is a solution to my problem. I don�t know anything about that stuff, and hate to waste the time learning it unless absolutely necessary. In any case, it sets off my KISS (Keep It Simple, Stupid) alarm like crazy.

Back to my original question: my JSP page runs fine with Tomcat, but not with Jrun 4.0. Given that Tomcat is the Reference Implementation for the JavaServer Pages technology (http://tomcat.apache.org/), I conclude that JRun is behaving badly. Is there any way to make JRun behave nicely in this regard?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

I have had no dealings with JRun (thankfully) since the late 90's, so I couldn't tell you if you could change its behavior. I would highly doubt it, though.

I also understand your reluctance to take on 3rd-party libraries to solve what should be a simple problem.

But the situation is that the behaviour of * in this area appears to be only vaguely addressed by the spec, and has already proven to be highly container-dependent. So that's what you've got to deal with.

Personally, I've never used the * mechanism in all of the seven years that I've been working with JSPs. In fact, I never even do this type of procesing in a JSP at all -- I always write page controller servlets to handle the data prior to shipping control off to a JSP to render the view. That gives me complete control over how the data is handled.

My advice would be to either explore that route, or to come up with your own means to collect your data in a manner in which you are guarenteed to get it in the format that you desire.
Ralph Kehmeier
Greenhorn

Joined: Apr 16, 2005
Posts: 10
Sorry I haven�t been clear. I�m not using the * mechanism, never have, probably never will:
<jsp:setProperty name="beanName" property="*" />

Rather, I�m using something like:
<jsp:setProperty name="beanName" property="properyName" param="parameterName" />

and then multiple elements like (irrelevant parts eliminated):
<input type=text name="parameterName">
<input type=text name="parameterName" value="11">
<input type=text name="parameterName" value="22">
<input type=text name="parameterName">

...which, when submitted (with method=set), sends something like:
...?parameterName=¶meterName=11¶meterName=22¶meterName=

which should be read by the Javabean as a string array. The result, however, is that somewhere along the line, the empty element [0] is dropped, and elements [1] and [2] are shifted into positions [0] and [1]. This is clearly untenable.

If it�s not possible to make this work with JRun, then the question is: what is the simplest way, or even any way that works (with JRun), using a JSP, to enter an indexed property, a.k.a. string array, into a Javabean?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Originally posted by Ralph Kehmeier:
Rather, I�m using something like: ... jsp:setProperty ...


Same difference. I never use that either.

As I said, I never use a JSP as a data processor.

what is the simplest way, or even any way that works (with JRun), using a JSP, to enter an indexed property, a.k.a. string array, into a Javabean?


If you insist on doing this in a JSP, you'll probably need to write your own code to set the bean properties. So you either need to resort to scriptlet code on the page, munge something together with JSTL <c:set>, or write your own custom action to do this.

Also be aware that the order in which parameters are delivered is not guarenteed to be in any particular order. So if your code is relying on the order of the parameters, you could be surprised at any point in the future when the order changes.
[ March 04, 2006: Message edited by: Bear Bibeault ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jrun, jsp:setProperty, null string array elements
 
Similar Threads
add and remove method
problem using javabean in JSP
JSP w/ Date and Calendar
Java Bean default value for setProperty
using jsp:useBean