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" %>
<html>
<head>
    <title>JSP - Bean Property Demo</title>
</head>
<body>

<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"/>

</body>
</html>

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) {
        this.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;
    }

    @Override
    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 can I get current working directory?

A system properties named user.dir can be used if you want to find the current working directory of your Java program.

package org.kodejava.example.io;

public class CurrentDirectoryExample {
    public static void main(String[] args) {
        // System property key to get current working directory.
        String USER_DIR_KEY = "user.dir";
        String currentDir = System.getProperty(USER_DIR_KEY);

        System.out.println("Working Directory: " + currentDir);
    }
}

Result example:

Working Directory: /Users/wsaryada/Projects/kodejava

How do I get user home directory name?

package org.kodejava.example.lang;

public class UserHomeExample {
    public static void main(String[] args) {
        // This is the key that we used to obtain user home directory
        // in the operating system
        String userHome = "user.home";

        // We get the path by getting the system property with the 
        // defined key above.
        String path = System.getProperty(userHome);

        // We print your home path here
        System.out.println("Your Home Path: " + path);
    }
}

On my machine it print something like:

Your Home Path: /Users/wsaryada

How do I get operating system name and version?

package org.kodejava.example.lang;

public class OperatingSystemInfo {
    public static void main(String[] args) {
        // The key for getting operating system name
        String name = "os.name";
        // The key for getting operating system version
        String version = "os.version";
        // The key for getting operating system architecture
        String architecture = "os.arch";

        System.out.println("Name: " + System.getProperty(name));
        System.out.println("Version: " + System.getProperty(version));
        System.out.println("Arch: " + System.getProperty(architecture));
    }
}

Below is the example result of our program, of course it could be different from what you’ll see in your machine because it depends on the operating system that you use.

Name: Windows XP
Version: 5.1
Architecture: x86

How do I get operating system temporary directory / folder?

package org.kodejava.example.lang;

public class TempDirExample {
    public static void main(String[] args) {
        // This is the property name for accessing OS temporary directory
        // or folder.
        String property = "java.io.tmpdir";

        // Get the temporary directory and print it.
        String tempDir = System.getProperty(property);
        System.out.println("OS temporary directory is " + tempDir);
    }
}