What are the implicit objects of expression language?

El has some implicit objects but these are not same as jsp. The implicit objects are Maps that map the respective implicit objects to their values except one object that is, pageContext. The pageContext object ia not a Map, it is the actual reference to the pageContext.
The implicit objects are listed below,

-pageScope: A map that maps page scoped attributes to their values.

-requestScope: A map that maps request scoped attributes to their values.
Using requestScope:
We can get the request parameters as,
${requestScope.param name}
Or
${requestScope[“param name”]}
The implicit objects are not the actual objects.
So to call request methods? Suppose getMethod().
We can do it by scripting as,
<%=request.getMethod()%>
But we can not write ${requestScope.method} because requestScope is not the request object, rather it is a Map. It is used to get request attributes not the request properties.We can use pageContext.(see in the page context heading)

-sessionScope: A map that maps session scoped attributes to their values.

-applicationScope: A map that maps application scoped attributes to their values.

-Param: A map that maps request parameter names to their values. Same as request.getParameter() in servlet.

-paramValues: A map which maps parameter names to a String[] of values for that parameter. It is same as request.getParameterValues() in servlet.

Using param and paramValues:
In the HTML form:
<from action=”Test.jsp”>
Name:<input type=”text” value=”name”/>
First food:<input type=”text” value=”food”/>
Second food:<input type=”text” value=”food”/>
<input type=”submit” value=”click”>
</from>
In test JSP:
Request param name is: ${param.name}
Value of first food is: ${paramValues.food[0]}
Value of second food is: ${paramValues.food[1]}

-header: A Map that maps header name to its value. It is same as request.getHeader() in servlet.

-headerValues: A Map that maps the header names to a String[] of all values for that header. It is same as request.getHeaders() in servlet.
Using header:
We can get the header using scripting as,
Host is:<%=request.getHeader(“host”)%>
We can get also using EL as,
Host is: ${header[“host”]}
Or
Host is: ${header.host}
You can use either .(dot) or [] operator to get the header Map values.
If there are multiple values of same header, you can use headerValues.

-cookie: A map that maps cookie name to the cookie object. It is same as request.getCookies() in servlet.
Using cookie:
In scripting:
<%
Cookie[] cookie=request.getCookies();
for(int i=0;i<cookie.length;i++){
if(cookie[i].equals(“username”)){
out.println(cookie[i].getValue());
%>
Using EL:
${cookie.username.value}

-initParam: A map that maps the initialization parameter names to their values. It is same as request.getInitParameter() in servlet.
Using init parameter:
In the web.xml:
<context-param>
<param-name>name</param-name>
<param-value>bhabani</param-value>
</context-param>
We can get this init parameter in our jsp,
By scripting:
<%=application.getInitParameter(“name”)%>
By EL:
${initParam.name}
We have two types of initialization parameter <init-param> and <context-param>. But the EL initParam means <context-param>

-pageContext: It is not a Map. It holds the actual reference of pageContext.

The other implicit objects are used to get the attributes. pageContext is used to get everything else (methods).
How to call the getMethod() of request object,
Using scripting:
<%=request.getMethod()%>
Using EL:
${pageContext.request.method}
This means pageContext has a request property and request has a method property.

No comments:

Post a Comment