7.1) Given a scenario, write EL code that accesses the following implicit variables including pageScope, requestScope, sessionScope, and applicationScope, param and paramValues, header and headerValues, cookie, initParam and pageContext.
For EL expressions to work you need to make sure you have the correct version of the web-app tag at the start of the deployment descriptor. I wasted a lot of time trying to get EL expressions to work before I realised I had to have this version of the deployment descriptor. It needs to point to version 2.4 of the servlet specification. To make life easy you can cut and paste the following text at the top of your deployment descriptor (WEB.XML).
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
The expression language (EL from now on) is a new feature of JSP and was introduced with the JSP 2.0 specification. It's purpose was to help programmers write JSP pages with less or no scriptlets. Instead of scriptlets the EL is a new set of XML tags offers much of the functionality of scriptlets. Thus the EL has access to implicit objects in the JSP page that are similar but not identical to those available to scriptlets.
It is important to remember that EL expressions are not Java. In some ways they are a little like the difference between MS Excel formulae and VBA code (well only a little bit). Note that you cannot directly call Java methods from within EL expressions. EL makes extensive use of what I call curly braces, prefixed by the dollar sign thus you if you have a page with the EL code
${param.username}And access the page with the url
http://www.myurl.com/myjsp.jsp?username=marcusgreenThe EL expression will print out marcusgreen in the page
That EL code is equivalent to the script
<%= request.getParameter("username") %> The output of EL expressions can be embedded within other page tags. Thus the following code can use the request parameter to decide what HTML tags would surround the word Hello.
<${param.tag}>Hello</${param.tag}If a page with this expression was called with the query string
index.jsp?tag=h1
The output would be
<h1>Hello</h1>
The EL implicit objects, as given in the objective are
pageScope
requestScope
SessionScope
applicationScope
param
paramValues
header
headerValues
cookie
initParam
pageContext
With the exception of pageContext all of these objects are of type map, the pageContext object is a reference to the actual pageContext object. The implications of the type being Map is covered in the next objective.
Other sources
JSP Implicit objects from gulland.com
http://www.gulland.com/courses/JavaServerPages/jsp_objects.jsp
JSP Implicit objects from the Sun J2EE tutorial
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro7.html
The EL according to Oracle
http://www.oracle.com/technology/pub/articles/cioroianu_jspapi.html
IBM article The JSTL EL
http://www-128.ibm.com/developerworks/java/library/j-jstl0211.html
According to Mikalai Zaikin
http://java.boot.by/wcd-guide/ch07.html