It's not a secret anymore!
The moose likes Servlets and the fly likes Pissy jsp:usebean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Pissy jsp:usebean" Watch "Pissy jsp:usebean" New topic

Pissy jsp:usebean

Andrew Shafer
Ranch Hand

Joined: Jan 19, 2001
Posts: 338

First, this really pisses me off and I was hoping some people could check out the behavior on other JSP implementations.
I'm running Tomcat 3.2.1
This might be documented somewhere and I admit I haven't looked everywhere, but it seems so fundamental that it should be mentioned somewhere <b>IN BOLD</b>.
Ok, here is the meat of the matter.
You are cruising along with your little
<jsp:useBean id="CoolBean" class="myCoolClass" /> tag.
And you think you are pretty cool cause you been working hard on your useful little bean.
Now you want to process some form data.
So you GET a little data, rightfully named according to your bean's private fields and get/set methods.
Now you bust out with the
<jsp:setProperty name="CoolBean" property="*" /> thinking everything will be find and dandy.
But its not!
Now to the crux, using request.getParameter(), if a parameter is not present in the request, you get null and if it is, but has no value you get "". Seems reasonable, but. . .
<jsp:setProperty name="CoolBean" property="*" /> is setting everything to null unless if has a value. so ?param1=&. . . will give you a null value for param1 in your bean.
So you get a little curious cause things aren't working as you expected and you mess around and try a few things.
<jsp:setProperty name="CoolBean" property="param1"
value='<%= request.getParameter("param1") %>
Low and behold it works as expected, ok, that's at least a solution to the NullPointers I was getting, but it still pisses me off.
I thought maybe it was trying to set the values to null for some reason, but after I few experiments, I don't think it is calling the setXXX methods unless XXX has a value in the request.
Seems like a flaw in the design or in the implementation of
<jsp:setProperty name="SomeClass" property="*" />
Any comments or test on other containers?

Terence Doyle
Ranch Hand

Joined: May 30, 2001
Posts: 328
Sounds to me like your bean should have default values for your variables and a do-nothing filter in the setXXX methods if the new value is null.
That way a form can partially change the variables and still be useful.
Hope that helps,
Terry Doyle
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Terence Doyle (edited October 12, 2001).]

Raising Flares debut album 'Ignition' out now

Terry Doyle <br />SCPJ 1.4 , SCWCD , SCMAD(Beta)
Andrew Shafer
Ranch Hand

Joined: Jan 19, 2001
Posts: 338

There are tons of ways to work around it.

What if I want different behavior when the parameter is not present than if it has no value?
Why doesn't request.getParameter() give you null for param=?
I just want a little consistency, is that so wrong?

I agree. Here's the link:
subject: Pissy jsp:usebean
It's not a secret anymore!