EL Variables Are pageContext Attributes

This section provides a tutorial example to prove that EL variables are 'pageContext' attributes.

Based the rules and the examples in the previous sections, we can easily conclude that:

Here is an example JSP page to show you that you can mix variables and pageContext attributes any way you want:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" 
   xmlns:c="http://java.sun.com/jsp/jstl/core" version="2.1"> 
<!-- ExpVariable.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<c:set var="message" value="Hi there!"/>
1. ${message}<br/>
2. <jsp:expression>pageContext.findAttribute("message")
   </jsp:expression><br/>

<jsp:scriptlet><![CDATA[
   String s = "Hello world!";
   pageContext.setAttribute("hello", s, PageContext.PAGE_SCOPE);
]]></jsp:scriptlet>
3. ${hello}<br/>

<jsp:useBean id="today" class="java.util.Date"/>
4. ${today}<br/>

<jsp:scriptlet><![CDATA[
   java.util.Date d = new java.util.Date();
   d.setTime(d.getTime()+24*60*60*1000);
   pageContext.setAttribute("tomorrow", d, PageContext.PAGE_SCOPE);
]]></jsp:scriptlet>
5. ${tomorrow}<br/>

<jsp:setProperty name="today" property="time" value="1000000000000"/>
6. ${today}<br/>
7. ${today.time}<br/>

</body></html>
</jsp:root>

Here is the output this page:

1. Hi there!
2. Hi there!
3. Hello world!
4. Sun Jul 01 13:20:19 EST 2012
5. Mon Jul 02 13:20:19 EST 2012
6. Sat Sep 08 21:46:40 EDT 2001
7. 1000000000000

Note that:

Exercise: Since there many methods to add an attribute to pageContext, useBean, c:set, and pageContext.setAttribute, write a simple program to show what happens if the same attribute name is used by different methods.

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

EL (Expression Language)

 What is EL (Expression Language)?

 EL Expression Types and Usage

 Literal Data and Named Variables

 Basic Operators and Operations

 Predefined Implicit Objects

 Collection Elements and Object Properties

 Expression Examples in Static Text

 Expression Examples in Static Text - Result

EL Variables Are pageContext Attributes

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials

 References

 PDF Printing Version