Programming Tutorials

Introduction to JSP expression language

By: Abinaya in JSP Tutorials on 2007-09-23  

One of the features of the JSP specification that you'll be using most often is the JSP expression language, an intentionally simple language that is, to a large extent, independent from JSP. In previous incarnations of JSP, Java code was embedded into JSP pages in the form of scriptlets, for example:

<%
MyBean bean = new MyBean();
String name = bean.getName();
out.println(name);
%>

This scriptlet creates a new instance of a class called MyBean, gets its name property, assigns this to a string variable, and then outputs this string to the page. Now you might be looking at this and thinking, I can achieve the same thing by using the JSP standard actions (<useBean> and <getProperty>).

Although this is certainly true, it was previously extremely hard to write a function-rich JSP-based web application without using a number of scriptlets within your pages. In fact, there are many problems associated with using Java code in the form of scriptlets in JSP pages.

The first and most obvious of these is that it's very common for non-Java programmers to create the user interface for a system. This is because graphic designers are generally better than Java programmers at creating functional user interfaces. The second problem caused by the use of scriptlets is that of maintainability. Embedding large amounts of code into the user interface of a system makes the interface much harder to change and understand.

For all of these reasons, the JSP 2.0 specification introduced an expression language (EL) that can do pretty much everything that scriptlets can do. This language is far simpler to understand than Java and looks very similar to JavaScript. The following are good reasons for this similarity:

JavaScript is something that most page authors are already familiar with.
The EL is inspired by ECMAScript, which is the standardized version of JavaScript.

In fact, both ECMAScript and the XPath EL inspired the JSP EL. The EL specification states, ". . . the experts involved were very reluctant to design yet another expression language and tried to use each of these languages, but they fell short in different areas".

If you've been following the progress of JSP, and the JSP Standard Tag Library (JSTL), you're probably aware that the first expression language was released as part of the JSTL. The EL was then incorporated into the JSP 2.0 specification with JSTL 1.1. At around the same time, the JavaServer Faces (JSF) expert group was developing an expression language for JSF. Because of JSF requirements, the JSF expression language had some differences from the JSP expression language. JSP 2.1 unifies the two versions so that there is a single expression language used for JSP, JSTL, and JSF.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in JSP )

Latest Articles (in JSP)