EL in itself
is not very useful when creating web
applications. In this tutorial, you’ll focus on how to use the
EL to read values from JavaBeans
to display within a JSP page. In previous incarnations of the JSP specification, you would have
had to use code such as the following to read values from a JavaBean:

<jsp:getProperty name="myBean"
property="name" />

An alternative (and more common) method is to use a scriptlet
such as the following:

<%= myBean.getName()%>

The use of scriptlets does not represent
good practice in JSP development.
This may make you ask the question, “If I can use the <getProperty>
standard action,
why does anyone use scriptlets?" The answer to this
question is simple: We developers are
lazy! The scriptlet option represents less code and is a lot
quicker to type!

To get around this problem, the EL provides a nice way to access
the properties of a
JavaBean that is in scope within a page, request, session, or
application. To achieve the same
as the previous code sample, you can use the following
expression:

${myBean.name}

This is a nice neat way to access properties; there are no nasty
brackets or any other Java like
syntax present. This brings us to another core feature of the
EL: the concept of named
variables. The EL provides a generalized mechanism for resolving
variable names into objects.
This mechanism has the same behavior as the
pageContext.findAttribute() method of the
PageContext object. Take the following, for example:

${product}

This expression will look for the attribute named product by
searching the page, request,
session, and application scopes, in that order, and will print
its value. This works regardless of
how the object was placed into the particular scope. That is,
one JSP page could put an attribute
into request, session, or application scope, and another JSP
page could access the attribute
simply by using an EL statement that uses the name of the
attribute. If the attribute is not
found, null will be returned. This method is also used to
resolve the implicit objects that we’ll
talk about in the next section.

EL can be used to access JavaBeans and the
properties of JavaBeans.
The JSP page in Listing 3-6 creates a JavaBean of type
com.apress.projsp.Person with an
id of person, and sets its properties to the values of
parameters in the HTTP request with the
same name as the properties. This is achieved with the following
code:

The <jsp:setProperty> tag has various syntaxes. When you
use property="*", that tells
the page implementation class to find each request parameter
with the same name as a
JavaBean property, and to set the JavaBean property with the
value of the request parameter.
The JSP page accesses the object via the id given to it in the
previous <useBean> tag, in
this case, person. The page then displays the values of the
properties of the Person JavaBean
in a table; this is achieved by the following code:

The id is used to access the JavaBean that you declared with the
previous <useBean> tag.
In this example, the object was created and accessed within the
same page. However, as we
noted earlier, the object could have been created from any page,
and still accessed in the
simpleBean.jsp page.

It’s worth noting that you could have used the following code
to access the properties
of our JavaBean:

Try changing the values in the form and clicking Submit Query.
You should see your new
values in the table. Now that you’ve seen a very simple use of
JavaBeans and the EL, you’ll look
at a more complex use of the two technologies.