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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JSP newbie: My first JavaBean

Status
Not open for further replies.

BlueBeep

Programmer
Joined
Aug 13, 2002
Messages
23
Location
US
Hey guys. This is my first time with JSP, and I'm trying to create a JavaBean. I seem to be getting an error when I try to set up the following:

I have a folder called "test" which is the root of my app. Under this directory, I have a folder called "WEB-INF", then a folder called "classes" then the folder called "mathy". Under "mathy" I have a the java file called average.java along with the compiled class file.

The code for average.java is as follows:

package mathy;
import java.io.Serializable;

public class average implements Serializable {

private double total = 0;
public average(double n1, double n2, double n3) {
total = n1 + n2 + n3;
total = total / 3;
}
public double getAverage() {
return total;
}
}

then under the original "test" folder, I have a jsp page called "exp-1.jsp" with the following code:

<% mathy.average av1 = new mathy.average(34.4, 56.1, 694.3); %>

Now, I'm not trying to execute any methods, just trying to see if a bean can be made. But now luck. Can anyone help? Thanks, I really appreciate it.


I get the following error by the way with Tomcat Version 4.0-b5:


A Servlet Exception Has Occurred
org.apache.jasper.JasperException: Unable to compile class for JSPC:\jakarta-tomcat-4.0-b5\work\localhost\test\exp_0002d4_jsp.java:55: Class mathy.average not found.
mathy.average av1 = new mathy.average(34.4, 56.1, 694.3);
^
C:\jakarta-tomcat-4.0-b5\work\localhost\test\exp_0002d4_jsp.java:55: Class mathy.average not found.
mathy.average av1 = new mathy.average(34.4, 56.1, 694.3);
^
2 errors
 
You've got to import your class into your JSP by adding the following line at the top of your page :
Code:
<%@ page import=&quot;mathy.average&quot; %>
Water is not bad as soon as it stays out human body ;-)
 
Thanks for the reply! However, now I just get one error instead of two. Someone mentioned that I had to include

import java.beans.*;

in my original java bean. Is this correct? I response would be greatly appreciated. Thanks again!

The error:

A Servlet Exception Has Occurred
org.apache.jasper.JasperException: Unable to compile class for JSPC:\jakarta-tomcat-4.0-b5\work\localhost\test\exp_0002d6_jsp.java:3: Class mathy.average not found in import.
import mathy.average;
^
1 error
 
Here, I think you've got a tomcat config problem. It seems that tomcat doesn't know where to find your &quot;average&quot; class.
Well, try to create a &quot;mathy&quot; directory under the &quot;WEB-INF&quot; directory and put your &quot;average.class&quot; in. Because your directories structure must respect your packages structure.

Tell me if It works better. Water is not bad as soon as it stays out human body ;-)
 
I can kiss you... it works! Finally... you have no idea in how many places I asked this... =) Thank you thank you! It also works if I name my package &quot;classes.mathy&quot; instead of just mathy. Why is this? I thought Tomcat automatically looked for classes in the WEB-INF\classes folder... or so I thought by this book I'm reading.

Also, everytime I edit a JSP file, and I try to refresh, I don't get an updated version... just the original page that I viewed first. Got a solution? Wanna go two for two? =)
 
As I told you before, the WEB-INF directory is the root directory for all your classes. In this directory, the structure must exactly match the packages structures. ie: if you've got 2 classes :
Code:
toto.tutu.myclass
and
Code:
toto.tata.anotherClass
, the directories structures under WEB-ROOT must be :
WEB-ROOT
|- toto
|- tutu
| |- myclass
|- tata
|- anotherClass

For the refresh problem, it's due to page caching. This caching can either be client side so you've got to delete the &quot;temporary internet files&quot; folder under your profile; or server size then delete the &quot;Work&quot; directory under the %tomcatHome% directory.

Sorry, i'm french and my english is poor (but my tailor is rich ;-)) and I don't understand &quot;Wanna go two for two?&quot; Water is not bad as soon as it stays out human body ;-)
 
Thanks for the help! I still have no luck with the refresh however. No matter how many times I try to delete the work directory, restart tomcat - nothing. I have to delete the work direectory manually everytime I want to refresh the JSP for an update that I did. Any solutions? Thanks.

As far as the expression &quot;two for two&quot; - it just simple means succeeding at two things straight in a row. =)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top