Thursday, August 14, 2014

JSP JavaBeans

What is JavaBean ?

JavaBean is a special Java class which is written according to the JavaBean API. There are conventions for JavaBeans.

  • The class should be serializable.
  • The class must have a public constructor (no arguments). 
  • It can have getter and setter methods for every property in the class.

JavaBean naming conventions

There is a post for Java naming rules in this blog. You can refer it for more details.

Example for JavaBean

package user;


public class JavaBeanExample implements Serializable{
    private String name=null;
    public void setName(String name){;
    public String getName(){
        return name;

JavaBean with JSP

You can access JavaBeans through JSP. To do this task we use jsp:useBean action with few attributes. Here is an example that shows how to add useBean action into your JSP page.

<jsp:useBean id="Bean ID" scope="Bean's scope"/>

jsp:useBean attributes

There you have seen what are the attribute that can be used with jsp:useBean. There are few rules that you need to follow writing jsp pages.

  • id attribute is mandatory. This is the unique identifier of the bean.
  • scope attribute is optional. Default value is page. You cannot use session scope if session attribute of page directive is set to false.
  • Other three attributes can be used according to following combinations. At least one of them should be mentioned.
      1. class
      2. type
      3. class + type
      4. beanName + type

jsp:setProperty action

In JavaBean class there are setter and getter methods as I mentioned before. This setProperty action is used set methods. Here you can see how to use it in JSP.

<jsp:setProperty name="bean's id" property="property name" value="value">

jsp:setProperty attributes

As you can see there are only four attributes in jsp:setProperty. You need to understand few things before step forward into jsp:getProperty.

  • name attribute is mandatory. Because this is used to identify the bean. This value should be same as the id of the jsp:useBean action.
  • property attribute is also mandatory. It indicates the property of the bean to be set. You can set all the properties to respect values using " * " for property attribute. (property = "*")
  • value is optional attribute. Look at the following codes. Both are same.

<jsp:setProperty name="userName" property="firstName" value="Ann">

Above code is equal to following scriplet code.

<% userName.setFirstName("Ann") %>

  • Instead of using value attribute it can be used  param attribute to define requested parameters. Following codes are equivalent.

<jsp:setProperty name="userName" property="firstName" param="newName">

Above code is equal to following scriptlet code.

<% userName.setName(request.getParamter("newName")) %>

jsp:getProperty action

This is used to retrieve and print values of the bean. You can use it as follows.

<jsp:getProperty name="beanInstanceName" property="propertyName">

jsp:getProperty attributes

Following JSP code and Scriptlet  codes are equal.

<jsp:getProperty name="userName" property="firstName">

This is equal to, 

<% out.print(userName.getFirstName()); %>

Now it is the time to see what really happens in coding. Here is an example that demonstrate how to use JavaBeans with JSP.

package factory;


public class EmployeeBean implements Serializable{
    private String name = null;
    private String id = null;

    public String getName(){
        return name;
    public String getId(){
        return id;
    public void setName(String name){ = name;
    public void setId(String id){ = id;


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
        <h2>Employee details</h2>
        <form method="post" action="employeeJsp.jsp">
            <label>Name :</label> <input type="text" name="name"></br>
            <label>ID :</label> <input type="text" name="id"></br>
            <input type="submit" value="Submit">


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="factory" class="factory.EmployeeBean" scope="session"/>
<jsp:setProperty name="factory" property="name"/>
<jsp:setProperty name="factory" property="id"/>
Instead of using above two statemens, you can use following code
    <jsp:setProperty name="factory" property="*"/>

<!DOCTYPE html>
        <h1>Hi <jsp:getProperty name="factory" property="name"/> ! </h1>
        <h2>Your ID is <jsp:getProperty name="factory" property="id"/> </h2>
        Instead of using jsp:getProperty, you can use following code
            <%= factory.getName() %>
            <%= factory.getId() %>

Download project file