1. Create context

Declare a JavaBean for storing user information

public class User {
    private String id; / / user id
    private Bank bank; // Bank information
    
    // omit get set
}

public class Bank {
    private String name;   // Bank name
    private String number; // Bank card number
}
Copy the code

We can use JSP standard actions to get the user ID

<jsp:getProperty name="user" property="id"/>
Copy the code

But if we want to get the user’s bank card number, the JSP standard action does not provide such a nested access mechanism, so the only way to achieve this functionality is to get it in Java code in the JSP page.

In the MVC design pattern, JSPS are just views, and the view’s job is to display the response, not to do anything about program control and business logic in the JSPS. So there should be as little or no Java code in your JSP pages.

After JSP 2.0, EL (Expression Language) expressions can be used to handle such problems. EL expression provides a way to simplify expressions in JSP, the purpose is to minimize the Java code in JSP pages, make JSP page processing procedures written more concise, easy to develop and maintain.

2. Components

2.1 EL expression syntax

  • ${ expression }

Expression Specifies what to output, which can be a string or an expression consisting of the EL operator

2.2 EL operator

${expression} ${expression} ${expression} ${expression} ${expression} ${expression} ${expression}

2.2.1 Arithmetic operators

Arithmetic operator instructions
+ add
Reduction of
* take
/ or div In addition to
% or mod Take more than

+ is used only as an arithmetic operator in EL expressions, not as a string concatenator

2.2.2 Relational operators

Relational operator instructions
= = or eq Is equal to the
! = or ne Is not equal to
< or lt Less than
> or gt Is greater than
< = or le Greater than or equal to
> = or ge Less than or equal to

2.2.3 Logical operators

Logical operator instructions
&& or the and With the operation
| | or or Or operation
! Or not The operation

2.2.4 Conditional operators

Conditional operator instructions
? : ? If the preceding conditional expression is true, the preceding expression is executed; otherwise, the following expression is executed

2.2.5 Value operators

Value operator instructions
. Access properties in an object
[] Access properties in an object

Access an array or collection

Get the value of a variable dynamically

When the attribute name contains special characters such as -,., and, only the [] operator can be used

2.2.6 Validate operators

Validation operator instructions
empty Return true in the following cases

null

String of zero length

A collection or array without elements

2.3 Access data from four domains

The four fields are pageContext, Request, Session, and Application. When we are not using the built-in objects in Section 2.4, we are accessing data from the four fields

  • ${ varName }The execution process of

2.3.1 Accessing basic data types

<%
    pageContext.setAttribute("id".1);
    request.setAttribute("age".18);
    session.setAttribute("name"."zhangfei");
    application.setAttribute("weight".75.5);
%>

${ id } <br>
${ age } <br>
${ name } <br>
${ weight } <br>
Copy the code

The execution result

When the same attribute name exists in different fields, it will be obtained according to the above process

<%
    pageContext.setAttribute("name"."pageContext");
    request.setAttribute("name"."request");
    session.setAttribute("name"."session");
    application.setAttribute("name"."application");
%>

${ name }
Copy the code

We’re accessing the values in the pageContext field

Next we comment out pageContext

The < %// pageContext.setAttribute("name", "pageContext");
    request.setAttribute("name"."request");
    session.setAttribute("name"."session");
    application.setAttribute("name"."application");
%>

${ name }
Copy the code

The execution result

When we comment out the request, we’re accessing the session, and when we comment out the session, we’re accessing the application, we’re commenting out all of it, what’s going to happen?

The < %// pageContext.setAttribute("name", "pageContext");
// request.setAttribute("name", "request");
// session.setAttribute("name", "session");
// application.setAttribute("name", "application");% >Copy the code

It can be found that when using EL expression to access data in the four domains, data will be obtained from pageContext, Request, Session and application successively, and will not be displayed if the data cannot be obtained.

Is there a way to get data from a given domain? Yes, we will cover that in section 2.4.2.

Next, we will use the arithmetic operators. The relational and logical operators are used in a similar way, so we won’t show you

<%
    request.setAttribute("name"."zhangfei");
%>

${ name + 1 }
Copy the code

There will be a Java. Lang. A NumberFormatException, we have said before is verified + only as arithmetic operators in the EL expression, but not as a string concatenation operator

Use integer attributes for arithmetic operations

<%
    request.setAttribute("age".18);
%>

${ age + 1 }
Copy the code

Results the correct

Now let’s use a conditional expression

<%
    request.setAttribute("name"."zhangfei");
%>

${ name.equals("zhangfei")?"yes" : "no" }
Copy the code

2.3.2 Accessing objects

A new User class

public class User {
    private int age;
    private String name;
    
    // omit get set
}
Copy the code

The User class cannot be placed under the default package (SRC), otherwise Classes from the default package must not be referenced from JSP file

