• 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

Java Bean in JSP

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am starting to learn about using Java Beans.
I copied some Java Bean codes from a book and compiled the .java file to a .class file. No problem.
Placed the .class file in ....\webapps\root\myjsp\WEB-INF\classes folder. The JSP files are in ....\webapps\root\myjsp.
When I tried to call the bean, I got the following error:
org.apache.jasper.JasperException: getProperty: Mandatory attribute property missing
at org.apache.jasper.compiler.JspUtil.checkAttributes(JspUtil.java:202)
at org.apache.jasper.compiler.Parser$GetProperty.accept(Parser.java:677)
at org.apache.jasper.compiler.Parser.parse(Parser.java:1073)
at org.apache.jasper.compiler.Parser.parse(Parser.java:1038)
at org.apache.jasper.compiler.Parser.parse(Parser.java:1034)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:182)
at org.apache.jasper.runtime.JspServlet.loadJSP(JspServlet.java:413)
at org.apache.jasper.runtime.JspServlet$JspServletWrapper.loadIfNecessary(JspServlet.java:149)
at org.apache.jasper.runtime.JspServlet$JspServletWrapper.service(JspServlet.java:161)
at org.apache.jasper.runtime.JspServlet.serviceJspFile(JspServlet.java:261)
at org.apache.jasper.runtime.JspServlet.service(JspServlet.java:369)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:503)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:559)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:160)
at org.apache.tomcat.service.TcpConnectionThread.run(SimpleTcpEndpoint.java:338)
at java.lang.Thread.run(Thread.java:484)
I have checked the code and am very sure I have copied it to the letter. I am using Tomcat 3.2.1 in Win 98.
Thanks in advance for your advise.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The exception you got

org.apache.jasper.JasperException: getProperty: Mandatory attribute property missing


indicates that you are missing an attribute in the getProperty tag.
For example, the following JSP tags will first create an instance of the class com.xyz.Foo on a new request to the page and refer to the instance by the Id 'foo'
Then the getProperty tag will get the JavaBean property 'bar' from that instance ... which means your class Foo should have a method 'getBar()'.
Both 'name' and 'property' are mandatory attributes for the getProperty tag.
<jsp:useBean id="foo" class="com.xyz.Foo" scope="request"/>
<jsp:getProperty name="foo" property="bar"/>
 
Mark Leong
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Made a mistake. My Tomcat version is 3.1 running on top of Win98.
The funny thing is even when I have deleted the bean (the class file), the same error is still reported. I believe I have not placed the bean in the correct folder.
At the moment I have placed the jsp and htm files in ..../webapps/root/myjsp.
The java, class files are placed in ...../webapps/root/myjsp/WEB-INF/classes.
My I do any setting to Tomcat to activate the bean?

Here are the files (htm, jsp, java):-
------------------------
<jsp:useBean id="orderedFruit" class="Fruit" />
<jsp:setProperty name="orderedFruit" property="fruitName" value="Mango" />
<jsp:setProperty name="orderedFruit" property="color" value="Orange" />
<jsp:setProperty name="orderedFruit" property="price" value="5.95" />
<jsp:setProperty name="orderedFruit" property="quantity" param="quantity" />

<html>
<body>
<h1>Your Fruit Order</h1>
<br><br>
Fruit: <jsp:getProperty name="orderedFruit" property="fruitName"/><br>
Color: <jsp:getProperty name="orderedFruit" property="color"/><br>
Price: $<jsp:getProperty name="orderedFruit" property="price"/><br>
Quantity: <jsp:getProperty name="orderedFruit" param="quantity" /><br>
Total:  $<%=orderedFruit.getPrice()*orderedFruit.getQuantity() %>
<br>
<p></p>
<a href="Listing12-3.jsp">Return to order form to adjust quantity</a>
</body>
</html>
--------------------------
public class Fruit {
private String fruitName;
private int quantity;
private String color;
private boolean isCitrus;
private float price;
public String getFruitName(){
return this.fruitName;
}
public void setFruitName(String name){
this.fruitName=name;
}
public int getQuantityInPounds(){
return this.quantity;
}
public void setQuantityInPounds(int quantity){
this.quantity=quantity;
}
public String getColor(){
return this.color;
}
public void setColor(String color){
this.color=color;
}
public float getPrice(){
return this.price;
}
public void setPrice(float price){
this.price=price;
}
public boolean isCitrus(){
return this.isCitrus;
}
public void setCitrus(boolean isCitrus){
this.isCitrus=isCitrus;
}
}
-------------------------------
<html>
<body>
<h1>Fruit Order Form</h1>
Fruit: Mango<br>
Color: Orange<br>
Price Per Pound: $5.95 <br>
<form action="confirm.jsp" method="post">
Number of pounds: <input type="text" name="quantity"><br>
<input type="submit" value="Order Fruit">
</form>
</body>
</html>
-----------------------------
There is also something very weird. Initially I have made a test jsp page in ..../webapps/test. I delete the folder completely and restarted Tomcat. However when I tried to load the jsp page, it loaded!! Can I delete files in the <tomcat-install>\work folder?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Leong:
Made a mistake. My Tomcat version is 3.1 running on top of Win98.
The funny thing is even when I have deleted the bean (the class file), the same error is still reported. I believe I have not placed the bean in the correct folder.
At the moment I have placed the jsp and htm files in ..../webapps/root/myjsp.
The java, class files are placed in ...../webapps/root/myjsp/WEB-INF/classes.
My I do any setting to Tomcat to activate the bean?

Here are the files (htm, jsp, java):-


Hi,
try deleting the work folder in the tomcat directory!
and see!!
hope it works!!!
mail me !
if not!
bye
------------------
 
Bill Siggelkow
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I am not mistaken, the problem is in the following tag ... you need to change 'param' to 'property'


Quantity: <jsp:getProperty name="orderedFruit" param="quantity" /><br>



[This message has been edited by Bill Siggelkow (edited December 05, 2001).]
 
Mark Leong
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, realised my mistake (in fact its the mistake from the book). I altered the Fruit.java file and compiled it without problem. Place the class file in .....\webapps\root\web-inf\classes and run the jsp page (also corrected the code). Now I got the following error message:-
org.apache.jasper.JasperException: Unable to compile class for JSPC:\tomcat\jakarta-tomcat-3.2.1\work\localhost_8080\_0002fgregory_0002fJSPsessions_0002fSession_00031_00032_0002fconfirm_0002ejspconfirm_jsp_0.java:60: Class gregory.JSPsessions.Session_00031_00032.Fruit not found.
Fruit orderedFruit = null;
^
C:\tomcat\jakarta-tomcat-3.2.1\work\localhost_8080\_0002fgregory_0002fJSPsessions_0002fSession_00031_00032_0002fconfirm_0002ejspconfirm_jsp_0.java:63: Class gregory.JSPsessions.Session_00031_00032.Fruit not found.
orderedFruit= (Fruit)
^
Now I am one step closer. Could it be my classpath setting? I have set the JAVA_HOME and TOMCAT_HOME parameters and pointed the PATH setting to the correct JDK folder (otherwise I would not be able to compile the java files). I have not made any setting to CLASSPATH.
I have not made any entry in the sever.xml or other related files.
Hmmmm..... Thought this would be easier than servlets. Got that working without a hitch.

Thanks guys.

[This message has been edited by Mark Leong (edited December 05, 2001).]
 
Mark Leong
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Placed the bean in a package and everything is OK now.
Why can't it work without package?
Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic