Wednesday, August 7, 2013

JSP Expression Language

Expression Language :
---------------------------

Expression language is added from jsp2.0 specification.It simplifies
many things as below.

Syntax of expression language is

Generally you say request.getParameter("name") to get the value..submitted
from the form.

Instead you can simply say

${param.name}

${firstThing.secondThing}
----------------------------------------
a)firstThing can be EL Implicit object
All the following are map objects
------------------------------------------
pageScope
requestScope
sessionScope
applicationScope
param
paramValues
header
headerValues
cookie
initParam
pageContext

or

b)firstThing can be attribute set in the following scopes:

-----------------------------------------------------------------
page scope

request scope

session scope

application scope

we will cover the more details...one by one

First Example:
-----------------
In the above description I said firstThing can be EL Implicit object or
an attribute in the four scopes.. I will take param Implicit Object as
a first example because that is very easy..

a.jsp
-------
<form action="b.jsp">
<input type="text" name="studentname"/>
<input type="submit"/>

</form>

b.jsp
----------
In b.jsp, you can get studentname submitted from a.jsp with
<%= request.getParameter("studentname") %>

or

${param.studentname}

param is a map created by container with all the key value pairs
from the form.

Param will be just like a hashmap with the following
key value pairs.Container stores all the key value pairs
in param.
--------------------------

studentname anil(whatever you type in the form)
studentcity hyderabad(whatever you type in the form).

I will cover all the Implicit objects one by one.
Practice the example given in a.jsp and b.jsp
and let me know if you get any issues.
Day2:
Expression Language Day2:
----------------------------------

Refer Yesterday's post if someone has missed it.
Expression language syntax is

${firstThing.secondThing}

firstThing can be attribute in 4 scopes(page,request,session,application)
firstThing can be EL implicit Objects.Refer yesterday's post....

When you use .(dot)syntax firstThing.secondThing...

firstThing should be always map or a bean

ImpPoint:
------------
All the Expression language implicit objects are maps.. except pageContext.
pageContext is a bean.

What is pageContext and How can we use it and what are the various uses
of it ?

pageContext is a bean..As I said.. firstThing before .(dot) should always
be bean or map.Since pageContext is bean I can use it before .(dot).

Various uses of pageContext
----------------------------------

1)Using pageContext, you can set attribute set in any of the scopes(request,
respone,session and application.

Eg: If in servlet/jsp you will ... set an attribute in session with following syntax

session.setAttribute("name","Anil");

With PageContext you can set an attribute in session Using following syntax:
-----------------------------------------------------------------------------------------
<%
pageContext.setAttribute("name", "anil",PageContext.SESSION_SCOPE);
%>

2)You can get the attribute set in any scope using pageContext.
Second argument is the scope from where you want to get the name.
You can specify PageContext.REQUEST_SCOPE, if you stored name
in request scope.you can think of requestScope,sessionScope,applicationScope
and pageScope as map of attributes.. and its values...

You can get that attribute using

<%= pageContext.getAttribute("name",PageContext.SESSION_SCOPE);
or using expression language syntax.

${sessionScope.name}



3) If you don't know the scope where you have set the attribute..

you can use
<%= pageContext.findAttribute("name") %>

It will search from page->request->session->application.

If it doesn't find in page it searches request,if it doesn't find in request it
searches session and then application like that.....

4)To get attributes set in various scopes using expression language..

${pageScope.name} -- To get name attribute from page.

${requestScope.name} -- To get name attribute from request.

${sessionScope.name} -- To get name attribute from session.

${applicationScope.name} -- To get name attribute from application.

5)To get attributes set in various scopes(page,request,session,application) using java syntax using jsp pageContext implicit object

a) <%
pageContext.getAttribute("name", "anilkumar",PageContext.PAGE_SCOPE);
%>

b)<%
pageContext.getAttribute("name", "anilkumar",PageContext.REQUEST_SCOPE);
%>

c)<%
pageContext.getAttribute("name", "anilkumar",PageContext.SESSION_SCOPE);
%>

d)<%
pageContext.getAttribute("name", "anilkumar",PageContext.APPLICATION_SCOPE);
%>


Refer pageContext.jsp that I am uploading for example...
 Day 3:

In the... day1 & day2 of Expression language....
We were discussing about the following EL syntax...

If any one missed out please search with "Expression Language "
for previous posts on EL.

1)${firstThing.secondThing} (Dot operator syntax)

a)You have to understand that.. firstThing is always EL implicit
object or bean.
b)secondThing after dot operator should be.. map key or bean property.

Eg: Suppose Country map is there..


2)${firstThing.["somestring"]} ([] syntax)

In this..

firstThing can be
a)map
b)bean
c)list
d)array

So,with map and bean... you can use both . operator syntax and
[] operator syntax.

If first thing is list or array, you can use only [] syntax..

Uploading the elBracketEx.jsp..In this example is given on how to use
[] operator syntax with array and map.
 
 
 

No comments:

Post a Comment