When to make a class a bean?

I can understand that if you have a GUI type class where you might want to make the class a Bean so that it can be used in tools like BeanBox.
But what if it is a non visible class? For example say a class that is used to interface to piece of equipment over the serial port? Are there any advantages to making the class a Bean?

Ok. My info may be a little out of date, but I'll try.
In object oriented programming, we have these things called patterns. They are called that because that's what they are caled in the original book by the "gang of four", whose names I can't recall. An example of a pattern is the idea of passing an event around, or having graphical objects "inside" other ones, or making a class that serves as an interface to a complex set of other classes. These patterns are not always directly supported by the APIs or language, they are more abstract than that - ways that we programmers go about architecting solutions to certain kinds of problems. (nb: a pattern is not the same thing as an algorithim). Of course, some patterns are supported directly by the APIs: EventObject, EventListener etc.
One common pattern is an object that holds a set of values, that fires events and so on. Of course any object can do this, but architecturally speaking, some objects work that way more naturally than others. In Java, we call such an object a JavaBean.
Java supplies a standard for JavaBeans at http://java.sun.com/products/javabeans/docs/spec.html . This standard specifies that a java bean has a null argument constructor, it has getters and setters and that if a bean has a getFoo() method and a setFoo() method, then these together mean that the bean has a "property" named Foo, and so on.
Now physically, the only thing that a java bean must have is:
it must be public,
it must have a public, no-argument constructor
However, architecturally, we make a load of assumptions. For instance, if your class has a getFoo() and setFoo() method, but they have absolutely nothing to do with each other, then you class isn't a JavaBean, even though physically the compiler and tools have no way to know this. Likewise, if your class has a public no-arg constructor, but your class requires that you call the setup() method for it to work properly, then it is not a JavaBean. JavaBeans can be initialised into a reasonable starting state with the constructor alone.
So JavaBean or not JavaBean is not a class you inherit or an API you use, it's a way of doing things.
Having said that, there is api support for JavaBeans in the java.bean package. You can create "BeanInfo" classes for your beans that contain metadata about the beans: long textual descriptions for the properties, icons for tools to use when displaying the beans and so on.
As for JSPs:
From memory, you can tell your JSP that an instance of a bean is to be created for each session that the JSP deals with. So say we make a bean that acts as a shopping cart, with items that themselves are beans (item number, quantity). Inside the JSP you can do things like "print shopping cart.item[2].name", and the JSP will know to convert this to a call to foobean.getItem(2).getName(). The reason for making it a bean is to make it possible for the web server to manage the creation of these things and the retreiving of them on a session-by-session basis. They can also be passed as arguments and so on. Another nice reason to use a bean is that you can encode derived values as getter methods on the bean (eg: getTotalPrice()). This is a nice place to put such logic.
The JSP spec is at http://www.jcp.org/aboutJava/communityprocess/final/jsr053/ . (dont confuse it with the servlet Spec, which is on the same page). See section 4.1: Standard Actions/usebean.

