• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with setProperty

 
Raghuraman Muthuswamy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am trying use a bean which has a setProperty. In the setProperty i have a If loop. But when i run the JSP page the condition is not satisfying. I have a text box to enter Speed. That page is posted to a JSP page where i receive the text box value. If the value is negative i have to execute the bean if condition. But its not working. will give you the code. Pls correct the error and explain me what i did wrong.

Bean Code:

package ecom;

public class SC
{
private int speed;
private String sp;

public int getSpeed()
{
return speed;
}

public void setSpeed(int s)
{
if (speed<0)
{
sp = "Speed Can't be Negative";
}
else
{
speed = s;
}
}


}

JSP Code:

<body>
<%
String var = request.getParameter("speed");
int var1 = Integer.parseInt(var);
%>
<h1>Speed of the Car is <%= var%></h1><br>
<jsp:useBean id="speed" class="ecom.SC" />
<jsp:setProperty name="speed" property="speed" value = '<%= var1%>' />
<jsp:getProperty name="speed" property="speed"/>
<%
System.out.println("Speed = "+var1);
%>

</body>

Your help is very much appreciated.

Thanks in advance,
Raghu
 
ss_java
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your method "getSpeed" returns an int and you are storing error message in a string.that is tht reason you are not able to retrieve the message.
 
Sarath Mohan
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish to add this too. The setSpeed method receives an integer but the parameter recieved by the jsp page will be a string. So setSpeed will not execute.

It will take speed=0; always since it is initialised as zero.

One solution

set and getSpeed should recieve speed as String. Then do proper conversion and do the validation
Cheers
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"ss_java",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic