Skip to main content

JSP tutorial part :16: JSP ACTION TAGS




JSP ACTION TAGS

 JAVA BEANS
Uses of JSP Constructs:
• Scripting elements calling servletcode directly
• Scripting elements calling servletcode indirectly (by means of utility classes)
• Beans
• Servlet/JSP combo (MVC)
• MVC with JSP expression language
• Custom tags
• MVC with beans, custom tags, and
a framework like Struts or JSF
Creating dynamic content in JSPs
The commonly used JSP tags for creating dynamic content within a
JSP are :

jsp:useBean
jsp:getProperty
jsp:setProperty
About Java beans:
A JavaBean is a Plain Old Java object (POJO) that isSerializable, has a0-argument i.e.no-argconstructor, and allows access to its properties usinggetter and setter methods.
Java Beans are Java classes that obey the following conventions:
–Must have a zero-argument (empty) constructor
•You can satisfy this requirement either by explicitly defining such a constructor or by omitting all constructors

–Should have no public instance variables (fields)
–Persistent values should be accessed through methods called getXxx
•If class has method getTitle() that returns a String, class is

said to have a String property named title
•Boolean properties may use isXxxinstead of getXxx

Note : It is the name of the method, not instance variable that matters!


Bean Properties


•Usual rule to turn method name into property name
–Drop the word “get” or “set” and change the next letter to
lowercase.
• Method name: getFirstName
• Property name: firstName
• Exception 1: booleanproperties
–If getter returns booleanor Boolean(wrapper class)
• Property name: prime
• Exception 2: consecutive uppercase letters
–If two uppercase letters in a row after “get” or “set”
• Method name: getURL
• Property name: URL (not uRL)






Examples:
Method              NamesProperty    NameExample            JSP Usage
getFirstName       firstName           <jsp:getProperty… property="firstName"/>

setFirstName                                   <jsp:setProperty… property="firstName“                                                                                                                                value=“propertyValue”/>

isExecutive           executive            <jsp:getProperty… property="executive"/>

setExecutive                                   <jsp:setProperty… property="executive“ value=“propertyValue”/>
(booleanproperty) 

getZIP                      ZIP                  <jsp:getProperty… property="ZIP"/>

setZIP                                            <jsp:setProperty… property="ZIP” value=“propertyValue”/>

Note: method names are derived from property names.

Using Beans : Basic tasks:

jsp:useBean
–In the simplest case, this element instantiates a java bean, or to locate an existing bean instance, and assign it to id.
It is used as:
<jsp:useBeanid="beanName" class="package.Class”/>
jsp:setProperty
–This element modifies a bean property (i.e., calls a setXXXmethod).
It is used as:
<jsp:setPropertyname="beanName"
property="propertyName"
value="propertyValue" />
jsp:getProperty
–This element reads and outputs the value of a property.
It is used as:
<jsp:getPropertyname="beanName"
property="propertyName" />

The <jsp:useBean> action tag:

We can also specify the life time of the object by giving it a specific scope.
The <jsp:useBean> action tag ensures that the object is available, with the
specified id, in the appropriate scope as specified by the tag.
Syntax:
<jsp:useBeanattributes>
Ex.
<jsp:useBeanid="regform"class="RegFormPack.RegForm“scope="session"/>
Once a bean class is loaded, we can use
jsp:setPropertyandjsp:getPropertyactions to modify/retrieve bean properties.

Attributes of <jsp:usebean> tag:

Attributes of the <jsp:useBean> Tag:
Id:The id attribute of the jsp:useBeanaction tag represents the variable name assigned to the id attribute of the jsp:useBean.
scope:The scope attribute represents the scope in which the bean instance has to be located or created. scopes can be page, request, session or application.
page :It means that we can use the Bean within the JSP page.
request:We can use the bean from any JSP page processing the same request.
session:We can use the bean from any jsppage in the same session as the jsppage that created the bean.
application:We can use the bean from any page in the same application as the jsppage that created the bean.
class:It takes the qualified class name (package.classname) to create a bean instance if the bean instance is not found in the given scope.

Comments

Popular posts from this blog

Applications of Insulating Materials

All electrical systems require some kind of insulation to prevent short circuits and leaking currents. 3 forms of insulators: solid, liquid and gaseous Performance of these insulators depend on the temperature Classification according to their temperature rating. Impregnation: Letting the solid material absorb some liquid With the advent of new materials, the classification has been modified by International Electrotechnical Commission: The transformer insulation: (a) conductor or turn-to-turn insulation, (b) coil-to-coil insulation, (c) low voltage coil-to-earth insulation, (d) high voltage coil-to-low voltage coil insulation, and (e) high voltage coil-to-ground insulation. Transformer oil -- provides the required dielectric strength and insulation -- cools the transformer by circulating itself through the core and the coil structure. -- should be in the liquid state over the complete operating range of temperatures between -40°C and+50°C. -- gets o...

MULTILEVEL INVERTER

                                       MULTILEVEL INVERTER Historical Review From the late nineteenth century through the middle of the twentieth century, DC-to-AC   power conversion   was accomplished using   rotary converters   or   motor-generator   sets (M-G sets). In the early twentieth century,   vacuum tubes   and   gas filled tubes began to be used as switches in inverter circuits. The most widely used type of tube was the   thyratron . The origins of electromechanical inverters explain the source of the term   inverter . Early AC-to-DC converters used an induction or synchronous AC motor direct-connected to a generator (dynamo) so that the generator's commutator reversed its connections at exactly the right moments to produce DC. A later development is the synchronous converter, in which the motor and generator windin...

JSTL-JSP Standard Tag Library part 2

Evolution of JSTL-JSP Standard Tag Library As JSP grew in popularity, it became clear that different sites' custom tags fell into familiar, frequently used patterns. For example, many pages needed to loop over data to print tables and lists. Vendors of JSP containers and members of the open-source community tried to address these needs by providing collections of tags --tag libraries--that solved common problems. While many JSP page authors used these libraries, nothing tied them together. To address this fragmentation, the Java Community Process --the same group that now maintains and standardizes JSP itself --decided to offer a standard tag library. JSTL 1.0, to include tags for the following common tasks:          •Looping over data to produce tables, lists, etc.          •Conditional operations          •Importing and processing data from other web pages         ...