How to set and get properties of a bean in JSP?

In this example you will learn how to set and get the value of Java object properties that you define in a JSP pages. For this example, let’s first start by creating a variable that we named customer, that will have a type of Customer class. To create this variable we use the <jsp:useBean> action.

After we create the customer variable we can set the property value of the customer bean using the <jsp:setProperty> action. And to get the property value of the customer bean we use the <jsp:getProperty> action.

The name attribute in the setProperty and getProperty action refer to our customer bean. The property attribute tells which property we are going to set or get. To set the value of a property we use the value attribute.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <title>JSP - Bean Property Demo</title>

<jsp:useBean id="customer" class="org.kodejava.example.bean.Customer"/>
<jsp:setProperty name="customer" property="id" value="1"/>
<jsp:setProperty name="customer" property="firstName" value="John"/>
<jsp:setProperty name="customer" property="lastName" value="Doe"/>
<jsp:setProperty name="customer" property="address" value="Sunset Road"/>

Customer Information: <%= customer.toString() %><br/>
Customer Name: <jsp:getProperty name="customer" property="firstName"/>
<jsp:getProperty name="customer" property="lastName"/>


And here is the code for our Customer bean. This bean contains property such as the id, firstName, lastName and address.

package org.kodejava.example.bean;

public class Customer {
    private int id;
    private String firstName;
    private String lastName;
    private String address;

    public Customer() {

    public int getId() {
        return id;

    public void setId(int id) { = id;

    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;

    public String getAddress() {
        return address;

    public void setAddress(String address) {
        this.address = address;

    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", firstName='" + firstName + ''' +
                ", lastName='" + lastName + ''' +
                ", address='" + address + ''' +

We we access the JSP page we will see the following output:

Customer Information: Customer{id=1, firstName='John', lastName='Doe', address='Sunset Road'}
Customer Name: John Doe
JSP Bean Property Demo

JSP Bean Property Demo

How do I get Java Home directory?

To get Java Home directory we can obtain it from system properties using the java.home key.

package org.kodejava.example.lang;

public class JavaHomeDirectory {
    public static void main(String[] args) {
        String javaHome = System.getProperty("java.home");
        System.out.println("JAVA HOME = " + javaHome);

On my computer this code give me the following output:

JAVA HOME = /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre

How do I get the username of operating system active user?

This example show you how to get operating system active user’s login name. We can obtain the username of current user by reading system properties using the key.

package org.kodejava.example.lang;

public class GettingUserName {
    public static void main(String[] args) {
        String username = System.getProperty("");
        System.out.println("username = " + username);