File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Problems with Bean in JSP... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Problems with Bean in JSP..." Watch "Problems with Bean in JSP..." New topic
Author

Problems with Bean in JSP...

Nick Ueda
Ranch Hand

Joined: Jun 17, 2003
Posts: 40
I am trying to put beans in JSP so I am creating a fruit order example that a book is giving me...
I can show you the code from each part and if you could help me out I would greatly appreciate it...
here is the error I get:
Error: 500
Location: /myWork/confirm.jsp
Internal Servlet Error:
org.apache.jasper.JasperException: Fruit
at org.apache.jasper.compiler.BeanRepository.getBeanType(Unknown Source)
at org.apache.jasper.compiler.GetPropertyGenerator.generate(Unknown Source)
at org.apache.jasper.compiler.JspParseEventListener$GeneratorWrapper.generate(Unknown Source)
at org.apache.jasper.compiler.JspParseEventListener.generateAll(Unknown Source)
at org.apache.jasper.compiler.JspParseEventListener.endPageProcessing(Unknown Source)
at org.apache.jasper.compiler.Compiler.compile(Unknown Source)
at org.apache.tomcat.facade.JasperLiaison.jsp2java(Unknown Source)
at org.apache.tomcat.facade.JasperLiaison.processJspFile(Unknown Source)
at org.apache.tomcat.facade.JspInterceptor.requestMap(Unknown Source)
at org.apache.tomcat.core.ContextManager.processRequest(Unknown Source)
at org.apache.tomcat.core.ContextManager.internalService(Unknown Source)
at org.apache.tomcat.core.ContextManager.service(Unknown Source)
at org.apache.tomcat.modules.server.Http10Interceptor.processConnection(Unknown Source)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(Unknown Source)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(Unknown Source)
at java.lang.Thread.run(Thread.java:536)
Root cause:
java.lang.ClassNotFoundException: Fruit
at org.apache.tomcat.util.depend.DependClassLoader.loadClassInternal1(Unknown Source)
at org.apache.tomcat.util.depend.DependClassLoader12$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.tomcat.util.depend.DependClassLoader12.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at org.apache.jasper.compiler.BeanRepository.getBeanType(Unknown Source)
at org.apache.jasper.compiler.GetPropertyGenerator.generate(Unknown Source)
at org.apache.jasper.compiler.JspParseEventListener$GeneratorWrapper.generate(Unknown Source)
at org.apache.jasper.compiler.JspParseEventListener.generateAll(Unknown Source)
at org.apache.jasper.compiler.JspParseEventListener.endPageProcessing(Unknown Source)
at org.apache.jasper.compiler.Compiler.compile(Unknown Source)
at org.apache.tomcat.facade.JasperLiaison.jsp2java(Unknown Source)
at org.apache.tomcat.facade.JasperLiaison.processJspFile(Unknown Source)
at org.apache.tomcat.facade.JspInterceptor.requestMap(Unknown Source)
at org.apache.tomcat.core.ContextManager.processRequest(Unknown Source)
at org.apache.tomcat.core.ContextManager.internalService(Unknown Source)
at org.apache.tomcat.core.ContextManager.service(Unknown Source)
at org.apache.tomcat.modules.server.Http10Interceptor.processConnection(Unknown Source)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(Unknown Source)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(Unknown Source)
at java.lang.Thread.run(Thread.java:536)
Nick Ueda
Ranch Hand

Joined: Jun 17, 2003
Posts: 40
Here is D:\jakarta-tomcat\webapps\myWork\FruitOder.html
<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>
Here is D:\jakarta-tomcat\webapps\myWork\confirm.jsp
<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="quantityInPounds" 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" property="quantityInPounds" /><br>
Total: $<%=orderedFruit.getPrice()*orderedFruit.getQuantityInPounds() %>
<p></p>
<a href="FruitOrder.html">Return to order form to adjust quantity</a>
</body>
</html>
Nick Ueda
Ranch Hand

