[Servlet & amp; JSP] Expression Language EL

Source: Internet
Author: User
Tags tld

[Servlet & JSP] Expression Language EL
EL Introduction

Some simple operations or judgments on the acquisition of some simple attributes, request parameters, headers, and cookies in JSP can be processed using the expression language, you can also compile some common function functions as EL functions to reduce some Scriptlet on the web page.

For example, for the following Scriptlet:

<%    String a = request.getParameter("a");    String b = request.getParameter("b");    out.println("a + b = " + (Integer.parseInt(a) + Integer.parseInt(b)));%>

If EL is used, only one line of program code can be used to rewrite or even enhance the Scriptlet. For example:

$ {Param. a} + $ {param. B} =$ {param. a + param. B} 

EL uses $ {And} to include the expression to be processed. You can use the dot operator (.) To specify the attribute to be accessed, and use the plus sign (+) operator to perform addition operations. Param is one of the hidden objects of EL, indicating the user's request parameters, param. a Indicates to obtain the value of request parameter a sent by the user. The input request parameter is automatically converted to the basic type and operated.

When the parameter is null, EL directly uses a null string and does not directly display the null value. When an operation is performed, no error is thrown.

EL's point operators can continuously access objects, just as in Java code. For example, you need to write as follows:

<%= ((HttpServletRequest)pageContext.getRequest()).getMethod() %><%= ((HttpServletRequest)pageContext.getRequest()).getQueryString() %><%= ((HttpServletRequest)pageContext.getRequest()).getRemoteAddr() %>

With EL, you can write it as follows:

${pageContext.request.method}${pageContext.request.queryString}${pageContext.request.remoteAddr}

PageContext is also one of the hidden objects of EL. The xxx name in the street after the dot operator indicates that the getXxx () method is called by the handler. If the type must be converted, EL will also handle it by itself.

You can use page to indicate the isELIgnored attribute of the element (true by default) to set whether the JSP page uses EL. The reason may be that the webpage already contains the $ {} syntax similar to EL. For example, a Template framework is used.

You can also set it in web. xml. If the label is true, EL is not used. For example:

<web-app ...>    <jsp-config>        <jsp-property-group>            <url-pattern>*.jsp</url-pattern>            <el-ignored>true</el-ignored>        </jsp-property-group>    </jsp-config></web-app>

The preceding labels are used to set compliance Whether or not EL is used for JSP web pages. If the JSP page uses the isElIgnored of the page command element to set whether EL is supported, the page command element is used as the main setting.

Use EL to access attributes

In EL, you can use the EL implicit object to specify a range to access attributes. If the range of attributes is not specified, the attribute specified in EL is searched in the order of page, request, session, and application by default.

If EL accesses an array object or List object, you can use the [] operator to specify the index to access the elements in the array.

Summary of the use of vertex operators and [] operators:

If the dot operator is used, the left side can be a JavaBean or Map object. If the [] operator is used, the left side can be a JavaBean, Map, array, or List object.

When the left side is a Map object, we recommend that you use the [] Operator, because the key name may have white space or dot characters when setting the Map.

EL implicit object

11 implicit objects are provided in EL. Except that the pageContext bank object corresponds to pageContext, other implicit objects correspond to the Map type.

Implicit pageContext object

Corresponding to the PageContext type, PageContext itself is a JavaBean. As long as it is the getXxx () method, it can be obtained using $ {pageContext. Xxx.

Hidden objects related to attribute ranges

The hidden objects related to the property range include pagination, requestScope, sessionScope, and applicationScope. The setAttribute () of pageContext, request, session, and application () the property object set by the method. If you do not use the EL implicit object to specify the scope of the action, the query starts from the property of pagination by default.

EL implicit objects pagination, requestScope, sessionScope, and applicationScope are not the same as JSP implicit objects pageContext, request, session, and application. EL implicit objects only represent the scope of action.

Implicit object related to Request Parameters

The EL implied objects related to request parameters are param and paramValues. Corresponds to the getParameter () and getParameterValues () Methods of the request.

Implicit object related to the Header

If you want to obtain the header data of a user request, you can use the header or headrValues implicit object. Corresponds to the getHeader () and getHeaders () Methods of the request.

Cookie implicit object

The cookie implicit object can be used to obtain the user's Cookie setting value. If the username attribute is set in the Cookie, you can use $ {cookie. username} to obtain the value.

Implicit object of initialization parameters

InitParam can be used to obtain the initial ServletContext parameter set in web. xml, that is .

EL operator Arithmetic Operator
+-* (// Or div) (% or mod )? : Logical operators
And or not Relational operators
Less than: <or lt
Less than or equal to: <= or le
Greater than:> or gt
Greater than or equal to:> = or ge
Equal to: = or eq

EL operator priority corresponds to Java Operator

EL UDF

The first step of a UDF is to compile a class. It must be a public class, and the method to be called must also be public and static. For example:

public class Infix {    public static double eval(String infix) {        double result = 0;        //other process        return result;    }}

The web Container must know how to use the eval () method of this class as an EL function. Therefore, you must write a Tag Library description file (TLD, this file is an XML file with the extension *. tld.

Infix. tld:

 
   
  
   1.0
    
  
   infix
    
  
   http://club.chuxing/infix
       
      
   
    Eval Infix
        
   
    eval
            
   
            cc.openhome.InFix        
       
   
            double eval(java.lang.String)       
     
  
 

In $ {infix: eval (...)}, the eval name corresponds Label settings. In fact, the class executed behind the eval name and the real static method are And Set.

You can put this TLD file directly under the WEB-INF folder, and the container will automatically locate the TLD file and load it. Now you can write a JSP to use this custom EL function, for example:
Express. jsp:

<%@ page language="java" contentType="text/html; utf-8"    pageEncoding="utf-8"%><%@taglib uri="http://club.chuxing/infix" prefix="infix" %>  
$ {Param. expression }=$ {infix: eval (param. expression }}

Taglib indicates that the container will use the custom function corresponding to the uri attribute when translating JSP, And the prefix is used to set the prename. When JSP contains EL custom functions from multiple designers, you can avoid name conflicts.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.