JSP Pages Using the Expression Language (EL)

Chapter 7 - Building JSP Pages Using the Expression Language (EL)

    JSP Pages Using the Expression Language (EL)

  • Building JSP Pages Using the Expression Language (EL)

    There are several implicit objects available in EL expressions used in JSP pages:

    pageContext - the PageContext object. Provides an API to access various objects including:

    o context - the context for the JSP page's servlet and any Web components contained in the same application.

    o session - the session object for the client.

    o request - the request triggering the execution of the JSP page.

    pageScope - a java.util.Map that maps page-scoped attribute names to their values.

    requestScope - a java.util.Map that maps request-scoped attribute names to their values.

    sessionScope - a java.util.Map that maps session-scoped attribute names to their values.

    applicationScope - a java.util.Map that maps application-scoped attribute names to their values.

    param - a java.util.Map that maps parameter names to a single String parameter value (obtained by calling ServletRequest.getParameter(String name)).

    paramValues - a java.util.Map that maps parameter names to a String[] of all values for that parameter that obtained by calling ServletRequest.getParameterValues(String name).

    header - a java.util.Map that maps header names to a single String header value that obtained by calling HttpServletRequest.getHeader(String name).

    headerValues - a java.util.Map that maps header names to a String[] of all values for that header.

    cookie - a java.util.Map that maps cookie names to a single Cookie object. Cookies are retrieved to the HttpServletRequest.getCookies().shared the same name in multiple cookies, an implementation used the array of Cookie objects returned by the getCookies() method.

    initParam - a java.util.Map that maps context initialization parameter names to their String parameter value that obtained by callingServletContext.getInitParameter (String name)).

    Examples:

    The request's URI obtained from HttpServletRequest:

    ${pageContext.request.requestURI}

    The value of the numberOfItems property of the session-scoped attribute named cart:

    ${sessionScope.cart.numberOfItems}

    The value of the numberOfItems property of the session-scoped attribute named cart:

    ${sessionScope.cart.numberOfItems}

    The context path:

    ${pageContext.request.contextPath}

    The session-scoped attribute named 'profile' (null if not found):

    ${sessionScope.profile}

    The String value of the productId parameter, or null if not found:

    ${param.productId}

    The value of the productId request parameter:

    ${param["productId"]}

    The String[] containing all values of the productId parameter, or null if not found:

    ${paramValues.productId}

    A collection's members can be accessed using square brackets as retrieval of the userName parameter from the param object. Members of an array or List can be accessed if the value in square brackets can be used to an int.

    Customer Profile for ${param["userName"]}...
    
    

    Maps can be accessed using the dot operator OR square brackets:

    ${param.userName} is EQUIVALENT to ${param["userName"]}. 
    

    The host HTTP attribute:

     
    ${header["host"]}
    

    Accessing a page-scoped object that is called pageColor:

     < body bgcolor="${pageScope.pageColor}" >

    it is equivalent to:

     < body bgcolor="${pageScope['pageColor']}" >

    JavaServer Faces technology uses the EL for the following functions:

    • Deferred and immediate evaluation of expressions

    • The ability to set as well as get data

    • The ability to invoke methods

    The EL provides a way to use simple expressions to perform the following tasks:

    • Dynamically read application data stored in JavaBeans components, various data structures, and implicit objects

    • Dynamically write data, such as user input into forms, to JavaBeans components

    • Invoke static and public methods

    • Dynamically perform arithmetic operations

    The EL is used to specify the type of expressions that a custom tag attribute used:

    Immediate evaluation expressions or deferred evaluation expressions - An immediate evaluation expression is evaluated at once, such as JavaServer Faces. A deferred evaluation expression can be evaluated later by the underlying technology using the EL.

    Value expression or method expression - A value expression references data, whereas a method expression invokes a method.

    Rvalue expression or lvalue expression - An rvalue expression can only read a value, whereas an lvalue expression can both read and write that value to an external object.

© 2015 by Learncertification All Rights Reserved. The certification names are the trademarks of their respective owners. Terms & Privacy Policy