using a javabean in JSP....where???

Here is what I have:
Tomcat 4.0
JDK 1.4
W2000 Server

I'm trying to use javabeans with my JSP page. I have successfully created a javabean class file and have put it in the /WEB_INF/classes folder in the directory where the jsp files are located. To access the javabean I used this tag:

<jsp:useBean class="insertBean" id="insert" scope="application" />

I have also tried..

<jsp:useBean class="/classes/insertBean" id="insert" scope="application" />

but when I try to load the page I get an error saying that it cannot find insertBean.class

Where should I put the class file?
LVL 1
ltdang7Asked:
Who is Participating?
 
cheekycjConnect With a Mentor Commented:
I added:

package mark;

to the top of insertBean.java

put it in my WEB-INF/classes/mark/
directory

loaded beans.jsp and it worked fine.

I didn't try the jar but I did notice that your jar didn't have the directory hierarchy.

so when you want to create the jar you must do the following:

create a mark directory.
compile insertBean in this directory.

To create the jar.. go up one level.

now run:

jar -cf mark.jar mark/*

that should create the jar with the proper directory structure.

You will have to put this in your lib directory.

Also restart Tomcat if need be.

HTH,
CJ
0
 
objectsCommented:
Is your bean in a package?
The WEB-INF directory needs to be in the basae directory of the webapp.
0
 
ltdang7Author Commented:
NO it's not, this is my directory info


ROOT
    |-- WEB-INF  [web.xml]
    |   |-- classes  insertbean.class
    |   `-- lib  
    |
    `jsp files
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
cheekycjCommented:
are you importing the bean? does it have a package associated with it?

when you have the usebean try putting the full package name.

CJ
0
 
ltdang7Author Commented:
It is not in a package, I have got the file insertBean.class alone in the classes folder.
0
 
cheekycjCommented:
rather than useBean can you try this:

<%
  insertBean myInserBean = new insertBean();

%>

and see what happens.
0
 
cheekycjCommented:
also, is the file name insertBean or InsertBean

CJ
0
 
ltdang7Author Commented:
It is insertBean.call and this is what I got when I tried..
<%
 insertBean myInserBean = new insertBean();

%>

I got this>>>

Generated servlet error:
C:\Program Files\Apache Tomcat 4.0\work\localhost\_\beans$jsp.java:60: Class org.apache.jsp.insertBean not found.
                 insertBean myInserBean = new insertBean();
0
 
ltdang7Author Commented:
It is insertBean.call and this is what I got when I tried..
<%
 insertBean myInserBean = new insertBean();

%>

I got this>>>

Generated servlet error:
C:\Program Files\Apache Tomcat 4.0\work\localhost\_\beans$jsp.java:60: Class org.apache.jsp.insertBean not found.
                 insertBean myInserBean = new insertBean();
0
 
cheekycjCommented:
few things to note here:

first make sure that your beans class is public meaning it is declared as:
public class insertBean

Another issue might be namespace.  Supposedly some engines (maybe Tomcat falls in this category) do not handle unnamed packages (classes not in any package) well so classes end up not being found.  You can solve this by having a package name for your class.

CJ

0
 
rrzCommented:
>C:\Program Files\Apache Tomcat 4.0\work\localhost\_\beans$jsp.java:60: Class org.apache.jsp.insertBean not found.

Yes, as CJ says, put your bean in a package. Tomcat thinks the bean is in the org.apache.jsp  package.
0
 
ltdang7Author Commented:
Thanks guys...

>>first make sure that your beans class is public meaning >>it is declared as:
>>public class insertBean
I have check that and it is public

>>Yes, as CJ says, put your bean in a package. Tomcat >>thinks the bean is in the org.apache.jsp  package.
Can I use Winzip to make a jar file? Where do I out this package once I have created it?
0
 
cheekycjCommented:
if your package is
ltdang
and your class is insertBean

you reference it using ltdang.insertBean

and you put the ltdang.class file in
/WEB-INF/classes/ltdang/

or you can jar it up.

to jar it up you need to use the Java Jar command.

CJ
0
 
ltdang7Author Commented:
ok, I have created a jar by going into a CMD and typing

jar cf mark.jar insertBean.class

This created a file called mark.jar with the insertBean and the manifest file.
It is located in the /WEB-INF/classes folder off my ROOT directory.
In my JSP page I have this entry

<jsp:useBean id="insert" class="mark.insertBean" scope="session"/>

and when I load the page it gives my this error


An error occurred at line: 6 in the jsp file: /beans.jsp

Generated servlet error:
C:\Program Files\Apache Tomcat 4.0\work\localhost\_\beans$jsp.java:61: Class mark.insertBean not found.
                mark.insertBean insert = null;
                    ^


An error occurred at line: 6 in the jsp file: /beans.jsp

Generated servlet error:
C:\Program Files\Apache Tomcat 4.0\work\localhost\_\beans$jsp.java:64: Class mark.insertBean not found.
                    insert= (mark.insertBean)
                                 ^


An error occurred at line: 6 in the jsp file: /beans.jsp

Generated servlet error:
C:\Program Files\Apache Tomcat 4.0\work\localhost\_\beans$jsp.java:69: Class mark.insertBean not found.
                            insert = (mark.insertBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "mark.insertBean");


I get the feeling that I'm doing somthing really stupid! Please the idiot!
0
 
cheekycjCommented:
jars need to be put in the lib directory.

WEB-INF\lib

CJ
0
 
ltdang7Author Commented:
Sorry, gives the same error!

I'm increasing the points to 100 to cover the hard work done already.
0
 
cheekycjCommented:
can you try it by putting the insertBean.class
in
/WEB-INF/classes/mark

and see what happens then.

CJ
0
 
ltdang7Author Commented:
same error...

An error occurred at line: 6 in the jsp file: /beans.jsp

Generated servlet error:
C:\Program Files\Apache Tomcat 4.0\work\localhost\_\beans$jsp.java:61: Class mark.insertBean not found.
                mark.insertBean insert = null;
                    ^


An error occurred at line: 6 in the jsp file: /beans.jsp

Generated servlet error:
C:\Program Files\Apache Tomcat 4.0\work\localhost\_\beans$jsp.java:64: Class mark.insertBean not found.
                    insert= (mark.insertBean)
                                 ^


An error occurred at line: 6 in the jsp file: /beans.jsp

Generated servlet error:
C:\Program Files\Apache Tomcat 4.0\work\localhost\_\beans$jsp.java:69: Class mark.insertBean not found.
                            insert = (mark.insertBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "mark.insertBean");
0
 
cheekycjCommented:
ok.. can you post your mark.insertBean code?

and then your JSP.

I wanna try it out.

CJ
0
 
ltdang7Author Commented:
thanks for your support...

ftp://henry.27south.com
anonymous access

the files (insertBean.class,beans.jsp and mark.jar) are in the root directory
0
 
cheekycjCommented:
can you put the source of insertBean in there too.

CJ
0
 
cheekycjCommented:
btw: nice collection of files :-)
0
 
ltdang7Author Commented:
thanks, I saw you routing around!
0
 
cheekycjCommented:
:-)

can you post the code for the bean or even just email it to me:

cheekycj@yahoo.com

CJ
0
 
ltdang7Author Commented:
it's on there, try again
0
 
cheekycjCommented:
got it.

CJ
0
 
cheekycjCommented:
in insertBean.java

add the following line at the top:

package mark;

Now it should work.

CJ
0
 
ltdang7Author Commented:
The package entry has been added, but unfornatelly no change in the error message. It looks like the complier just does not know where the file is and I don't know what folder it is looking in!
0
 
ltdang7Author Commented:
Hurra! It worked. thank you thank you thank you
0
 
ltdang7Author Commented:
This guy is a genius, triple 'A' rating!
0
 
cheekycjCommented:
Glad I could help and Thanx for the "A".

CJ
0
All Courses

From novice to tech pro — start learning today.