Professional Documents
Culture Documents
JavaBeans
JavaBeans is a software component architecture that extends the power of
the Java language by enabling well-formed objects to be manipulated
visually at design time in a pure Java builder tool, such as NetBeans and
Eclipse.
In general, a JavaBean is a class but it must following three rules:
1.
2.
3.
Note when coding a getter for return a Boolean value, you should use is
method instead of get method. For example, is Gradudated.
When coding the get, set and is methods, you must follow the capitalization
conventions. So each method name must start with a lowercase letter and
each property name must start with an uppercase letter. For exaple,
setFirstName().
Also, it is commeon for a JavaBean to implement the Serializable interface,
though it is not required. So another class can use the set, get, and is
method to read and write an object's instance variables to and form a
persistent data source.
It is common to use JavaBeans to dedfine the business object. And these
JavaBeans are clalled Invisiable JavaBeans. However, JavaBeans can be used
to define buttons and other user-interfaces controls.
has a more elegant and compact syntax than standard JSP tags.
lets you access nested properties.
lets you access collections such as maps, arrays, and lists.
does a better job of handling null values
provides more functionality.
Disadvantages of EL
doesn't create a JavaBean if it doesn't already exist.
doesn't provide a way to set properties.
Working with EL
Typically before you can use EL to access a javaBean object, a
Servlet needs to use setAttribute method to store objects in
either request or session scope (i.e. HttpServletRequest and
HttpSession respectively). Or even you can store the object
within page scope using implicit PageContext.
In JSP, you use the dot operator, the code to the left of the
operator must specify a JavaBean or a map, and the code to the
right of the operator must specify a JavaBean property or a map
key.
Syntax:
${attribute}
When you use this syntax, EL looks up the attribute starting with
the smallest scope (page scope) and moving towards the largest
scope (application scope).
Search sequence
1.
2.
3.
4.
Examples of EL
Example of accessing an attribute in the request object
Servlet code
Date currentDate = new Date();
request.setAttribute("currentDate", currentDate);
JSP code
<p>The current date is ${currentDate}</p>
JSP code
<p>Hello ${user.firstName}</p>
Syntax:
${scope.attribute}
All of the implicit EL objects for specifying scope are maps. As a result, you can use the
dot operator to specify a key when you want to return the object for that key.
Example of specifying request scope
Servlet code
Date currentDate = new Date();
request.setAttribute("currentDate", currentDate);
JSP code
<p>The current date is ${requestScope.currentDate}</p>
JSP code
<p>Hello ${sessionScope.user.firstName}</p>
${attribute["propertyKeyOrIndex"]}
Servlet
User user = new User(Peter, Lee, peter@yahoo.com);
session.setAttribute("user", user);
JSP
<p>Hello ${user[firstName]}</p>
Servlet
String[] grades= {A, B, C};
ServletContext application = this.getServletContext();
application.setAttribute(grades, grades);
JSP
<p>The best grade is ${grades[0]}<br>
The second best grade is ${grades[1]} </p>
Servlet
ArrayList<User> users = UserRepository.getUsers(path);
season.setAttribute(users, users);
JSP
<p>The first users address is ${users[0].emailAddress}<br>
The second users address is ${users[1].emailAddres} </p>
JSP
<p>Book ISBN: ${item.book.isbn}</p>
Or
<p>Book ISBN: ${item[book].isbn}</p>
JSP
JSP
Implicit EL Objects
They are built-in objects that allow you to perform common tasks in a JSP.
All of the implicit objects are maps, except for the pageContext object which is a JavaBean.
Implicit object
Description
param
paramValues
header
headerValues
cookie
initParam
pageCount
JSP code
Examples
of
Implicit
EL
Objects
To work with cookies
Servlet code
JSP code
<p>The email cookie: ${cookie.emailCookie.value}</p>
<context-param>
<param-name>addmissionsEmail</param-name>
<param-value>monica@npu.edu</param-value>
</context-param>
JSP code
Other EL Operators
Alternative
Operator
Alternative
==
eq
&&
and
!=
ne
||
or
<
lt
not
Operator
Alternative
div
>
gt
mod
<=
le
>=
ge
Example:
<p>Operator test: ${1 + 1}</p>
Operator
Alternative
empty x
X?Y:Z