GeeCON Prague 2014*
The moose likes Servlets and the fly likes JSP -> Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "JSP -> Bean" Watch "JSP -> Bean" New topic
Author

JSP -> Bean

Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
In the JSP examples that comes with Tomacat, I have a question regarding the number guess JSP and bean. I will not give the code here since many of you already have it.
Question: What causes the "setGuess" method of the "NumberGuessBean" bean to get called when the form is submitted. I do not see the connection.
Thanks for the help. I am just getting started with JSP and Beans.
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
I don't have the code with me now. However, i looked at this example before. I think, setGuess method is called automatically when you have <jsp:setProperty> directive. If you use property="*" in your attribute, Tomcat will match the method to the property and invokes it automatically. So the setGuess method is called at the beginning when the page is loaded. When the action is not specified, i believe the submit button refer to the page itself. One way to find out when it is called, you can add one line of code in all the method(System.out.println("setXXX called") . Then observe the dos or shell console when you hit the page. Hope i am right.
[This message has been edited by SoonAnn Lim (edited July 09, 2001).]
[This message has been edited by SoonAnn Lim (edited July 09, 2001).]
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
The JSP uses the following:
<jsp:useBean id="numguess" class="num.NumberGuessBean" scope="session"/>
<jsp:setProperty name="numguess" property="*"/>
So you were correct about the "*".
However, I still do not see how that tag causes the "setGuess" method of the bean to be invoked.
Still need help.
Thanks.
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
setGuess may have one argument that correspond to the form field in your jsp page. When you hit submit, the form field is matched by the server with the property. Once it finds the match, the method is invoked (this is my interpretation only). Try use println statement in both your .jsp and bean, then you will know where and when it is called.
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
Here is the form:
<form method=get>
What's your guess? <input type=text name=guess>
<input type=submit value="Submit">
</form>

Here is the Bean method called:
public void setGuess(String guess) {...
The method is getting called when the submit is done. I verified that already.
From your explanation, this is being done because the method parameter name is "guess"?
Please let me know if I understand you correctly. That seems like a wierd way to have a bean method invoked. Wouldn't there be conflicts if the parameter name is being used?
Thanks again for your replies.
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
Still looking for the answer, thanks.
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
Does the "*" invoke all set... methods?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16101
    
  21

Don't make life more difficult than you need to! The useBean tag is designed, as its name implies to use Javabeans. Javabeans have properties, such as firstName, city - or guess. The useBean tagset understands that when you want to set a property, you must call the bean-mandated method setXXX and to get a property, call getXXX. where XXX is the property name. So to set the property "guess", the tag invokes "setGuess".


Customer surveys are for companies who didn't pay proper attention to begin with.
Subbu Aswathanarayan
Ranch Hand

Joined: Jun 22, 2001
Posts: 73
Originally posted by Rob Levo:
Does the "*" invoke all set... methods?

Yes the '*' invokes all the set methods whose name match exactly as the request parameter.Here is an excerpt from the book "Web Development with JavaServer Pages"
When multiple form field names map directly to Bean properties you can also use
the special wild card character �*� in the place of a property name. Using a wild
card indicates that you wish to set the value of any Bean property whose name corresponds
to the name of a request parameter. The names must match exactly as
there is no way to map parameters to properties with different names when the wild
card is used. For each property of the Bean, a matching request parameter is looked
for. Extra request parameters are ignored, though they can be accessed through
scriptlets and the implicit request object.

Subbu


Subbu
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
Thanks all! I understand now.
 
GeeCON Prague 2014
 
subject: JSP -> Bean