Expression Lang
Expression Language(EL):
-->Writing java code in jsp is bad practice, so prefer different tags while developing jsp components, performing arithmetic and logical operations through tags may be a complex process or sometimes may not be possible. So we can use "EL" as alternate
Syn:-
- $ (<expression>)
E.g:-
To enable EL
- <%@page isELignored="false" %>
- $ (4+5)-->9
To ignore EL
- <%@page isELignored="true"%>
- $ (4+5)-->$ (4+5)
-->EL deals with
a.)EL operators
b.)EL implicit objects(additional objects to existing 9 objects)
c.)EL functions
a.)EL operators:
-->Same as java operators but can be used in different syntaxes
- ||-->or
- &&-->and
- <-->lt
- <=-->le
- !=-->ne
- >-->gt
- >=-->ge
- ==-->eq and etc...
E.g(1):-
- 4<5?$ (4<5) (or) $ (4 lt 5)
E.g(2):-
- 10>20?$ (10>20) (or)$ (10 gt 20)
Note: EL can be applied on the tags and also on the template text of jsp
EL Implicit objects:
-->EL gives more implicit object supporting java code less jsp development
pageScope, requestScope, sessionScope, applicationScope, cookie, param, paramValues, header, headerValues, initParam and etc...
xxxScope objects:-
-->Allows to keep data in different scopes and also allows to read data from different scopes
set_values.jsp:-
- <%request.setAttribute("attr1","val1";
- session.setAttribute("attr2","val2");
- application.setAttribute("attr3","val3");%>
get_values.jsp:-
- attr1(req) data::$ (requestScope.attr1)<br>
- atrr2(ses) data::$ (sessionScope.attr2)<br>
- attr3(application) data::$ (applicationScope.attr3)
-->Gives first request to set_values.jsp and other requests to get_values.jsp from same browser and different browser
Param:-
It is useful to read one request param values
Param values:-
-->It is useful to read multiple request param values, it can be also used to get multiple values of one request param
E.g:-
request url:- http://localhost:1025/ELProj1/paramTest.jsp?uname=teja&uname=kumar&pwd=hair
paramTest.jsp
- uname request param value:$ (param.uname)-->gives teja
- <br>
- uname, pwd request param values:$ {paramValues.uname(0)},$ {paramValues.uname(1)},$ {paramValues.pwd(0)}
header:-
Useful to get one request header value
headerValues:-
Useful to get multiple request header values
headerTest.jsp:-
- user-agent request header values: ${header("user-agent")}
- <br>
- Accept request header value: ${headerValues.accept(0)}
Cookie:-
-->Useful to read and display cookie name and cookie values
- Cookie name holding sessionID: ${cookie.JSESSIONID.name}
- <br>
- Cookie value: ${cookie.JSESSIONID.value}
-->HttpSession object allocates memory on server having session id and these session id goes to browser and comes back to web application along with the request, response in the form of cookie
initParam:-
-->Given to read context param values that are configured in web.xml file
E.g:-
web.xml:-
- <web-app>
- <context-param>
- <param-name>dbuser</param-name>
- <param-value>scott</param-value>
- </context-param>
- </web-app>
initParamTest.jsp
- DBUser context param values: ${initParam.dbuser}-->gives scott
EL functions:
-->EL function is like developing custom tag i.e we can execute java class methods internally for EL function that is developed
ELProj2
|-->java resources
|-->src
|-->org.el
|-->WishGenerator.java
|-->webcontent
|-->FxTest.jsp
|-->WEB-INF
|-->web.xml
|-->wish.tld
Steps:-
(1)Developing java class with static method having logic to executes for EL function (Function handler class)
WishGenerator.java
- package org.el;
- public class WishGenerator{
- public static String sayHello(String user){
- return "gud morning"+user;
- }
- }
(2)Configure function name, function handler class in tld file
- <taglib>
- <tlib-version>1.1</tlib-version>
- <jsp-version>2.1</jsp-version>
- <short-name>myfx</short-name>
- <uri>wishfx</uri>
- <function>
- <name>sayHello</name>
- <function-class>org.el.WishGenerator</function-class>
- <function-signature>java.lang.String sayHello</function-signature>
- </function>
- </taglib>
(3)Import and use EL function in jsp component
test.jsp
- <%@ taglib uri="wishfx" prefix="fx"%>
- Wish Message: ${fx:sayHello("teja")}
Comments
Post a Comment
Express your feelings