Joined: Jun 17, 2003
Posts: 40
Here is the bean: D:\jakarta-tomcat\webapps\ROOT\WEB-INF\classes\Fruit.java
whiich is compiled into a .class file.
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;
}
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66


This is your master clue.
The Fruit class is not being found. Either you don't have it in the web app's classpath, or (more likely) your web container isn't dealing with classes in the default package. There are some well-known problems with this in various containers.
You should place Fruit in a package other than the default. This is a good practice even regardless of this error.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nick Ueda
Ranch Hand

Joined: Jun 17, 2003
Posts: 40
i have no idea what you are talking about so how do I go about doing this? How do I place it in another package other than default?
Thanks,
Nick Ueda
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66


There's your clue! (always look for the root cause of a servlet exception to find out what triggered it in the first place).
The class Fruit can't be found. Either you don't have it in the classpath, or (more likely) your servlet container is tripping over the fact that Fruit is in the default package. There are some well-know problems with various containers in this regard.
You should place Fruit in a package other than the default. This is also just good practice regardless of this error.
hth,
bear
Nick Ueda
Ranch Hand

Joined: Jun 17, 2003
Posts: 40
I am sorry...i may not have phrased my question properly. I understand that the Fruit class cannot be found, but I don't know where else to put it...
How do I redirect my classes and such to another directory other than DEFAULT?
Thanks,
Nick Ueda
Winston Smith
Ranch Hand

Joined: Jun 06, 2003
Posts: 136
Nick --
Tomcat should come with some examples of JSP pages (check [TomcatHome]/webapps/examples). You'll see that all the Bean classes are stored in [TomcatHome]/WEB-INF/classes, with the package name examples. Tomcat will search the WEB-INF directory for classes to load, so you'll need to place your classes here. You either need to set the Tomcat configuration to load classes from wherever your classes are located, or place your classes in one of the WEB-INF directories, such as the one under ROOT.


for (int i = today; i < endOfTime; i++) { code(); }
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Btw, sorry for the double posting -- my browser was doing some wierdnesses last night.
Nick, check out the Java in General forum for more information on packages. You really need to have some good Java fundamentals under your belt to tackle more advanced topics such as servlets and JSP.
hth,
bear
[ June 23, 2003: Message edited by: Bear Bibeault ]
Nick Ueda
Ranch Hand

Joined: Jun 17, 2003
Posts: 40
I am trying to make it painfully clear to Tomcat as to which directory I want it to look in for my classes and I see how the examples are working...but I can't quite get Tomcat to look where I want it to.
Any more help would be great!
(Thank you guys so much for helping me this far...)
Thanks,
Nick Ueda
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Some details on how you are setting things up, and what exactly it is not doing would help.
Perhaps at this point it would make sense to start a new topic in the Tomcat forum...
bear
Nick Ueda
Ranch Hand

Joined: Jun 17, 2003
Posts: 40
Originally posted by Bear Bibeault:
Some details on how you are setting things up, and what exactly it is not doing would help.

