FreeKB - Java bean in Eclipse - Getting Started
Java bean in Eclipse - Getting Started

Home > Search


A bean is simply a class that adheres to the following rules.

  • Has getters and setters methods (eg setFoo, getFoo)
  • A public no-argument constructor
  • Implements java.io.Serializable

For example, lets say you've created a dynamic web application that contains a package named com.sample.main.

 

And then you've created a class called People in the package.

 

Add the following to MyBean.java. This bean will display the text "John Doe".

package com.sample.main;

// implement java.io.Serializable
public class MyBean implements java.io.Serializable {

    // private variables
    private static final long serialVersionUID = -3774654564564563L;
    private String name;

    // constructor
    public MyBean() {
    	name = "Jane Doe";
    }
    public MyBean(String name) {
        this.name = name;
    }

    // getter
    public String getName() {
        return name;
    }
    // setter
    public void setName(String name) {
        this.name = name;
    }
}

 

The bean can be imported into a JSP page.

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 9d2bb in the box below so that we can be sure you are a human.




Comments