JavaBean JSP Example


One of the best advantages of JSP is the usage of Java beans. JSP can make of JavaBean in its code. JSP links with JavaBean with jsp:useBean tag. For this reason, JSPs are much used in frameworks like Struts.

1. What is JavaBean? or
2. What are JavaBean API specifications?

  1. It is a simple Java public class.
  2. There will be a default (no-args) public constructor.
  3. Properties are given in the form of instance variables.
  4. Setter and getter methods, called implicitly, to set and get the values from bean properties.
  5. Naming conventions should be followed like setMarks() and getMarks() for marks property variable.

3. What are advantages of JavaBean and how to use JavaBeans in JSP?

  1. If once jsp:useBean is declared, the JavaBean becomes a component of JSP file. All the scriptlets and expressions can make use of JavaBean properties.
  2. JavaBean properties can written with setter methods and read with getter methods.
  3. sets the property by calling set method. This property is known as mutator (which can mutate the property value).
  4. gets or retrieves the property by calling get method. This property is also known as accessor (which can access the property value).
  5. JavaBean propertiese can be read, write, read and write. It is very simple to make them. setter methods do not exist, JavaBean becomes read-only and if getter methods do not exist, JavaBean becomes write-only, if both setter and getter methods exist, it becomes read and write.
  6. With JavaBeans, reusability increases many fold. To say, JavaBean is a reusable software component.
  7. A few extra methods also can exist and these methods should be called explicitly.
  8. JavaBeans can be integrated to form an application.
  9. JaveBean should be serialized by implementing Serializable interface. But, here it is not required as we are not writing JavaBean to a file or sending to client across distributed network.
  10. The development team can be divided into two groups. Java experts group developing Java beans and HTML experts designing Web application.
  11. MVC architecture uses Java beans and JSPs.

Syntax linking JavaBean in JSP

<jsp:useBean id="bean_alias_name" class="package.bean_actual_name" scope="page/request/session/application" />

The bean can be called with alias name in JSP file. The id attribute should be an unique name. The class attribute gives the actual name of the JavaBean along with package declaration. JavaBean should be placed always in a separate package as required by most of the Web servers. The scope of JavaBean can be anyone of the above four.

Generally, Programmer uses Scriptlet to write some processing code. But this processing can be used by that JSP file only but not by other JSPs requiring same functionality. Reusability goes down. With JavaBean, the processing code or business logic is shifted from Scriptlet to JavaBean. This JavaBean can be used by any JSP. JavaBean increases reusability, flexibility and easier to debug the code. With this knowledge, let us go for an example.

The application comes with 3 programs. HTML file that takes user name and password and sends to JSP. The JSP does not have any validation code, but is available with JavaBean. The JSP sends the client data to JavaBean. JavaBean validates and the result is sent back to JSP. In turn, JSP sends the result of validation to client.

Example on JavaBean JSP

1. Client File Name: LogIn.jsp

  

Using Java Beans with JSP

Enter User Name
Enter Password

The HTML reads user name and password from client and sends them to Receive.jsp

2. Server-side File Name: Receive.jsp

 
  

  
  

  You entered user name as  
You entered user password as

You are a <%= snr.validate("Rao", "java") %> user.
Thank You

3. Server-side File Name: ValidateBean.java

package pack;
public class ValidateBean
{
  String user;
  String pass;

  public ValidateBean( ) {   }

  public void setUser(String user)  				
  {
    this.user = user;
  }
  public String getUser( )
  {
    return user;
  }

  public void setPass(String pass)  				
  {
    this.pass = pass;
  }
  public String getPass( )
  {
    return pass;
  }

  public String validate(String s1,String s2)
  {
    if(s1.equals(user) && s2.equals(pass))
      return "VALID";
    else
     return "INVALID";
   }
}

Screenshot of LogIn.html when some values are entered

JavaBean JSP

Screenshot of response sent by Receive.jsp

JavaBean JSP

JavaBean JSP Code Explanation

<jsp:useBean id="snr" class="pack.ValidateBean" />

The jsp:useBean action tells the container that this JSP file uses a bean by name ValidateBean available in the package called pack. That is, the name of the bean used is given in the attribute "class". This ValidateBean can be referred as "snr" anywhere in the JSP file. snr is the alias name of ValidateBean. The alias name is given in id attribute of jsp:useBean tag.

<jsp:setProperty name="snr" property="user" />
<jsp:setProperty name="snr" property="pass" />

The jsp:setProperty action tells the container to set the value for user and pass instance variables (known as properties) of ValidateBean class by calling setUser() and setPass() methods. That is, property="user" instructs container to call setUser() method to set a value for variable user of ValidateBean (referred by alias name snr).

The above two statements can be replaced with one as follows.

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

"*" indicates to call all set methods to set values for all properties, user and pass variables.

<jsp:getProperty name="snr" property="user" />
<jsp:getProperty name="snr" property="pass" />

The getProperty action calls get method and gets the value of the property. The first statement calls getUser() method and retrieves the value of the variable user (set earlier with set method) and directly puts in the output stream of client.

<%= snr.validate("Rao", "java") %>

The above statement calls validate() method of ValidateBean and checks the user name and password entered by the user with Rao and java. The result of validation is returned to JSP expression which sends to client.

Note: Compile the ValidateBean with package notation of -d and copy the generated package pack to the classes folder of Tomcat/Weblogic.

    Know Differences

  1. 11 Differences between JSP include directive and include action
  2. 10 differences between include action and forward action in JSP
  3. Difference between JSP forward and redirect Table

JSP View All

2 thoughts on “JavaBean JSP Example”

Leave a Comment

Your email address will not be published.