Note:
I am trying to get an example from JSP Weekend Crash Course to work...
D:\jakarta-tomcat\webapps\myWork
is where I have put two files...
FruitOrder.html and confirm.jsp code for the previous files are listed above.
Previously I have placed and compiled my Fruit.java bean (this file is also list above)in:
D:\jakarta-tomcat\webapps\ROOT\WEB-INF\classes
but this is obviously not being found by Tomcat.
The book told me to do that.
FruitOrder.html calls confirm.jsp which uses the Fruit bean.
I would appreciate anyone's input on how my files should be set up.
I do not know how stuff is being directed to:
D:\jakarta-tomcat\work\DEFAULT\myWork\ is this correct? I assume this is where the JSP goes after being translated into a servlet...
I need to fix this problem quick so I can continue learning JSP...If anymore information is needed to help me solve
this problem...please tell me what and I will be here to give it... Thank you so much for your help...
So when ever I submit information in the html form it gives me this error...
Error: 500
Location: /myWork/confirm.jsp
Internal Servlet Error:
org.apache.jasper.JasperException: Unable to compile Note: sun.tools.javac.Main has been deprecated.
D:\jakarta-tomcat\work\DEFAULT\myWork\confirm_2.java:50: Class Fruit not found.
Fruit orderedFruit = null;
^
D:\jakarta-tomcat\work\DEFAULT\myWork\confirm_2.java:53: Class Fruit not found.
orderedFruit= (Fruit)
^
D:\jakarta-tomcat\work\DEFAULT\myWork\confirm_2.java:58: Class Fruit not found.
orderedFruit = (Fruit) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "Fruit");
^
D:\jakarta-tomcat\work\DEFAULT\myWork\confirm_2.java:103: Class Fruit not found.
out.print(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((Fruit)pageContext.findAttribute("orderedFruit")).getFruitName())));
^
D:\jakarta-tomcat\work\DEFAULT\myWork\confirm_2.java:110: Class Fruit not found.
out.print(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((Fruit)pageContext.findAttribute("orderedFruit")).getColor())));
^
D:\jakarta-tomcat\work\DEFAULT\myWork\confirm_2.java:117: Class Fruit not found.
out.print(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((Fruit)pageContext.findAttribute("orderedFruit")).getPrice())));
^
D:\jakarta-tomcat\work\DEFAULT\myWork\confirm_2.java:124: Class Fruit not found.
out.print(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((Fruit)pageContext.findAttribute("orderedFruit")).getQuantityInPounds())));
^
7 errors, 1 warning
at org.apache.tomcat.facade.JasperLiaison.javac(Unknown Source)
at org.apache.tomcat.facade.JasperLiaison.processJspFile(Unknown Source)
at org.apache.tomcat.facade.JspInterceptor.requestMap(Unknown Source)
at org.apache.tomcat.core.ContextManager.processRequest(Unknown Source)
at org.apache.tomcat.core.ContextManager.internalService(Unknown Source)
at org.apache.tomcat.core.ContextManager.service(Unknown Source)
at org.apache.tomcat.modules.server.Http10Interceptor.processConnection(Unknown Source)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(Unknown Source)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(Unknown Source)
at java.lang.Thread.run(Thread.java:536)
[ June 23, 2003: Message edited by: Nick Ueda ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Firstly, you should familiarize yourself with how webapps should be layed out on disk. I think you are getting confused by trying to piggy-back your stuff in the Tomcat folders.
A short primer assuming you want to create an app named 'mywork':
1) create a folder named mywork somewhere on disk; doesn't really matter where.
2) create the following folders:

3) Put your html and JSP files in mywork/
4) Put your web.xml if you have one in mywork/WEB-INF/
5) Put any jar files your app needs to run in mywork/WEB-INF/lib/
6) Compile your bean and other classes into mywork/WEB-INF/classes/ being sure not to place any classes in the default package.
7) Edit $CATALINA_HOME/conf/server.xml. Find the </Host> declaration and declare your context just before that declaration. An example context could be:

Make sure that the docbase attribute specifies where you create the mywork folder.
8) Restart Tomcat and you now have your own context to play in. Any URL that specifies mywork/ as a context will resolve to this. For example:

The best thing you could do for yourself is to read the Servlet spec. It may seem daunting, but it's actually not hard to read and spells out all the "rules" quite clearly.
hth,
bear
Justin Chu
Ranch Hand

Joined: Apr 19, 2002
Posts: 209
    
    1
Try adding a package name to your fruit class..
Prepend to your Fruit.java
package myexample;
Compile, and place it here
/WEB-INF/classes/myexample/Fruit.class
Refer to your Fruit class as myexample.Fruit throughout your JSP pages.
The last time I tried, the JSP file cannot find any classes without a package.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with Bean in JSP...