Saturday, June 15, 2013

jsp:javaBean- Action tag in JSP


    Hi All,
    Welcome to Java-recent.
    In this post we will discuss about jsp:useBean ,important Action element.
    Agenda:-
    • jsp:useBean tag
    • jsp:setProperty
    • jsp:getProperty
    jsp:useBean :-
  • used to instantiate and access a Java Bean methods, set variable values, parameter values etc.
  • used to minimize java code in a JSP page,which helps in separating presentation and business logic
Before going to use jsp:useBean lets know what is a Java Bean
Java Bean is nothing but a reusable component with instance variables,getter and setter methods.

Definition from Wikipedia
JavaBeans are reusable software components for Java. Practically, they are classes that encapsulate many objects into a single object (the bean). They are serializable, have a 0-argument constructor, and allow access to properties using getter and setter methods.

A Java Bean should have
  • No-arg constructor
  • should have setter and getter methods for all instance variables
  • Should not be abstract
  • Implementing Serializable interface will avoid exceptions

Example for Java Bean:-

public class EmployeeBean implements Serializable {
private String empId;
private String empName;
private String empMailId;
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpMailId() {
return empMailId;
}
public void setEmpMailId(String empMailId) {
this.empMailId = empMailId;
}
}




Instantiating JavaBean uisng jsp:useBean:-
Syntax :-
< jsp:useBean id= "instanceName" scope= "page | request | session | application"
class= "packageName.className" type= "packageName.className"
beanName="packageName.className | <%= expression / >

Attributes of jsp:useBean :-

Jsp:useBean Attributes
Usage
id It’s a unique identifier, same as reference name to an object in a class. This will be used to get the elements of the bean
Scope
Determines the bean scope for more on this topic read my JSF managed bean scopes
By default scope will be page
Class Fully qualified name of the class ,instantiates the bean class
Type Fully qualified name of the class

Mandate attributes are id and class .

Example JSP page :-

<jsp:useBean id="employeeBean" class="com.javabeans.EmployeeBean" scope="session">

<!--setting values-->
<jsp:setProperty name="employeeBean" property="empName" value="Ram"/>
<jsp:setProperty name="employeeBean" property="empMailId" value="Sudheer@javarecent.com"/>

</jsp:useBean>

<!-- Retrieving values and printing -->
<p>EmployeeName :- <jsp:getProperty name="employeeBean" property="empName"></jsp:getProperty></p>

<p>EmployeeMailId:- <jsp:getProperty name="employeeBean" property="empMailId"></jsp:getProperty>



Accessing Java Bean properties:-

    Jsp:setProperty :- is used to set values for Java Bean properties.
  • name attribute is holder for reference name
  • property attribute is holder for JavaBean property
  • value attribute assigns value to property referred

Syntax:-
<jsp:setProperty name="instanceName" property="Bean property name" value="value to property"/>

    This is similar to instanceName.setMethod();

Retrieving values from a Java Bean :-
    Jsp:getProperty :- is used to retrieve values from bean and the value in jsp page.
  • name attribute is holder for reference name
  • property attribute is holder for JavaBean property

Syntax:-
<jsp:setProperty name="instanceName" property="Bean property name"/>

    This is similar to instanceName.getMethod(); like employee.getName();
    Deploy the above mentioned code into web server like Tomcat
    Output of the above jsp code will be like:-

EmployeeName :- Sudheer
EmployeeMailId:- Sudheer@javarecent.com






Happy Learning

Please provide your valuable comments on this article and share it across your network.


No comments:

Post a Comment

Like and Share