Servelets JSPTopicsJavaBeans JSP
Servelets JSPTopicsJavaBeans JSP
JSPs Pages
Introduction
1
Introduction to Java
Beans
• A Java Bean is a reusable software component that works with Java
• You can easily create and initialize beans and get and set the values
of their properties
2
Identifying the property of a bean
public class A1 {
private int id; What are the properties of this bean?
private String username;
• public
public void setName(String
String getName(){ x){ username=x;
• } return username;
}
• public void setEmpID(int y){ name
• id=y;
•}
• public int getEmpID(){ return id;
•}
empID
}
3
3
Actions: The jsp:useBean Action
4
3
Actions: The jsp:useBean Action (Contd.).
package p1;
public class Example1 {
private int age;
public int getAge(){
}
public void setAge(int i){
}
If we have to instantiate the given class in java, we will
}
use the following syntax :
p1.Example1 obj1 = new p1.Example1();
In jsp, the same result is achieved using jsp:useBean :
<jsp:useBean id=“obj1” class =“p1.Example1” />
5
3
<jsp:getProperty>
• Converts property names following the bean standards
6
3
Example on jsp:getProperty
package jspbean;
import java.util.Calendar;
public class TimeBean{
private int hour,minute,second;
public TimeBean(){
Calendar cal = Calendar.getInstance();
hour = cal.get(Calendar.HOUR);
minute = cal.get(Calendar.MINUTE);
second = cal.get(Calendar.SECOND);
}
public int getHour() { return hour; }
public int getMinute() { return minute; }
public int getSecond() { return second; }
public String getMessage(){
return "Time: "+hour+" "+minute+" "+second;
}
}
7
3
Example on jsp:getProperty (Contd.).
<HTML>
<HEAD><TITLE>Time Bean </TITLE></HEAD>
<BODY>
<jsp:useBean id="b2" class="jspbean.TimeBean" />
Hour=<jsp:getProperty name="b2" property=“hour" /> <BR>
Minute=<jsp:getProperty name="b2“property=“minute"/><BR>
Second<jsp:getProperty name="b2" property=“second"/><BR>
Message= <jsp:getProperty name="b2" property=“message"/>
</BODY>
</HTML>
8
3
<jsp:setProperty>
• Sets the value of one or more properties in a JavaBean component
Example:
<jsp:useBean id=“person" class=“p1.A1" />
9
3
Properties of JSP:setProperty
1. name
2. property
3. Value
name: This required attribute designates the bean whose property will be set.
The jsp:useBean element must appear before the jsp:setProperty element.
property: This required attribute indicates the property you want to set.
However, there is one special case: a value of "*" means that all request
parameters whose names match bean property names will be passed to the
appropriate setter methods.
•
value: This optional attribute specifies the value for the property. String values
are automatically converted to numbers, boolean to Boolean, byte to Byte,
char to Character via the standard valueOf method in the target or wrapper
class. For example, a value of "true" for a boolean or Boolean property will be
converted via Boolean.valueOf, and a value of "42" for an int or Integer property
will be converted via Integer.valueOf.
10 10
Example on jsp:setProperty
CircleBean.java
package jspbean;
public class CircleBean{
private double radius;
public void setRadius(double r) { radius = r; }
public double getArea() { return
Math.PI*radius*radius; }
}
Circle.jsp
<BODY>
<jsp:useBean id="cb" class="jspbean.CircleBean" />
<jsp:setProperty name="cb" property="radius"
value="2.0" />
Reduced Form: <jsp:getProperty name="cb"
property=“area" />
</BODY>
11 11
Quiz