Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getProperty not showing up any output 1

Status
Not open for further replies.

oaklandar

Technical User
Feb 12, 2004
246
US

I am trying to get my getProperty to work in my JSP page.

My results show a blank where it should show me the average:
Counts on web pages: 40.0 -- 50.0 -- Average now equal to

My JSP page:
Code:
<jsp:useBean id="myStat" class="colors.myStatBean">
<%
	double[] theNumbers = {40, 50};
	myStat.setNumbers(theNumbers);
%>
</jsp:useBean>
<html>
<body>
Counts on web pages:
<%
	double[] numbers = myStat.getNumbers();
	for(int i = 0;i < numbers.length;i++)
	{
		
			out.println(numbers[i] + " -- ");
		
	}
%>

Average now equal to
<jsp.getProperty name="myStat" property="getAverage"/>
<%-- I also tried with property="average" and it also gave me a blank output --%>

</body>
</html>


The Java file:
Code:
package colors;

import java.util.*;

public class myStatBean
{
	private double[] numbers;
	
	//constructor
	public myStatBean()
	{
		numbers = new double[2];
		numbers[0] = 1;
		numbers[1] = 2;
	}
	
	public double getAverage()
	{
		double sum = 0;
		for (int i=0;i < numbers.length;i++)
			sum += numbers[i];
			return sum/numbers.length;
	}
	
	public double[] getNumbers()
	{
		return numbers;
	}
	
	
	public void setNumbers(double[] numbers)
	{
		this.numbers = numbers;
	}

}

 
A bean should be constructed like this :

Code:
public class MyBean {
 private double average = 0.0D;
 
 public double getAverage() {
    // calculate average if you need to
    return average;
 }

 public void setAverage(double average) {
    this.average = average;
 }
}

and you should access it like so :

<jsp.getProperty name="myStat" property="average"/>
 
Thanks, I tried the below but it still doesnt print anything for the Average value:
Code:
<jsp:useBean id="myStat" class="colors.myStatBean">
<%
	double[] theNumbers = {40, 50};
	myStat.setNumbers(theNumbers);
	myStat.setAverage(average);
%>
</jsp:useBean>
<html>
<body>
Counts on web pages:
<%
	double[] numbers = myStat.getNumbers();
	for(int i = 0;i < numbers.length;i++)
	{
		
			out.println(numbers[i] + " -- ");
		
	}
%>

Average now equal to
<jsp.getProperty name="myStat" property="average"/>
</body>
</html>

and my Java File:
Code:
package colors;

import java.util.*;

public class myStatBean {
	private double[] numbers;
	private double average = 0.0D;

	//constructor
	public myStatBean()
	{
		numbers = new double[2];
		numbers[0] = 1;
		numbers[1] = 2;
	}
	
	public double getAverage()
	{
		double sum = 0;
		for (int i=0;i < numbers.length;i++)
			sum += numbers[i];
			average = sum/numbers.length;
			return average ;
	}

 public void setAverage(double average) 
 {
    this.average = average;
 }
	
	public double[] getNumbers()
	{
		return numbers;
	}
		
	public void setNumbers(double[] numbers)
	{
		this.numbers = numbers;
	}
}
 
Sorry typo error found. Disregard my last post. Thanks again for helping me solve my problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top