JAVAWeb11-server rendering technology-JSP-02-EL expression (will use)

1. Introduction to EL expressions

  1. The full name of EL expression: Expression Language, is the expression language
  2. EL expression is mainly to replace the expression script of jsp page<%=request.getAttribute("xx")%>
  3. EL expressions are more concise than jsp expression scripts when outputting data
  4. The basic syntax of EL expressions: ${key1}, you can understand that it is a syntactic sugar

2. Quick Start with EL Expressions

el_qs.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Quick introduction to el expressions</title>
</head>
<body>
<h1>Quick introduction to el expressions</h1>
<%
    request.setAttribute("name", "delta");
%>
<%-- Interpretation
    1. If name is null, request.getAttribute() returns a null string
    2. If name is null, ${<!-- -->name}, returns ""
--%>
<h1>jsp expression script</h1>
Name = <%=request.getAttribute("name") == null ? "": request.getAttribute("name")%><br/>
<h1>el-expression</h1>
name = ${<!-- -->name}<br/>
</body>
</html>
  1. When an EL expression outputs null, it outputs “”
  2. When the jsp expression script outputs null, the output is a “null” string

3. EL common output format

1) Common properties, array properties, List collection properties and map collection properties of output Bean are commonly used in EL expressions

2) EL common output application examples

  1. Demonstrates EL common output usage

book.java

public class Book {<!-- -->

    private String name;//Book title
    private String[] writer;//author
    private List<String> reader;//reader
    private Map<String, String> topics;//Comment
    //alt + insert


    public String getName() {<!-- -->
        return name;
    }

    public void setName(String name) {<!-- -->
        this.name = name;
    }

    public String[] getWriter() {<!-- -->
        return writer;
    }

    public void setWriter(String[] writer) {<!-- -->
        this.writer = writer;
    }

    public List<String> getReader() {<!-- -->
        return reader;
    }

    public void setReader(List<String> reader) {<!-- -->
        this. reader = reader;
    }

    public Map<String, String> getTopics() {<!-- -->
        return topics;
    }

    public void setTopics(Map<String, String> topics) {<!-- -->
        this.topics = topics;
    }

    @Override
    public String toString() {<!-- -->
        return "Book{" +
                "name='" + name + '\'' +
                ", writer=" + Arrays. toString(writer) +
                ", reader=" + reader +
                ", topics=" + topics +
                '}';
    }
}

el_output.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>El expression output data demo</title>
</head>
<body>
<h1>el expression output data demo</h1>
<%
    //Create a Book object and put in related properties
    //private String name;//Book title
    //private String[] writer;//author
    //private List<String> reader;//reader
    //private Map<String, Object> topics;//comments
    Book book = new Book();
    book.setName("Insect Story");
    book.setWriter(new String[]{<!-- -->"jack", "tom"});
    ArrayList<String> readers = new ArrayList<>();
    readers.add("Old Han");
    readers.add("Lao Li");
    book.setReader(readers);//put readers

    //Create topics
    HashMap<String, String> topics = new HashMap<>();
    topics.put("topic1", "This is the best cartoon I have ever seen");
    topics.put("topic2", "Good movie~~");
    book.setTopics(topics);

    //Put the book into the request domain object
    request.setAttribute("bookkey", book);

%>
book object: ${<!-- -->bookkey}<br/>
<%-- ${<!-- -->book.name} calls getName() ,
The get method must be provided, otherwise an error will be reported --%>
book.name= ${<!-- -->bookkey.name}<br/>
book.writer= ${<!-- -->bookkey.writer}<br/>
book.writer[0]= ${<!-- -->bookkey.writer[0]}<br/>

book.readers= ${<!-- -->bookkey.reader}<br/>
book.readers second = ${<!-- -->bookkey.reader.get(1)}<br/>
book.readers second = ${<!-- -->bookkey.reader[1]}<br/>

<%-- map special character key can be read with [], for example book.topics['1'] --%>
book.topics= ${<!-- -->bookkey.topics}<br/>
book.topics first comment = ${<!-- -->bookkey.topics["topic1"]}<br/>

</body>
</html>

complete test!

4. EL operation

  1. Basic syntax: ${ operation expression }
    Syntax: ${operation expression}

  2. Relational operations

  3. logic operation

  4. Arithmetic

