Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

JSP/Bean basics
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.

Report this thread to moderator Post Follow-up to this message
Old Post
fishfry
07-29-04 02:09 AM


Re: JSP/Bean basics
In article <BLOCKSPAMfishfry-79D76A.00214327072004@netnews.comcast.net>,
BLOCKSPAMfishfry@your-mailbox.com enlightened us with...
> 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 i
f
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.

--
--
~kaeli~
If you don't pay your exorcist, you get repossessed.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace


Report this thread to moderator Post Follow-up to this message
Old Post
kaeli
07-29-04 02:09 AM


Re: JSP/Bean basics
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-

fishfry wrote:
> 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.

Report this thread to moderator Post Follow-up to this message
Old Post
Carl Smotricz
07-29-04 02:09 AM


Re: JSP/Bean basics
In article <MPG.1b7010f31af9eb86989ff8@nntp.lucent.com>,
kaeli <tiny_one@NOSPAM.comcast.net> wrote:

> In article <BLOCKSPAMfishfry-79D76A.00214327072004@netnews.comcast.net>,
> BLOCKSPAMfishfry@your-mailbox.com enlightened us with... 
>
> 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/[/color
]

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)

Report this thread to moderator Post Follow-up to this message
Old Post
fishfry
07-29-04 02:09 AM


Re: JSP/Bean basics
On Wed, 28 Jul 2004 05:25:00 GMT, fishfry wrote:

>     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. ]

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Thompson
07-29-04 02:09 AM


Re: JSP/Bean basics
In article <waoh7a3bt6bt.1v8wm33xcqnpk$.dlg@40tude.net>,
Andrew Thompson <SeeMySites@www.invalid> wrote:

> On Wed, 28 Jul 2004 05:25:00 GMT, fishfry wrote:
> 
>
> 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. ]

Aha. It's the other way round ... property declarations private, getters
and setters public. So:

private int x = 47;
...
public int getX() {
return x;
}

work perfectly. Thanks all!!

Report this thread to moderator Post Follow-up to this message
Old Post
fishfry
07-29-04 02:09 AM


Re: JSP/Bean basics
On Wed, 28 Jul 2004, fishfry wrote:
[..]
> 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"
> />

property=getX() perhaps?  the primitive variable x *should* be private,
either implicit or explicitly declared, I imagine.  making it public would
break data encapsulation IMHO.  Isn't the *point* of beans to use getX()
and setX()? By definition any class that uses getters and setters is a
bean AFAIK.


Thufir Hawat
<http://www.shaw.ca/members/hawat/source/>

Report this thread to moderator Post Follow-up to this message
Old Post
thufir.hawat@mail.com
07-29-04 02:09 AM


Re: JSP/Bean basics
On Wed, 28 Jul 2004 05:44:56 GMT, fishfry wrote:
> In article Andrew Thompson <SeeMySites@www.invalid> wrote: 
.. 
..
> private int x = 47;
...
> work perfectly.

Aaah.. (chuckles) I knew it was *something* to
do with the access modifiers of 'x'.    ;)

> Thanks all!!

Welcome.

Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Thompson
07-29-04 02:09 AM


Re: JSP/Bean basics
On Wed, 28 Jul 2004 05:31:50 GMT, Andrew Thompson
<SeeMySites@www.invalid> wrote:

>On Wed, 28 Jul 2004 05:25:00 GMT, fishfry wrote:
> 
>
>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. ]

I think, rather, what you want is public visibility for the
getter/setters, not the variable. Looked like your getX() setX()
methods were are default (package) security. So JSP won't be able to
see them since it isn't in fooBeanPackage. Try

public int getX()
public void setX()

instead. While you're at it, I think the convention on package names
is always lowercase if for multiple words. ie: package foobeanpackage

That's the prevalent style in the api anyway.



Report this thread to moderator Post Follow-up to this message
Old Post
Cid
07-29-04 02:09 AM


Re: JSP/Bean basics
On Wed, 28 Jul 2004 07:44:29 -0400, Cid <cid@noSuchAddress.no> wrote:

> On Wed, 28 Jul 2004 05:31:50 GMT, Andrew Thompson
> <SeeMySites@www.invalid> wrote:
> 
>
> I think, rather, what you want is public visibility for the
> getter/setters, not the variable. Looked like your getX() setX()
> methods were are default (package) security. So JSP won't be able to
> see them since it isn't in fooBeanPackage. Try
>
> public int getX()
> public void setX()
>
> instead. While you're at it, I think the convention on package names
> is always lowercase if for multiple words. ie: package foobeanpackage
>
> That's the prevalent style in the api anyway.
>

I believe it's not even convertion anymore.
it has changed in 1.2

Report this thread to moderator Post Follow-up to this message
Old Post
Patrick
07-29-04 02:09 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:26 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.