@Java Academic Academic Party

Everybody is good! I am your old friend Java Academic party, today to share with you the most complete JavaWeb EL technology. The EL Expression Language is a simple language that provides a way to simplify expressions in JSPS in order to minimize the Java code in JSP pages, making JSP page handlers simpler to write and easier to develop and maintain. Without saying much, I will introduce the EL technology in JavaWeb in detail.

1. Introduction to the EL Tool package

  • Develop a JAR package with Java technology
  • Function is to reduce the JSP file development Java command development intensity
  • The Tomcat server has an EL tool package (/lib/el-api.jar).

2.JSP file function

Instead of the response body object, write the result of doGet,doPost execution in the Servlet to the response body

3. Main development steps in JSP files

The scoped processing results are read and written to the response body

<% Strint value = (String)(request.getSAttribute("key1")); % >Copy the code

Step 1: Read the result of the processing from the specified scope object

Step 2: Type the obtained data strongly

Step 2: Write the transformed data to the response body

3. Introduction to EL expressions

1. Command format (this is not a reference to an object attribute) :

${Scope object alias. Shared data}

2. Function of command:

  • EL expressions are a special command format provided by the EL toolkit.
  • EL expressions are used on JSP files
  • Is responsible for reading the specified shared data from the scope object on the JSP file and output to the response body

4.JSP files can use the scope of the lost object

Note: JSPS have four scope objects and servlets have three scope objects

5.EL expressions provide the scope object alias

6. The EL expression writes the reference object property to the response body

  1. Command format:

    ${Scope object alias. Share data name. Attribute name}

    For example: ${applicationScope. Key1. Sname}

  2. Function This command reads the property value of a reference object for a specified shared data association from a scope object.

    And automatically writes the result of the property value to the response body.

  3. Property name: Be sure to match exactly the name of the reference type property, including case.

    This property can be called directly whether it is private or public

    Because EL expressions use the reflection mechanism, through the reflection mechanism

    Gets the properties in the current class.

The EL expression does not provide a method to traverse the collection, so the collection content output cannot be read from the scoped object

7. Simplified version of EL expressions

  1. Simplified command format:

    ${share data name}

  2. EL expressions allow developers to omit scoped object aliases

  3. How it works: The EL expression simplification uses the guess algorithm because it does not specify a scope object.

  4. Existence hidden danger

    • Easy to slow down the execution of the program (because you have to search in each scope)
    • Location errors may occur
  5. Application scenarios

    It is designed to make it easy to read and output shared data from pageContext

  6. Although the simplified version of EL expressions has many pitfalls, in the actual development process, developers generally use the simplified version rather than the standard version in order to save time. Go home early from work.

8.EL expressions ——- supports operational expressions

Premise: Sometimes in THE JSP file, it is necessary to perform an operation on the read shared data and write the operation result to the response body.

  1. Operation expression:

  1. Relational operations:

When you’re doing this, you can write > or you can write GT and it’s the same thing

  1. Logical operations: && | |!

9.EL expressions provide built-in objects

The first built-in object is param

  1. Command format: ${param. request parameter name}

  2. This command is used to read the parameters in the current request packet from the request object and write the parameters to the response body

  3. In lieu of a command:

    Suppose you have a file called index. JSP

    Send the request: Http://localhost:8080/myweb/index.jsp? userName=mike&password=123

    In this case, the request parameters in the request packet need to be written into the response body

    <%
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
    %>
    <%=userName%>
    <%=password%>
    Copy the code

Output results:

The second built-in object is paramValues

  1. ${paramValues. Request parameter name [subscript]}

  2. This command is used to send a request parameter that is associated with multiple values

    In this case, run paramValues to read the value at the specified position in the request parameter

    And write to the response body.

  3. In lieu of a command:

    Suppose there is a file called index_1.jsp

    Send the request: Http://localhost:8080/myweb/index_1.jsp? pageNo=1&pageNo=2&pageNo=3

    Note that this request has only one request parameter, pageOn, not three

    The pageNo request parameter exists in the request package

    PageNo: [1, 2, 3]

    <% String array[] = request.getParamterValues("pageNo"); % > <! <%=array[0]%> <%=array[1]%>Copy the code

    Output results:

10. The EL expression is abnormal

Error: the name attribute cannot be found in index_1.jsp because the global variable in Student is sname, and the name attribute cannot be found in index_1.jsp

Javax.mail. El. PropertyNotFoundException: the object is not found in the specified attributes

——– to see the big guys can pay attention to xiaobian, will always update tips, free to share with you!! ———

Click on the planet to quickly enter the Github planet!! There are even more fun technologies to explore