In this post, we will discuss JavaBean. 

Before, let us look at the JSP Model1 architecture.

JSP Model1 architecture

We already made a DTO class in Java, and that is JavaBean.

JavaBean is a component written in Java. 

There are some things that you need to consider when you make a Javabean.

1. In JavaBean, the member variable is called property.
2. Property is a field for storing values created by declaring the access controller private.
3. Properties are used as intermediate data storage when the contents of the JSP page are stored in the DB or when the contents stored in the DB are printed on the JSP page.
4. set() method and get() method are used a lot in JavaBean, and they
mainly use public access modifiers.
5. The Java Bean file's storage location should be in the WEB-INF/classes folder.

 

 

We will make a java file called "SimpleBean.java" in the src folder.

You can easily write generate getters and setters by right-clicking the mouse.

SimpleBean.java

// JavaBean, DTO Class(Data Transfer Object)

package javaBean;

public class SimpleBean {
	
	private String msg;	    // Property

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
}

There are three action tags related to JavaBean.

Action tags related to JavaBean Description
<jsp:useBean id=" " class=" " scope=" " /> to locate or instantiate a bean class
<jsp:setProperty name=" " property=" " value=" "/> to save a property value in a JavaBean object
<jsp:getProperty name=" " property=" " /> to bring the property value from the JavaBean object

 

<Example>

MemberInfo.java (JavaBean class)

package madvirus.member;

import java.sql.Timestamp;

public class MemberInfo {
    
    private String id;
    private String password;
    private String name;
    private String address;
    private Timestamp registerDate;
    private String email;
    
    public String getId() {
        return id;
    }
    public void setId(String val) {
        this.id = val;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String val) {
        this.password = val;
    }
    public String getName() {
        return name;
    }
    public void setName(String val) {
        this.name = val;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String val) {
        this.address = val;
    }
    public Timestamp getRegisterDate() {
        return registerDate;
    }
    public void setRegisterDate(Timestamp val) {
        this.registerDate = val;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String val) {
        this.email = val;
    }
}

 

 

'Java > JSP' 카테고리의 다른 글

JSP) Model1 vs Model2  (0) 2022.09.15
JSP) JSP and Oracle  (0) 2022.08.31
JSP) Action tags - include  (0) 2022.08.31
JSP) Handling Exceptions  (0) 2022.08.30
JSP) Action tags - forward  (0) 2022.08.30

+ Recent posts