Bean Classes in Java Web Project

What are bean classes in java?

Java beans are the classes with the property of nullary constructor, serializable, and allow access to their properties(members) using get and set methods.

Example of Bean class

package com.travelandtour.beans;

public class BusBean {
    private String BusNumber;

    public String getBusNumber() {
        return BusNumber;

    public void setBusNumber(String busNumber) {
        BusNumber = busNumber;


Why do we use the bean classes in java?

A nullary constructor helps to initialize an object implicitly to an invalid state. Serialization is converting a java object into a byte array so it can be preserved.

Usually, when the execution of the java program is over, objects get destroyed. But due to serialization, those objects will be preserved in a memory buffer/file.

Why variables are private in bean classes?

In bean class private variable as the name indicates that this keyword in java provides privacy or protection to our data, we can create these personal data and protect them by private keyword so that no other class can access till you give them some option to do so.

There are two basic ways to give access to private data on the basis of our choice of us

Fully private data of a class: 

  • Only private variables
  • No public methods to access them.

Private data with access:

  • Private variables
  • Public methods so that others can access them.

Create and use BeanClasess in Java Project

Define bean (ImageBean.Java) is a simple java bean class that contains some private variables like ID, name, upload image, and getter, and setter methods for these variables.

package com.Image.Bean;

import java.sql.Blob;

public class ImageBean extends BaseBean{

      private long id;
      private String name;
       private Blob UploadImage;
    public long getId() {
        return id;
    public void setId(long id) { = id;
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public Blob getUploadImage() {
        return UploadImage;
    public void setUploadImage(Blob uploadImage) {
        UploadImage = uploadImage;

UseBean in JavaProject

populate been is a method under the servlet that takes requests from the front end and converts these image attributes (id, name) into objects using Imagebean.

protected BaseBean populateBean(HttpServletRequest request) {
  	   ImageBean bean = new ImageBean();
		Blob blob=null;
		Part filepart;
           try {
               filepart = request.getPart("uplodeimage");
               blob = medicinePacketUpload(filepart);
           } catch (Exception e) {
               // TODO Auto-generated catch block
            //Upload material method called

       populateDTO(bean, request);


       return bean;

How to auto-generate getters and setters for bean classes in Java

  • Define the variable name
  • Right-click on the class
  • Go to “Source”
  • Click to Generate Getter and Setter
  • Select the variable from the list
  • Click to generate