• Post Reply Bookmark Topic Watch Topic
  • New Topic

error with JSP and Bean

 
trupti nigam
Ranch Hand
Posts: 627
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ALL,
I have a JSP page which accepts two inputs from the user.
I have a Bean which has setter and getter method for these values (variables).
The problem is that one of the setter method is not recognised. I mean when I use <jsp:setProperty..> and provide the static value to the variable it gives me error saying proper method for this variable is not defined in the bean.
But the other setter method for the second variable is being recognised.
what can be the error.
I am attaching here with code.
The setUpperLimit method is not being recognised.
package myServlets;
import java.util.*;
public class NumberBean {
int answer;
boolean success;
String hint;
int numGuesses;
int upperLimit;
public NumberBean() {
System.out.println("inside the constructor.");

//reset();
}

public void setUpperLimit(String upperLimit)
{
System.out.println("inside the setUppserLimit method.");

try {
int temp = Integer.parseInt(upperLimit);
this.upperLimit = temp;
System.out.println("The temp is: "+ temp);
System.out.println("The upperlimit is: "+ this.upperLimit);
}
catch (NumberFormatException e) {
//upperLimitNum = -1;
}
}
public void setGuess(String guess) {
System.out.println("inside the setGuess method.");
numGuesses++;
int g;
try {
g = Integer.parseInt(guess);
}
catch (NumberFormatException e) {
g = -1;
}
if (g == answer) {
success = true;
}
else if (g == -1) {
hint = "a number next time";
}
else if (g < answer) {
hint = "higher";
}
else if (g > answer) {
hint = "lower";
}
}
public boolean getSuccess() {
return success;
}
public String getHint() {
return "" + hint;
}
public int getNumGuesses() {
return numGuesses;
}

public int getUpperLimit() {
System.out.println("inside the getUpperLimit method.");
System.out.println("The upper limit is: "+upperLimit);
return upperLimit;
}
}
the .JSP file..
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>NumberGuessGame</title>
</head>
<%@ page import = "myServlets.NumberGuessBean" %>
<jsp:useBean id="numguess" class="myServlets.NumberGuessBean" scope="session"/>
<jsp:setProperty name="numguess" property="upperLimit" value="150"/>
<body>
Welcome to the Number Guess game.<p>
I'm thinking of a number between 1 and some upper limit number where you can
enter the upper limit.<p>
<form name = "Number Guess Bean" action= "numguess.jsp" >
Enter the upper limit: <input type="text" name= "upperLimit">
<input type=submit value="Submit">
</form>

</body>
</html>

thanks in advance..
trupti
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe your problem is with your method signature: public void setUpperLimit(String upperLimit)
upperLimit is defined as an int, so your method signature should look like this instead:
public void setUpperLimit(int upperLimit)
The beans api will automatically take care of converting a String (e.g. "150") to an int.
Hope that helps.
 
trupti nigam
Ranch Hand
Posts: 627
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Blake,
It helped..:-)
trupti
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!