How JavaBeans in Java Work

Introduction

This article explains how Java Beans work and form processing in JSP using Java Beans. The NetBeans IDE is used for the sample examples.

Form Processing using JavaBean

Forms are a very common method of interaction in web sites, it is a common way to interact with the users directly. JSP makes form processing especially easy.

It is a standard way of handling forms in JSP using a "bean". I already defined "Java Bean" in my previous article. So by using "setter" and "getter" methods of the Beans class, we create a user interaction form and display user properties as they provided.

For creating a form we need to follow a procedure in the NetBeans IDE.

Step 1

Open the NetBeans IDE.

NetBeans IDE

Step 2

Choose "Java web" -> "Web application" as shown below

Java Web App

Step 3

Type your project name as "UserDataForm" as in the following.

UserDataForm

Step 4

Now choose your Java version and server wizard as shown below.

version and server wizard

Step 5

Now delete the default "index.jsp" file and create a new HTML file named "userdata.html" and provide the following code.

This file is used to provide an interface to the user through which they enter his "name", "age" and "email-id".

userdata.html

<!DOCTYPE html>

<html>

    <head>

        <title>User Data Form</title>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <meta name="viewport" content="width=device-width">

    </head><center>

        <form method="post" action="printuserdata.jsp">

            <body bgcolor="khaki">

                <h1>Welcome User</h1>

                <table>

                    <tr>

                        <td>

                            <font color="magenta"<b>Enter Name:</b></font>

                        </td>

                        <td>

                            <input type="text" name="uname">

                        </td>

                    </tr>

                    <tr>

                        <td>

                            <font color="magenta"<b>Enter Age:</b></font>

                        </td>

                        <td>

                            <input type="text" name="uage">

                        </td>

                    </tr>

                    <tr>

                        <td>

                            <font color="magenta"<b>Enter Email-Id:</b></font>

                        </td>

                        <td>

                            <input type="email" name="uemail">

                        </td>

                    </tr>

                    <tr>

                        <td>

                            <input type="submit" value="Submit">

                        </td>

                    </tr>

                </table>

            </body>

        </form>

    </html>

Step 6

To collect user data, we define a Java class named "UserInfo" in the "user" package with the fields "uname", "uage" and "uemail" and we provide a setter and getter method for them.

UserInfo.java

package user;

public class UserInfo {

    String uname;

    String uemail;

    int uage;

 

    public String getUname() {

        return uname;

    }

 

    public void setUname(String uname) {

        this.uname = uname;

    }

 

    public String getUemail() {

        return uemail;

    }

 

    public void setUemail(String uemail) {

        this.uemail = uemail;

    }

 

    public int getUage() {

        return uage;

    }

 

    public void setUage(int uage) {

        this.uage = uage;

    }

}

Step 7

Now create a new JSP file named "printuserdata". This class is used to save user information in "UserInfo.java" and then display it to the browser. It depends on your choice, you can make both files separate, in other words one to save information and the other for displaying data. But due to code redundancy I used a single file.

printuserdata.jsp

<jsp:useBean id="user" class="user.UserInfo" scope="session" />

<jsp:setProperty name="user" property="*"/>

<html>

    <body><center>

        <h1>You Entered:</h1><br>

        <table border="2">

            <tr><td><b>Name:</b></td><td><font color="red"><%= user.getUname()%></td></tr>           

            <tr><td><b>Age:</b> </td><td><font color="red"><%=user.getUage()%></td></tr>

            <tr><td><b>Email-Id </b></td><td><font color="red"><%=user.getUemail()%></td></tr>

        </table>

    </body>

</html>

Step 8

Now our project is ready to run.

Right-click on the project menu, then select "Run". The following output will then be generated.

User Data Form

Step 9

Now enter the details in the given form as in the following. I provided some details for it.

Filling Detail

Step 10

Now on click on the "submit" button. The user information is displayed on the browser menu as in the following.

Output

Up Next
    Ebook Download
    View all
    Learn
    View all