JavaBean in JSP : a simple tutorial

Hi !

Purpose of this tutorial : To give the reader a better understanding of using Java beans in JSP.

Advantage : This tutorial serves as an instant learning tutorial for those who would want to learn about using  Java bean in JSP.

Sources : The code used in this short tutorial has been extracted from

What is a Java Bean ?
Java bean are classes written in the Java programming language conforming to a particular convention. A Java bean has a set of properties that can be read or changed. It is also possible to discover the properties a bea has available, which well suits beans for modeling all kinds of real world things that can also be described as a set of properties.


What we will be doing?

We will create a simple html page(getName.html) that is used to get user’s name, email and age.  When we submit that data, it invokes a JSP page(SaveName.jsp) that inserts the user entered data into a bean. We provide a “click here to continue” link in that same page, which when clicked would invoke another JSP page (NextPage.jsp) that retrieves the data that was inserted into the bean and displays it!

The implementation is simple and straight forward!

Tools needed:

I have tested this code successfully in Tomcat 6.0 configured suitably with jdk1.5.0_14 and jre1.5.0_14.

code :




Keep the above files in your webserver directory. My webserver classpath is C:\Tomcat 6.0\webapps\ROOT\ . Now we must develop a Java code to store the values obtained from the form. We then compile that Java code to obtain its “class” file. This class file is the real beauty of Java. Its contents look somewhat junk type when viewed using any editor. And that means that its not easy to hack your data from it(say, a password was sent from the form).

The contents of the Java code are very important. We define a Java class with fields “username”,”email” and “age” which must correspond exactly to the field names in getName.html. We define two types of methods in this Java code viz, a “setter” method and a “getter” method.

In simple,

* The setter method is a method that starts with the keyword “set” followed by the field name. In our example the setter methods would be setUsername,setEmail and setAge. Now compare this with the field names in getName.html. The only point you must note here is  the first letter, following “set”, is in uppercase. These setter methods insert the user entered data into the corresponding variable names into the bean.

At this point, by reasoning you should have understood that a bean is an instance of the Java class(correct me if i am wrong).

* The getter method is a method that returns the values stored in the javabean, to the entity that invoked it(NextPage.jsp in this case). The getter methods are named in the same way as setter methods except that the keyword “set” is replaced by “get”. So now we have getUsername,  getEmail, getAge

The java code is shown below:

Now compile the above Java code and place the code inside a folder named “user” because we have declared a java package by that name. This folder (along with the java class)should be placed in the following location:


For me its :

C:\Tomcat 6.0\webapps\ROOT\WEB-INF\classes\

If the folder “classes” is not present then create one.

That is it invoke the getName.html file from the browser after starting Tomcat server and see the code in action. JSP rocks!


See the output here , here and here.

What I have not explained here?

I have not explained the meaning of the java bean tags in the jsp code given about. Their meanings and pupose are quite intuitive,if not just google to find more info on them.

How could i make use of this idea?

You can use the idea gathered from this tutorial for designing a user login page using java beans. Creativity has no bounds.