Skip to main content

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:-
  1. $ (<expression>)
E.g:-
To enable EL
  1. <%@page isELignored="false" %>
  2. $ (4+5)-->9
To ignore EL
  1. <%@page isELignored="true"%>
  2. $ (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
  1. ||-->or
  2. &&-->and
  3. <-->lt
  4. <=-->le
  5. !=-->ne
  6. >-->gt
  7. >=-->ge
  8. ==-->eq and etc...
E.g(1):-
  1. 4<5?$ (4<5) (or) $ (4 lt 5)
E.g(2):-
  1. 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:-
  1. <%request.setAttribute("attr1","val1";
  2. session.setAttribute("attr2","val2");
  3. application.setAttribute("attr3","val3");%>
get_values.jsp:-
  1. attr1(req) data::$ (requestScope.attr1)<br>
  2. atrr2(ses) data::$ (sessionScope.attr2)<br>
  3. 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
  1. uname request param value:$ (param.uname)-->gives teja
  2. <br>
  3. 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:-
  1. user-agent request header values: ${header("user-agent")}
  2. <br>
  3. Accept request header value: ${headerValues.accept(0)}
Cookie:-
-->Useful to read and display cookie name and cookie values
  1. Cookie name holding sessionID: ${cookie.JSESSIONID.name}
  2. <br>
  3. 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:-
  1. <web-app>
  2.   <context-param>
  3.      <param-name>dbuser</param-name>
  4.      <param-value>scott</param-value>
  5.   </context-param>
  6. </web-app>
initParamTest.jsp
  1. 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
  1. package org.el;
  2. public class WishGenerator{
  3.  public static String sayHello(String user){
  4.   return "gud morning"+user;
  5.  }
  6. }
(2)Configure function name, function handler class in tld file
  1. <taglib>
  2.    <tlib-version>1.1</tlib-version>
  3.    <jsp-version>2.1</jsp-version>
  4.    <short-name>myfx</short-name>
  5.    <uri>wishfx</uri>
  6.    <function>
  7.      <name>sayHello</name>
  8.      <function-class>org.el.WishGenerator</function-class>
  9.     <function-signature>java.lang.String sayHello</function-signature>
  10.   </function>
  11. </taglib>
(3)Import and use EL function in jsp component
test.jsp
  1. <%@ taglib uri="wishfx" prefix="fx"%>
  2. Wish Message: ${fx:sayHello("teja")}

Comments

Popular posts from this blog

JSP Comments

Comments in JSP:- -->Compiler /Interpreter doesn't take commented code for compilation /Interpretation, so the commented code doesn't participate in execution JSP supports 3 types of comments:- a.)HTML comments /Template text comments/ Output comments Syn :- <!---text---> -->Recognized by html interpreter of browser b.)JSP comments /Hidden comments Syn :- <%--text--%> -->JSP page compiler recognize these comments c.)Java comments /Scripting comments Syn :- //-->For single line /*-- -----*/-->For multiple line -->Java compiler(java) recognize these comments -->In Eclipse IDE JES class for first.jsp comes in our(workspace)folder/.metadata/.plugins/.org eclipse Comments (1)JSP comment<%--%> (2)Java comment(// or /*--*/) (3)HTML comments(<!---> JES Source Code (1)No (2)Yes (3)Yes In JES compiled code (1)No (2)No (3)Yes In the code going to browser (1)No (2)No (3)Yes Output (1)No (2)No (3)No -->Jsp comments are not visible in any phase...

Scripting Tags

JSP Tags/ Elements (1)Scriptlet     Standard syn:-         <%.........%>     xml syn:-        <jsp:scriptlet>...</jsp:scriptlet> Note: All scripting tags allows us to place script code(java code) 1.)The code placed in scriptlet go to _jspService(-,-) of JES class 2.)In a jsp page we can have zero or more scriptlets 3.)We place request processing logics in scriptlets 4.)Variables declared in scriptlets becomes the local variables in _jspService(-,-) of JES class in first.jsp In first.jsp: <% int a=10;  out.println("square:"+(a*a));%> In first_jsp.java(JES): public class first_jsp extends..{     public void _jspService(-,-){        int t=30;        out.println(“Square:”+(t*t));     }  } The code placed in scriptlet can use implicit objects of jsp because implicit objects and the code placed scriptlet goes to _jspService(-...

Project Architecture's

1.) Functional flow/ Architecture a.) Only for 6+/7+years b.) Explain the flow of process/business 2.) Technical Flow/Architecture a.) Upto 5+ years b.) Explain technologies /components that are involved in the project Servlet & JSP Project Architecture:- 1.)What is need of Business Deligate to convert VO class object to DTO class object ? a.) If Business Deligate is not there servlet component directly prepares DTO class object by receiving and translating form data and passes to service class, if service class excepts certain input in other format like numeric employee number as string employee number then we need to modify servlet component code i.e For the changes that happened in business tier service component we have to modify servlet component of presentation tier b.) If Business Deligate is taken then it gets original form data as VO class object having string inputs and converts VO class object to DTO class object to pass to service class c.) If service class excepts ...