JSP/Bean basics

F

fishfry

I'm just learning how to use JSP's to invoke simple Java Beans. The
concepts are clear but I can't get my example to work. The problem is
that my JSP can't find my Bean class.

Is it mandatory for the Bean to be packaged in a jar file? Do I have to
use the BDK? Or is it sufficient to write a class that conforms to Bean
conventions, compile it, and put the class file along my classpath? I'm
pretty sure I already have my Bean class file in my classpath, because
it's in the same place as my other Tomcat servlets and they run fine.

I've Googled around for tutorials and strangely, I can't find the simple
answer I'm looking for. Any help greatly appreciated.
 
C

Cid

I'm just learning how to use JSP's to invoke simple Java Beans. The
concepts are clear but I can't get my example to work. The problem is
that my JSP can't find my Bean class.

Is it mandatory for the Bean to be packaged in a jar file? Do I have to
use the BDK? Or is it sufficient to write a class that conforms to Bean
conventions, compile it, and put the class file along my classpath? I'm
pretty sure I already have my Bean class file in my classpath, because
it's in the same place as my other Tomcat servlets and they run fine.

You may have to provide some more specifics (including code). IIRC the
bean must be in a package - not the default package. Other than that
it can be in /classes directly (.class file) or a jar in /lib.
 
K

kaeli

I'm just learning how to use JSP's to invoke simple Java Beans. The
concepts are clear but I can't get my example to work. The problem is
that my JSP can't find my Bean class.

It has to be in a package.
Did you package the beans?

The path to the package must be in the classpath of the server in some way.
You mention that you are using Tomcat. Tomcat uses a web.xml file and the
directories have to be set up a certain way. I use IPlanet, so I can't say if
it's exactly the same, but my beans are in WEB-INF/classes/beanPackageName/
Is it mandatory for the Bean to be packaged in a jar file?

Not for mine it didn't.
I use IPlanet.
Or is it sufficient to write a class that conforms to Bean
conventions, compile it, and put the class file along my classpath?

That's all I did.
(IPlanet 6 on a Unix Solaris machine)

Here's what I did. Perhaps it will help you.

I have an application I'll call orderSystem. It is in
/home/myLogin/public_html/orderSytem
In that directory are all the jsp and html files.
Then, I have a subdir called WEB-INF.
/home/myLogin/public_html/orderSytem/WEB-INF/web.xml has the config stuff. I
also have a TLD for my taglib.
/home/myLogin/public_html/orderSytem/WEB-INF/classes/myTaglib/ has the
taglibs.
/home/myLogin/public_html/orderSytem/WEB-INF/classes/myBeanPackage/ has the
beans. The beans all have a package
package myBeanPackage;
at the top.

I don't have ANY jar files.

Now, the JSP calls the bean like this:
<jsp:useBean id="myBeanId" class="myBeanPackage.theBeanName" scope="page"/>

Hope this helps.

--
 
C

Carl Smotricz

A little extra info on the required "beaniness" of the beans:

To work with jsp:getProperty/setProperty, your class will need to have
getters and setters, obviously. But it's possible, at least in my setup
with Tomcat 4.something, to use classes that don't even have
getters/setters! The only HARD requirement remaining from the Bean spec
is that the class needs to have a no-argument constructor.

Why would I do such a silly thing? Well, the jsp:useBean tag lets me
assign an ID to a class instance bound to my
page|request|session|application. This in turn allows me to call methods
on the object within scriptlets. If that's what I want to do.

Happy hacking!

-Carl-
 
F

fishfry

kaeli said:
It has to be in a package.
Did you package the beans?

The path to the package must be in the classpath of the server in some way.
You mention that you are using Tomcat. Tomcat uses a web.xml file and the
directories have to be set up a certain way. I use IPlanet, so I can't say if
it's exactly the same, but my beans are in WEB-INF/classes/beanPackageName/

Thanks much. I'm getting close. I know whatever I'm doing wrong must be
totally lame. Here's my code.

Bean:

package fooBeanPackage;

public class FooBean {
int x = 47;
String s = "Hello World";

public FooBean() {
}

int getX() {
return x;
}

void setX(int val) {
x = val;
}

String getS() {
return s;
}

void setS(String val) {
s = val;
}
} // FooBean

And now here's the relevant part of my JSP:

<jsp:useBean id = "foo" class = "fooBeanPackage.FooBean" scope = "page"
/>

<p>Here is data from FooBean:<br>

<jsp:getProperty name = "foo"
property = "x"
/>


After compiling FooBean, I put the class file in classes/fooBeanPackage.

Now when I run the JSP I get:

org.apache.jasper.JasperException: Cannot find any information on
property 'x' in a bean of type 'fooBeanPackage.FooBean'

org.apache.jasper.runtime.JspRuntimeLibrary.getReadMethod(JspRuntimeLibra
ry.java:883)

org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java
:1055)

org.apache.jasper.compiler.Node$GetProperty.accept(Node.java:1079)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2180)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2230)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2236)
org.apache.jasper.compiler.Node$Root.accept(Node.java:485)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2180)

org.apache.jasper.compiler.Generator.generate(Generator.java:3255)

org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:277)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:456)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)

org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.jav
a:553)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.jav
a:291)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
 
A

Andrew Thompson

int x = 47;

try..
public int x = 47;

[ and, please choose either c.l.j.help, *or*
c.l.j.programmer. Cross-posting to those two
very different groups can cause problems.
F'Ups set to c.l.j.help. ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top