Java Beans and the Expression Language

By: Henry Emailed: 1729 times Printed: 2341 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.

Listing below is a JSP page that uses EL to access JavaBeans.

simpleBean.jsp
<jsp:useBean id="person" class="com.apress.projsp.Person" scope="request">
<jsp:setProperty name="person" property="*"/>
</jsp:useBean>
<html>
<head>
<title>EL and Simple JavaBeans</title>
<style>
body, td {font-family:verdana;font-size:10pt;}
</style>
<head>
<body>
<h2>EL and Simple JavaBeans</h2>
<table border="1">
<tr>
<td>${person.name}</td>
<td>${person.age}</td>
<td>&nbsp;</td>
</tr>
<tr>
<form action="simpleBean.jsp" method="post">
<td><input type="text" name="name"></td>
<td><input type="text" name="age"></td>
<td><input type="submit"></td>
</form>
<tr>
</table>
</body>
</html>

The JSP page above uses a JavaBean of type com.apress.projsp.Person. That JavaBean is shown in Listing below.

//Person.java
package com.apress.projsp;
public class Person {
private String name;
private int age;
public Person() {
setName("A N Other");
setAge(21);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}

Use the following steps to deploy the JSP page and JavaBean into a web container:

  1. Create a file called simpleBean.jsp in the expressionLanguage folder, and enter the code from Listing above into it.
  2. Inside the expressionLanguage\WEB-INF directory, create a directory called classes.
  3. Create a com\apress\projsp subdirectory within the WEB-INF\classes directory.
  4. Create a file called Person.java within the WEB-INF\classes\com\apress\projsp directory with the contents shown in Listing above, and compile it.
  5. Start Tomcat and your web browser and go to http://localhost:8080/expressionLanguage/simpleBean.jsp.


You should see something similar to the page shown below.


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:

<jsp:useBean id="person" class="com.apress.projsp.Person" scope="request">
<jsp:setProperty name="person" property="*"/>
</jsp:useBean>

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:

<tr>
<td>${person.name}<td>
<td>${person.age}</td>
<td>&nbsp;</td>
</tr>

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:

<tr>
<td>${person["name"]}</td>
<td>${person["age"]}</td>
<td>&nbsp;</td>
</tr>

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.


Java Beans Home | All Java Beans Tutorials | Latest Java Beans Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

Very nice tutorial........

View Tutorial          By: Santosh Saha at 2010-04-11 05:58:51
2. View Comment

Hello,
Thank you very much for this sample. Finally I have understood how can I this stuff works together.


View Tutorial          By: Alex at 2010-08-12 07:30:32
3. View Comment

Fantastic tutorial...
very help full to me...thank you


View Tutorial          By: venkat b at 2013-05-30 17:16:04

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Henry
Generating Your Key Pair example using keytool in Java
Summary of Operators in Java
JDBC Basics and JDBC Components
The IterationTag Interface in JSP
Trigonometric, Hyperbolic, Exponential and Logarithmic Functions in C
Using Bit-field in C
Java Beans and the Expression Language
Initialization of Variables in C
Creating Struts Modules
else if statement in C
Line Counting sample program in C
Accessing Context Data in Beans using JSF
Using peek() and putback() with cin in C++
Internationalization Support in J2ME MIDP
java.lang.SecurityException: MIDP lifecycle does not support system exit.

More Tutorials in Java Beans
Creating a JavaBean to Connect with Google API
Spring Vs EJB ( A feature comparison)
What is EJB server and what are EJB Components?
JavaBeans Basic Concepts
JavaBeans vs. Custom Tags
Java Beans and the Expression Language
A sample that shows Java Beans, Servlets and JSP working together
Advantages of Java Beans
Design Patterns for Properties in a Java Bean
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
ADVANTAGES OF EJB
Steps to develop EJB Environment
EJB is a server side component:
Entity Bean
History Of Java

More Latest News
Most Viewed Articles (in Java Beans )
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
A sample that shows Java Beans, Servlets and JSP working together
What is EJB server and what are EJB Components?
ADVANTAGES OF EJB
Spring Vs EJB ( A feature comparison)
Reusable component
Java Beans and the Expression Language
EJB is a server side component:
Entity Bean
Advantages of Java Beans
Design Patterns for Properties in a Java Bean
JavaBeans Basic Concepts
Steps to develop EJB Environment
History Of Java
JavaBeans vs. Custom Tags
Most Emailed Articles (in Java Beans)
Steps to develop EJB Environment
ADVANTAGES OF EJB
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
What is EJB server and what are EJB Components?
History Of Java
Advantages of Java Beans
A sample that shows Java Beans, Servlets and JSP working together
JavaBeans vs. Custom Tags
Creating a JavaBean to Connect with Google API
Reusable component
EJB is a server side component:
Entity Bean
Design Patterns for Properties in a Java Bean
Java Beans and the Expression Language
JavaBeans Basic Concepts