Similar Messages

  • When to make a different class

    I am just getting started doing programming in general and one thing i dont get is when you make different classes and why any code or tutorials would help alot.

    ok thanks for the tutorials. can you give like the names of the classes i would need to make a pong game and an outline of what should go in there e.g. class: player paddle controls for movement detecting when the ball hits ect.

  • Problem importing classes and beans

    Hey there. Im having one major fustrating problem! When I code supporting classes and beans for my JSPs I get a code 500 internal server error when trying to import (via <%@ page import="class" %> and <jsp:useBean/>) Im storing my classes and beans in the WEB-INF folder and the calling JSPs are located in /ROOT/tests/8/jsp.jsp. Im using the following to import a class or bean:
    <%@ import="aClass" %>
    Seen as tho its in the WEB-INF folder I won't have to explicitly refer to where the class is located, just the class name.
    I never had this problem when I was using my hosting service. Its only on my localhost server in which I get the Internal Server error.
    Help appreciated, thx.
    PS: Im quite new to JSP/Java Servlet.

    import (via <%@ page import="class" %> and
    <jsp:useBean/>) Im storing my classes and beans in the
    WEB-INF folder try put your class file in WEB-INF/classes.
    or first put bean in the package, like WEB-INF/classes/packagename/beanclass
    in jsp page:
    <jsp:useBean id="Mybean" class="packagename.beanclass" scope="request" />
    Question: is /ROOT a context entry in your server.xml?
    Which JSP Container (version) you use? Maybe your localhost server's set up is different with your hosting.

  • When to use Custom Adapter, Event Beans and Spring beans

    Hi,
    I have basic question on when or on what basis/scenario the Custom Adapter, Event Beans or Spring beans should be used.
    All three has same implementation methods.
    Please help me in understanding when to use Custom Adapters, Event Beans or Spring beans.
    Thanks,
    Sri
    Edited by: 897694 on Mar 7, 2012 8:12 AM

    custom adapters:
    you have to use a custom adapter when you don't find any adapter in ocep which will serve your purpose.
    Most of the times you use an adapter to connect the application with external things like http server, etc It can be both to listen the events or to send the events outside.
    Event Beans:
    This type of beans does most of the work that an adapter code does but in convention beans are not used to connect the application with external stuff
    You can use Beans as event sink etc In this case using an using an adapter is not suggestable.
    If you implement Interfaces like Initializing Bean for an adapter code it won't work properly, you have to use an event Bean in that case.
    Spring beans:
    Spring beans are used when the bean code doesn't implement EventSink or EventSender.
    These beans are used to enable something (eg: make ${wlevs_dev_home}/abc/a/b.java this path is not understandable by java code unless you override the path using a spring bean)
    Most of the things which doesn't need a connection between components in the application (eg cache loader: you can do this using spring beans and event beans both but spring beans doesn't need a connection with cache)
    Hope this small writeup helps you :)
    Edited by: Patha on Mar 8, 2012 9:10 AM

  • Error(2): Unable to find class for bean: connection defined by tag...

    I created a project, and a JavaBean that connects to the database and I want to use it in a JSP page that exists in my project.
    Bean:
    package BeerPackage;
    import java.sql.*;
    public class ConnectionBean
    JSP:
    <%@ page import="BeerPackage.*" contentType="text/html;charset=windows-1252"%>
    <jsp:useBean id="connection" class="ConnectionBean" scope="session"/>
    Then I get the error:
    Error(2): Unable to find class for bean: connection defined by tag with class: ConnectionBean
    When I try to build the JSP
    Any ideas?

    Seems even though I have an import, I still have to specify the package name in front of the useBean.
    What am I missing here?

  • How to make a class immutable?

    I hav attend the interview where they ask this question.
    I know little bit is Make the class final and declare all variables as static final .
    Can any Help me.
    This question

    Thi is just my opinion;
    An immutable object is an object that once created, it's values can no longer be changed.
    Thus an immutable object could be an object that allows it's values to be changed only when created with the use of the constructor, and only has get methods to retrieve those values (no set methods).
    @Peetzore
    Defining them as final is something I never did, however it makes sense :) and will start doing so as well
    Regards,
    Sim085

  • How to make a class an array

    Hello, i am very confused as to how to make an array inside a class and use it from another class. For example, when making a "Book of hotties" where i would create entries in a book(objects) of hot women ,with parameters
    name, last name, phone number, and rating, and i am supposed to make a class for the book itself with a Person [ ] blackBook, and also a separete class called Person where i would create each person. How would i go about creating this array and where would i create it? and from that how can i access it in order to perform methods like sorting and checking entries? Tnx in advance.
    Edited by: Secmugen on May 5, 2009 6:19 AM
    Edited by: Secmugen on May 5, 2009 6:20 AM

    You probably need to start here: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
    There is also a Java collection called ArrayList which is suitable for storing and retreiving objects such as BlackBook, Person etc...
    To access elements of an array within a class you would most likely use a public getter method. eg.
    public class BlackBook {
       private ArrayList<Person> persons = new ArrayList<Person>();
      public ArrayList<Person> getPersons() {
         return this.persons;
      public Person getPerson(int index) {
        this.persons.get(index);
    };etc..

  • Error: Unable to find class for bean

    Dear all,
    please help help with the problem
    i've a jsp as follows:
    <@page import="java.util.*, com.abc.*" %>
    <jsp:useBean id="mybean" class="MyClass" />
    <% out.println("Testing bean"); %>
    where MyClass.class is located WEB-INF/classes/com/abc/MyClass.class
    it works fine in Oracle iAS with oc4j 1.0.2.1.1 (not 9iAS R2)
    when i upgrade to 9iAS R2 with oc4j 9.0.3
    it's complaining the error of
    OracleJSP: oracle.jsp.parse.JspParseException: Line # 8, <jsp:useBean id="mybean" class="MyClass" />
    Error: Unable to find class for bean: mybean defined by tag with class: MyClass
    then i changed the line <jsp:useBean....> to
    <jsp:useBean id="mybean" class="com.abc.MyClass" />
    it didn't complain the error
    anyone know that the syntax of specifying class must be coded as package.xxx.xxx etc?
    Thank you very much

    You should specify the fully qualified class name (i.e. with package structure) in the class attribute of useBean tag.
    Read the syntax of useBean tag here
    http://java.sun.com/products/jsp/syntax/1.1/syntaxref1115.html
    Chandar

  • 'invalid class-java beans not available for import

    Hi Experts,
    i am using java bean model in my Web Dynpro Application.
    for that one i have created command bean(Customer) and in my command bean i am using some other class(CustomerInfo) as reference. this CustomerInfo class contains 4 attribtes(name ,id ,etc  all are of type String)
    when i try to import these 2 class into web dynpro (using java bean model),for the Customer class it
    is giving an error message 'invalid class-java beans not
    available for import'.but 2nd class(CustomerInfo) is imported successfully with out giving the error.what might be reason.
    any help will be appriciated
    Thanks in advance
    With Regards
    Naidu

    Naidu,
    Please try to repeat JavaBean model import when running IDE in console mode (correspondign short-cut should be available in Windows Start menu, otherwise just copy original short-cut, edit it and change in command string "javaw" to java).
    Post here what is shown on console during import.
    VS

  • Problem when trying to delete CMP entity bean

    Hi,
    When I try to remove a CMP entity bean, I have this error :
    re09.projet_ejb.syndic.AdminException: Exception thrown from bean; nested exception is: javax.ejb.EJBException: nested exception is: com.sun.jdo.api.persistence.support.JDOUnsupportedOptionException: JDO76207: Update of a primary key field is not allowed.It's only happening when I try to remove a bean which has a relation with another bean. The relation type is One to Many. I tried to search on this forum to find an answer but I found nothing that could help me.
    Message was edited by:
    Starship
    Sry I just found the mistake, the cascade delete was not on the right place :s
    Message was edited by:
    Starship

    Got it sorted so I thought I would share the solution here:  The problem wasnt related to LR 5.2 but was to do with Mac OS X 10.8.5 AKA Mountain Lion.  I had also noticed that I was having an issue around deleting anything on my MAC.  When I tried to delete a file from anywhere I was asked to submit my password.  The message I got was finder wants to make a change please submit password.  I checked through the mountain Lion forums and found a solution here; https://discussions.apple.com/message/20499360#20499360%2320499360
    I implemented it and the problem has dissappeared.

  • Example of a WS returning a class(a bean)

    Hi Folks !
    I need an example of a WS returning a class(a bean).
    The class should have at least 2 attributes.
    Any help is very appreciated.
    Best regards !
    Ricardo

    Hello,
    Study the following code - it should help:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    * This is a fun applet to show how button events work.
    public class EventTest extends Applet implements ActionListener{
       Button shrinkButton, growButton;
       int size = 50;
        * init( ) creates two Button objects, adds an ActionListener
        * to each (this actionListener listeners for when the Button is
        * pressed) and then adds each Button to the applets Panel.
       public void init( ){
          shrinkButton = new Button( "Shrink");
          shrinkButton.addActionListener( this);
          add( shrinkButton);
          growButton = new Button( "Grow");
          growButton.addActionListener( this);
          add( growButton);
        * This method is called when a Button is pressed. It uses the
        * method e.getSource( ) to see which Button called this method.
       public void actionPerformed( ActionEvent e){
          if( e.getSource( ) == shrinkButton)
             size -= 10;
          else
             size += 10;
          repaint( );
        * This method is called from method actionPerformed( ) and
        * repaints the text at the appropriate size.
       public void paint( Graphics g){
          Font font = new Font( "Ariel", Font.BOLD, size);
          g.setFont( font);
          g.drawString( "Big or Small?", 200, 200);
    }I hope this helps.
    Best regards
    -Marek.

  • Looking for a JCalendar Class or bean

    I need to find a Calender class or bean that gives you a visual date selector, ie you can click through the months and select the day from a kind of date matrix, is there such a class out there freely available?
    I was hoping IBM may have such a class, if so does anyone know how i can get it?
    thanx

    Hi,
    I can send you two clasess (with code) for do this : an special JTextField (whoe present the user the date ) and when user click inside a JDialog with a Calendar.
    Send me an e-mail at [email protected]
    Bye

  • Cisco 2911 vesio 12,4 , i have some noise when i make call

    hi
    1: i have cisco  2911 with 2 card 4FXO i can make the call buth i heath some pertubation in the conversation
    2 : i can heat th ring when i make a external call
    this my configuration
    voice call carrier capacity active
    voice rtp send-recv
    voice service voip
    allow-connections h323 to h323
    allow-connections h323 to sip
    allow-connections sip to h323
    allow-connections sip to sip
    supplementary-service h450.12
    fax protocol cisco
    h323
      no call service stop
    voice class codec 1
    codec preference 1 g711ulaw
    codec preference 2 g729r8
    voice class h323 1
      h225 timeout tcp establish 3
    voice-port 0/0/0
    supervisory disconnect anytone
    cptone FR
    connection plar opx 0
    caller-id enable
    voice-port 0/0/1
    supervisory disconnect anytone
    no battery-reversal
    input gain -3
    output attenuation 4
    echo-cancel coverage 24
    no comfort-noise
    cptone FR
    timeouts interdigit 6
    timeouts call-disconnect 5
    timing hookflash-out 500
    connection plar opx 0
    impedance complex2
    caller-id enable
    best regard!

    Try this:
    1 - Use the default configuration for the port. Fw:
    voice-port 0/0/0
    supervisory disconnect anytone
    cptone FR
    connection plar opx 0
    caller-id enable
    voice-port 0/0/1
    supervisory disconnect anytone
    cptone FR
    connection plar opx 0
    caller-id enable
    2 - Do you hear noise in both ports?
    3 - Usually in the case of noise, the problem is not in the router of the beholder. Often the problem is the user of the remote end. The Other End. (Ask for verification on the other side too)
    4 - Check the qos is never enough.
    I hope I have helped.
    Luciane de Medeiros

  • Why my iphone 5 restart itself when I make a call on facetime?

    When I make a call on facetime, sometime my iphone 5 was hang, then black screen and restart itself.
    At the first, I think it's because my ios is 6.0 so I update to 6.0.1 but it still restart itself. What should I solve this problem?

    Thank you

  • I'm not techinal so please bear with me. I don't have the bar that allows me to type in a website. I did have a few minutes ago. When I make the page full screen and move my cursor to the top of the page, the bar drops down and I can use it.  help

    I'm not techinal so please bear with me. I don't have the bar that allows me to type in a website. I did have a few minutes ago. When I make the page full screen and move my cursor to the top of the page, the bar drops down and I can use it. I've tried restarting my computer, didn't work. Help!

    Well I figured it out, right click the arrow that makes the screen bigger and unclick the Hide Toolbar. I knew it couldn't be that hard.

Maybe you are looking for

  • Creation of Mail Forms -- From HTML page

    Hi all, I have a basic question wrt the creation of MAIL FORMS in SAP CRM. Our client creates Email communication content in HTML format. And the requirement is to use these HTML files for creation of Mail Forms (on a very frequent basis) While creat

  • Read in text file issue

    I'm working on a command line address book that reads in a file and then writes out a file. It won't read in the file (addbook.txt). I haven't tried the write out file. From everything I've been able to find...this is supposed to work. What am I miss

  • Using Apple TV with external DVD drive on Macbook Pro

    I am unable to play DVDs from my external hard drive attached to my Macbook Pro via the Apple TV on my Samsung smart TV. What am I doing wrong. Thanks.

  • How to upload html files using Weblogic Bulkloader

    I hav uploaded image files using bulkloader. however with html files am facing a prob. only the file structure gets created but there is no html file present inside. in Filename.html.md.properties i agv the followin nodeType=html lifecyclestatus=2 Fi

  • Removing a duplicate attachments prior to converting to PDF?

    I am creating PDF portfolios using email that requires archiving, these emails reside in Lotus Notes. Attachments are in the body of each email and once the file is converted, it will also appear just below the subject matter which then adds a duplic