JSP Using Expression Language
- What is EL?
- Creating JSPs with the Expression Language (EL)
- Understanding the Expression Language
- Using Implicit Variables in EL Expressions
- Using EL Operators in JSP Pages
- Incorporating functions with EL
- Conclusion
- EL Examples
What is EL? |
- Before JSP 2.0, we could use only a scriptlet, JSP expression, or a custom tag to include server state in the jsp page output.
- Expression Language (EL) was first introduced in JSTL 1.0.
- EL makes it easier to integrate server side state with the presentation output.
- EL expressions are no longer limited to JSTL action attributes, but may be used in any standard or custom action attribute declared to accept a runtime expression.
- EL expressions can be used in static text.
- EL expressions can be used directly in template text outside of any actions.
- A Primary feature of JSP technology version 2.0 is its support for an expression language (EL).
- An expression language makes it possible to easily access application data stored in JavaBeans components.
For Example, The JSP expression language allows a page author to access a bean using simple syntax such as ${name}.
Creating JSPs with the Expression Language (EL) |
Creating JSPs with the Expression Language (EL)
- Understanding the Expression Language
- Using EL Operators
- Incorporating functions with EL
Understanding the Expression Language |
Expression Language (EL)
- Depends less on Java
- Doesn’t use tags at all
Understanding The Expression Language
- All EL expressions begin with "${" and end with "}".
-
JSP script expression
The outside temperature is <%= temp %> degrees. -
EL expression
- EL expressions can’t use variables declared in scripts
Reserved Words
The following words are reserved for the JSP expression language and should not be used as identifiers.
and eq gt true instanceof
or ne le false empty
not lt ge null div mod
Note: that many of these words are not in the language now, but they may be in the future, so you should avoid using them.
Using Implicit Variables in EL Expressions |
The JSP expression language defines a set of implicit objects:
- pageContext :- The context for the JSP page. Provides access to various objects including:
- servletContext :- The context for the JSP page’s servlet and any web components contained in the same application. See Accessing the Web Context.
- session :- The session object for the client. See Maintaining Client State.
- request :- The request triggering the execution of the JSP page. See Getting Information from Requests.
- response :- The response returned by the JSP page. See Constructing Responses.
In addition, several implicit objects are available that allow easy access to the following objects:
- param :- Maps a request parameter name to a single value
- paramValues :- Maps a request parameter name to an array of values
- header :- Maps a request header name to a single value
- headerValues :- Maps a request header name to an array of values
- cookie :- Maps a cookie name to a single cookie
- initParam :- Maps a context initialization parameter name to a single value
Finally, there are objects that allow access to the various scoped variables described in Using Scope Objects.
- pageScope :- Maps page-scoped variable names to their values
- requestScope :- Maps request-scoped variable names to their values
- sessionScope :- Maps session-scoped variable names to their values
- applicationScope :- Maps application-scoped variable names to their values
When an expression references one of these objects by name, the appropriate object is returned instead of the corresponding attribute.
For example, org.apache.jasper.runtime.PageContextImpl@2f5c7277 returns the PageContext object, even if there is an existing pageContext attribute containing some other value.
Display the bufferSize of the page’s JSPWriter
Retrieve the request’s HTTP method
EL restrains you from invoking Java methods, you can’t use an expression like
Difference between Script expression and EL Expressions
Using EL Operators in JSP Pages |
EL operators can be divided into 4 categories:
- property/collection
- access operators
- arithmetic operators
- relational operators
- logical operators
EL Operators for Property and Collection Access
- a.b – Returns the property of a associated with the identifier, b
- a[b] – Returns the value of a associated with the key or index, b
- If b is a String EL treats them interchangeably
EL Arithmetic Operators
- Integer and BigInteger values for fixed-point numbers
- Double and BigDecimal values for floating-point numbers
- Addition: +
- Subtraction: –
- Multiplication: *
- Division: div and /
- Modulo division: mod and %
EL Relational and Logical Operators
- Equality: == and eq
- Non-equality: != and ne
- Less than: < and lt
- Greater than: > and gt
- Less than or equal: <= and le
- Greater than or equal: >= and ge
- Logical conjunction: && and and
- Logical disjunction: || and or
- Logical inversion: ! and not
Incorporation of Functions with EL |
Incorporation of Functions with EL
- The process of inserting an EL function into a JSP involves creating or modifying four files:
- Method class (*.java) :- Contains the Java methods that you want to use in your JSP.
- Tag library descriptor (*.tld) :- Matches each Java method to an XML function name.
- Deployment descriptor (web.xml) :- Matches the TLD to a tag library URI. (Note: Changing this file is optional, but recommended.)
- Java Server Page (*.jsp) :- Uses the tag library URI and function name to invoke the method.
Conclusion |
- Primary Goal of EL: to remove Java from JSP development
- The Expression Language provides essentially the same constructs for property access, collection access, arithmetic, logic, and relational comparisons as C or Java.
- Property access and collection access are essentially the same thing in EL.
- EL numbers must be of the Integer, BigInteger, Double, or BigDecimal data types
EL Examples |
Recent Comments