We can use the value operator. [] to get attributes in an object

<%
    User user = new User(18."zhangfei");
    session.setAttribute("user", user);
%>

${ user.name } <br>
${ user.["name"] } <br>
Copy the code

You can also use [] to get the value of a variable dynamically

<%
    request.setAttribute("var"."name");
%>

${ user[var]}Copy the code

Java identifiers cannot contain special symbols such as -,., and, etc.

2.3.3 Accessing an Array or collection

We can use the value operator [] to access arrays or collections

<%
    List<String> list = new ArrayList<>();
    list.add("C");
    list.add("Java");
    list.add("C++");
    request.setAttribute("list", list);

    String[] array = new String[]{"C"."Java"."C++"};
    session.setAttribute("array", array);
%>

${ list[0] } <br>
${ array[1] } <br>
Copy the code

The execution result

Next we use the validation operator

<%
    request.setAttribute("object".null);
    request.setAttribute("str1"."");
    request.setAttribute("str2"."value");
    request.setAttribute("arr1".new String[]{});
    request.setAttribute("arr2".new String[]{"element"});
    request.setAttribute("list1".new ArrayList<>());
    request.setAttribute("list2", Collections.singleton("element"));
%>

${ empty object } <br>
${ empty str1 } <br>
${ empty str2 } <br>
${ empty arr1 } <br>
${ empty arr2 } <br>
${ empty list1 } <br>
${ empty list2 } <br>
Copy the code

Accessing the Map collection

<%
    Map<String, String> map = new HashMap<>();
    map.put("o.name"."zhangfei");

    request.setAttribute("map", map);
%>

${ map.o.name }
Copy the code

The [] operator is used to access the data because the attribute name contains a special character

<%
    Map<String, String> map = new HashMap<>();
    map.put("o.name"."zhangfei");

    request.setAttribute("map", map);
%>

${ map["o.name"]}Copy the code

2.4 Accessing data from built-in objects

Built-in objects can be divided into six categories:

  • JSP
  • scope
  • Request parameters
  • Request header
  • Cookie
  • Initialization parameter

With the exception of the pageContext built-in object, the built-in object is essentially a Map class

Against 2.4.1 JSP

  • pageContext

(1) Essence

javax.servlet.jsp.PageContext
Copy the code

(2) Use

A more common use is to get the project path

${ pageContext.request.contextPath }
Copy the code

2.4.2 scope

  • pageScope
  • requestScopre
  • sessionScope
  • applicationScope

(1) Essence

pageContext.getAttribute(var);
request.getAttribute(var);
session.getAttribute(var);
application.getAttribute(var);
Copy the code

(2) Use

Again, use the above example, but this time we get the data from the specified field

<%
    pageContext.setAttribute("name"."pageContext");
    request.setAttribute("name"."request");
    session.setAttribute("name"."session");
    application.setAttribute("name"."application");
%>

${ pageScope.name } <br>
${ requestScope.name } <br>
${ sessionScope.name } <br>
${ applicationScope.name } <br>
Copy the code

2.4.3 Request Parameters

  • param
  • paramValues

(1) Essence

request.getParameter(var);
request.getParameterValues(var);
Copy the code

(2) Use

param.html

<form action="param.jsp" method="POST">Name:<input type="text" name="userName"> <br>Hobbies:<input type="checkbox" name="hobby" value="C">C
    <input type="checkbox" name="hobby" value="Java">Java
    <input type="checkbox" name="hobby" value="C++">C++ <br>
    <input type="submit" value="Submit">
</form>
Copy the code

param.jsp

${ param.userName } <br>
${ paramValues.hobby[0] } <br>
Copy the code

The results

2.4.4 request header

  • header
  • headerValues

(1) Essence

request.getHeader(var);
request.getHeaders(var);
Copy the code

(2) Use

${ header.Host } <br>
${ headerValues.Cookie[0]}Copy the code

2.4.5 cookies

  • cookie

(1) Essence

Cookie[] cookies = request.getCookies();
Map<String, Cookie> map = new HashMap<>();
for (Cookie cookie : cookies) {
    map.put(cookie.getName(), cookie);
}
Copy the code

(2) Use

${ cookie.JSESSIONID.name } <br>
${ cookie.JSESSIONID.value } <br>
Copy the code

2.4.6 initParam

  • initParam

(1) Essence

servletContext.getInitParameter(var)
Copy the code

(2) Use

Configure the initialization parameters in web.xml

<web-app>
    <context-param>
        <param-name>key1</param-name>
        <param-value>value1</param-value>
    </context-param>

    <context-param>
        <param-name>key2</param-name>
        <param-value>value2</param-value>
    </context-param>
</web-app>
Copy the code

initParam.jsp

${ initParam.key1 } <br>
${ initParam.key2 } <br>
Copy the code