wood burning stoves 2.0
The moose likes JSP and the fly likes Using Beans in my classes... is it possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Using Beans in my classes... is it possible?" Watch "Using Beans in my classes... is it possible?" New topic

Using Beans in my classes... is it possible?

Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
I am learning JSP and at first beans sounded like a really cool idea in my JSPs... but when I actually had to write class files, I faced the problem that I can't use beans in there... Weell I guess there's a way.. just that I dont know it
How do u guys go about it?
+ What's EJB? Entity Java Beans? Enterprise Java Beans? Simply Beans? What do the first two mean? And can somebody tell me whats the difference between java EE and java SE? is the enterpirse one commercial or something?
Thank you!
Helen Crosbie

Joined: Mar 11, 2003
Posts: 26
Hi Ivan,
I'm fairly new to this stuff myself, but I can help you out with a few definitions. A bean is actually a class itself which must follow a few rules such as it must provide a no-parameter constructor and its properties must have getter and setter methods. So yes, you will be able to use beans in your classes in the same way you would any other class.
An EJB is an Enterprise Java Bean. This is a bean that can be used in a distributed, or enterprise system. There are session EJBs which usually do something and entity EJBs that are used to hold data, and communicate with a database, for example.
J2SE is the standard Java edition which contains all the stuff you would expect for developing a straightforward application. J2EE is the enterprise edition that contains all the support of an enterprise (distributed) system, including EJBs, CORBA and masses more.
I hope this has helped a bit!
Mehdi Chaouachi
Ranch Hand

Joined: Jul 02, 2003
Posts: 87
Can you please give some code so that we can advise you.

Mehdi Chaouachi<p>Sun Certified Java Programer (1.4)<br />Sun Certified Web Component Developer (1.4).<br />Sun Certified Mobile Application Developer.
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
That still doesnt answer my question: how would you use beans in your classes?
Whenever you use bean in servlet or JSP, its stored in app, session or page map, so it has somewhat global capabilities. But in your classes you do not have access to tomcat's app\session\page maps, thus you can't use those beans....
so how do u go about that?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 61994

First of all, remember that a 'bean' is just a Java class that follows certain conventions. Thus, you can 'use' beans wherever you can use Java classes.
You question may be: how do I access beans placed in the various web app contexts from my classes? perhaps...
If so, then you need to pass the context around. Let's say for example you have a bean in the session context. Since the bean is stored in the HTTP session you need to pass the session context to your class (or another means of obtaining it -- the servlet request instance, for example).
You can then use the getAttribute() method to get a reference to the bean and call its method just as any other class.
If that's not what your question is, then I don't understand what you are asking.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
yeah, but is there a more... convinient way to do it?
jQuery in Action, 2nd edition
subject: Using Beans in my classes... is it possible?