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

OPERATION MANUAL FOR RS232 TO RS422/RS485 VICE-VERSA

INTRODUCTION: Milestone model LD-15U is a RS232,  to RS422/RS485 converter is designed for high-speed data transmission between computer system and or peripherals over Iong distance under high noise conditions. They provide dual line interface per signal. APPLICATIONS: Application for these converters can be for factory automation, Programmable logic controllers,, attendance recording systems, Barcode Readers, remote data transmission, remote terminals, EPABX etc. SPECIFICATIONS: Input: RS232-T×D, R×D, DSR (for control), GND (D9 Female connector) Output: RS422/RS485-Tx+, Tx-, Rx+. Rx-(D9 Male Connector). Each signal is protected by spike suppressor, fuse and has opto-isolation. Selection Switch: 1. Rear side has 2-way'SELECT' switch for selecting 2-Wire or 4-Wire mode in RS422/RS485 application. 2. (Optional) Rear side has 3-way'SELECT' switch for selecting AUTO Mode (No Handshake Signal), DSR+ Mode (+12V Control) and DSR-Model (-_12V Control) for co

Java Server page tutorial part 3: JSP Life Cycle

JSP Life Cycle: A JSP life cycle can be defined as the entire process from its creation till the destruction which is similar to a servlet life cycle with an additional step which is required to compile a JSP into servlet. Following are the steps followed by a JSP Container: ◦ Compilation  ◦ Parsing the JSP.  ◦ Turning the JSP into a servlet.  ◦ Compiling  the servlet.  ◦ Initialization ◦ Execution ◦ Cleanup Architecture: 1.       JSP Compilation: When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page.  If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page. The compilation process involves three steps: ◦ Parsing the JSP. ◦ Turning the JSP into a servlet.  ◦ Compiling the servlet.  2.       JSP Initialization: When a container loads a JSP it invokes the jspInit() method before servicing any requests.  If you need to perform

JSTL-JSP Standard Tag Library part 1

Evolution of JSTL: Java is a flexible, general-purpose programming language, JavaServer pages(JSP) depends on java but hides some of the hard details of writing full- fledged programs.  The JSP Standards Tag Library(JSTL) builds on top of JSP, making it even easier to use. Architecture : In this figure large web applications are designed using java, JSTL, and othe components like databases. In large applications, it's common for requests from web browser to be handled by a java program called a servlet, which interacts with databases and other Java code on the server. The servlet figures out how it wants a response be printed and then forwards the user to the right JSTL page, which takes care of nothing more than presenting information. 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 c