5. The empty operation of EL

  1. The empty operation can determine whether a data is empty, if it is empty, return true, otherwise return false
  2. The following situations are empty
    ● the value is null
    ● when the value is an empty string
    ● The value is an array of type Object with length zero
    ● list set, the number of elements is zero
    ● map set, the number of elements is zero

Applications:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Operation of el empty</title>
</head>
<body>
<h1>Operation of el empty</h1>
<%
    request.setAttribute("k1", null);
    request.setAttribute("k2", "");
    request.setAttribute("k3", new Object[]{<!-- -->});
    request.setAttribute("k4", new ArrayList<>());
    request.setAttribute("k5", new HashMap<>());

    request.setAttribute("score", 70);
%>
Whether k1 is empty = ${<!-- -->empty k1}<br/>
Is k2 empty = ${<!-- -->empty k2}<br/>
Is k3 empty = ${<!-- -->empty k3}<br/>
Is k4 empty = ${<!-- -->empty k4}<br/>
Whether k5 is empty = ${<!-- -->empty k5}<br/>
Pass or not = ${<!-- -->score >= 60 ? "Pass": "Fail"}
</body>
</html>

6. Ternary operation of EL

  1. expression1?expression2:expression3
  2. Returns the value of expression 2 if expression 1 evaluates to true, otherwise returns the value of expression 3.

Applications:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>The ternary operation of el empty</title>
</head>
<body>
<h1>Ternary operation of el empty</h1>
<%
    request.setAttribute("score", 70);
%>
Pass or not = ${<!-- -->score >= 60 ? "Pass": "Fail"}
</body>
</html>

7. EL’s 11 Hidden Objects

11 hidden objects of 7.1 EL, which can be used directly

7.2 EL gets attributes in four specific domains

  1. EL four domain-specific variables
  2. Applications
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Demonstrate four commonly used hidden objects (domain objects) of el</title>
</head>
<body>
<h1>Demonstrates four commonly used hidden objects (domain objects) of el</h1>
<%
    request.setAttribute("k1", "request-k1 data");
    pageContext.setAttribute("k1", "pageContext-k1 data");
    session.setAttribute("k1", "session-k1 data");
    application.setAttribute("k1", "application-k1 data");
%>
<h1>Get by jsp script</h1>
k1 in the request field= <%=request.getAttribute("k1")%><br/>
<h1>el way to get domain object data</h1>
k1= ${<!-- -->requestScope.k1} in the request field<br/>
k1= ${<!-- -->pageScope.k1} in the pageContext field<br/>
k1= ${<!-- -->sessionScope.k1}<br/> in the session domain
k1 in the application domain = ${<!-- -->applicationScope.k1}<br/>
</body>
</html>

7.3 Use of pageContext object

  1. Introduction to pageContext Object
  2. Applications
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Use of pageContext object</title>
</head>
<body>
<h1>Use of pageContext object</h1>
<%--
    //Get data related to the HTTP protocol through the request object
    request.getScheme() It can get the protocol of the request
    request.getServerName() Get the requested server ip or domain name
    request.getServerPort() Get the requested server port number
    getContextPath() Get the current project path
    request.getMethod() Get the method of the request (GET or POST)
    request.getRemoteHost() Get the client's ip address
    session.getId() Get the unique identifier of the session


--%>
<hr/>
<%-- Interpretation
    1. We can obtain information related to the http protocol through pageContext.request.xx
    2. Equivalent to replace request.getMethod()....
--%>
Protocol: ${<!-- --> pageContext.request.scheme }<br>
Server ip: ${<!-- --> pageContext.request.serverName }<br>
Server port: ${<!-- --> pageContext.request.serverPort }<br>
Project path: ${<!-- --> pageContext.request.contextPath }<br>
Request method: ${<!-- --> pageContext.request.method }<br>
Client ip address: ${<!-- --> pageContext.request.remoteHost }<br>
Session id: ${<!-- --> pageContext.session.id }<br>
<h1>Use jsp expression script to get the above information</h1>
ip address: <%=request.getRemoteHost() %><br>
<h1>Use el expression form to obtain information-simplified writing</h1>
<%
    pageContext.setAttribute("req", request);
%>
ip address (simplified acquisition): ${<!-- -->req.remoteHost}<br>
Get request method (simplified get): ${<!-- -->req.method}<br>
</body>
</html>