0% found this document useful (0 votes)
17 views

Servelets JSPTopicsJavaBeans JSP

Uploaded by

abhigoswami9729
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Servelets JSPTopicsJavaBeans JSP

Uploaded by

abhigoswami9729
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Using JavaBeans in

JSPs Pages
Introduction

1
Introduction to Java
Beans
• A Java Bean is a reusable software component that works with Java

• Any Java class that follows certain design conventions can be a


JavaBeans component

• It must follow certain conventions about method naming,


construction, and behavior
– Must have a public default constructor
– Properties must be declared private
– Properties must be accessible using get, set, and other methods
(accessor and mutator methods) obeying a standard naming
convention

• JSP technology directly supports using JavaBeans components with


JSP language elements

• 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

• Lets you load in a JavaBean to be used in the JSP page

• The simplest syntax for specifying that a bean should be


used is:
<jsp:useBean id="name" class="package.class" />

• This usually means "instantiate an object of the class


specified by class, and bind it to a variable with the name
specified by id."

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

• Has two attributes:


name="beanInstanceName"
• The name of the Bean instance as declared in a
<jsp:useBean> tag
property="propertyName"
• The name of the Bean property whose value you
want to display

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" />

<jsp:setProperty name="person" property="empID"


value="999"/>

<jsp:setProperty name="person" property="empID" value


= "<%= request.getParameter("Employee-Id") %>"/>

<jsp:setProperty name="person" property="name"


param="username" />

<jsp:setProperty name="person" property="empID" />

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

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

1. Identify the implicit objects in JSP


a. request b. response c. out d. session e. application

2. Is JSP page extensible?(TRUE/FALSE)

3. JSP handles runtime errors using attribute in


page directive.

4. How do I use comments within a JSP page?

13 © 2012 WIPRO LTD | WWW.WIPRO.COM


13

You might also like