• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with setProperty

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic