• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble writing to bean

 
Richie Heduan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am rather new to using JSP and I am having trouble writing variables taken from a html form and sending them to a bean..i was wondering if anybody would be able to shed some light on where I am going wrong..

My code for the HTML is
<----code---->


<html>
<head><title>Upper Body Resistance Training</title></head>
<body>
<form action="repmaxcalc.jsp" method="post">
Weight in lbs: <input type="text" name="weight" value=""><br/>

Number of Reps: <input type="text" name="reps" value=""><br/>

<input type="submit" value="Submit">
</form>
</body>
</html>

code for the JSP page that handles the input and sends it to the bean is:

<-----code------>

<%@ page language="java" contentType="text/html" %>

<html>
<head><title>Value Bean Demo</title> </head>
<body>

<jsp:useBean id="Calc" scope="request" class="mybeans.repmaxBean" />
<jsp:setProperty name="Calc" property="weight" value="<%= Calc.getWeight()%>" />
<jsp:setProperty name="Calc" property="reps" value="<%= Calc.getReps()%>" />
<ul>

<li>Repmax: <jsp:getProperty name="Calc" property="repmax" />
</ul>


</body> </html>

the code for the bean is

<------code------>

package mybeans ;

public class repmaxBean
{
// declare variables

private int weight;
private int reps;
private int repmax;

//setter methods



public void setWeight(int weight)
{

this.weight = weight;
}
public void setReps(int reps )
{
this.reps = reps;
}

public int getWeight()
{
return weight;
}

public int getReps()
{
return reps;
}



public int getRepmax()

{
repmax = (weight + reps);

return repmax;}

}

At the moment I just wanted the bean to perform a simple addition for simplicity until I's sure its receiving the variables

The error message Im getting from Tomcat is :

org.apache.jasper.JasperException: Can't find a method to write property 'weight' of type 'int' in a bean of type 'mybeans.repmaxBean'
org.apache.jasper.runtime.JspRuntimeLibrary.handleSetProperty(JspRuntimeLibrary.java:677)
org.apache.jsp.repmaxcalc_jsp._jspService(repmaxcalc_jsp.java:59)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

org.apache.jasper.JasperException: Can't find a method to write property 'weight' of type 'int' in a bean of type 'mybeans.repmaxBean'
org.apache.jasper.runtime.JspRuntimeLibrary.getWriteMethod(JspRuntimeLibrary.java:798)
org.apache.jasper.runtime.JspRuntimeLibrary.handleSetProperty(JspRuntimeLibrary.java:674)
org.apache.jsp.repmaxcalc_jsp._jspService(repmaxcalc_jsp.java:59)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
---------------------------------------------------------------------
I would greatly appreciate anything that anyone has to say about my code
Thanks,
Richie
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Upon a quick inspection, I don't see anything wrong with your bean structure that could cause that error message, but that's rather moot as what you're trying to do doesn't make much sense in the first place.

Consider the following fragment:



You are instantiating an instance of the bean, and then immediately try to set the weight property of the bean with the value of the weight property of the bean. Essentailly attempting to set the property with its own value.

It makes the same amount of semantic sense as something along the lines of the following:



Doesn't make much sense, does it?

If you are trying to set the bean properties from the values of the submitted parameters (which is what I am assuming), you need to reference the parameters, not the bean, to obtain the values.

I would greatly appreciate anything that anyone has to say about my code


Be careful what you wish for. I have a long laundry list of places in your code where you violate accepted practices and standards.
[ February 16, 2007: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!