This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP -> Bean

 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still looking for the answer, thanks.
 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the "*" invoke all set... methods?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18020
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Subbu Aswathanarayan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all! I understand now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic