Programming Tutorials

Java Beans and the Expression Language

By: Henry in Java Beans Tutorials on 2007-09-23  

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.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java Beans )

Latest Articles (in Java Beans)