Java web learning log (EL expression and JSTL Tag Library)

Shang Silicon Valley Learning Notes

EL expression

What is an EL expression

The full name of EL expression is Expression Language.

Function of EL expression: EL expression mainly replaces the expression script in jsp page to output data in jsp page.

Syntax of EL expression: ${expression}

EL expressions are much more concise than jsp expression scripts when outputting data.

In addition, when the EL expression outputs a null value, it outputs an empty string. jsp will output null.

The order in which EL expressions search for prime domain objects

When there are data of the same key in the four fields, the EL expression will search and output from small to large in the four fields.


The EL expression outputs the common attributes and array attributes of the Bean. List set attribute, map set attribute

Person class


Note: when an EL expression obtains a property, its essence is to call the corresponding get method. If there is no get method of the property, an error will be reported.

EL expression (operation)

Syntax: ${operation expression}, EL expression supports the following operators

  1. Relational operation
Relational operator explain example result
==Or eq be equal to ${5 = = 5} or ${5 eq 5} true
!= Or ne Not equal to ${5! = 5} or ${5 ne 5} false
< or lt less than ${3 < 5} or ${3 lt 5} true
>Or gt greater than ${2 > 10} or ${2 gt 10} false
< = or le Less than or equal to ${5 < = 12} or ${5 le 12 true
>=Or ge Greater than or equal to ${3 > = 5} or ${3 ge 5} false
  1. Logical operation
Logical operator explain example result
&&Or and And operation ${12 = = 12 & & 12 < 11} or ${12 = = 12 and 12 < 11} false
||Or or Or operation 12 = = 12 ∥ ∥ 12 < 11 or {12 = = 12 \ | \ | 12 < 11} or 12 = = 12 ‖ ‖ 12 < 11 or {12 = = 12 or 12 < 11} true
! Or not Inverse operation ${true or $} false
  1. Arithmetic operation
Arithmetic operator explain example result
+ addition ${ 12 + 18 } 30
- subtraction ${ 18 - 8 } 10
* multiplication ${ 12 * 12 } 144
/Or div division ${144 / 12} or ${144 div 12} 12
%Or mod Take mold ${144% 10} or ${144 mod 10} 4
  1. empty operation

Expression: ${empty element}

Empty operation can judge whether a data is empty. If it is empty, it will output true and if not, it will output false.

The following cases are empty:

  • When the value is null

  • When the value is an empty string

  • The value is an array of Object type. When the length is zero

  • list set, the number of elements is zero

  • map set, the number of elements is zero

  1. Ternary operation

Expression 1? Expression 2: expression 3

If the value of expression 1 is true, return the value of expression 2; Otherwise, expression 3 is returned.

  1. Dot operation and bracket operator

Point operation, you can output the value of an attribute in the Bean object

[] bracket operation, which can output the value of an element in an ordered set

And [] bracket operation can also output the value of the key containing special characters in the key in the map set.

For example:

11 implicit objects of EL expression

variable type effect
pageContext PageContextImpl It can get nine built-in objects in jsp
pageScope Map<String,Object> It can get the data in the pageContext field
requestScope Map<String,Object> It can get the data in the Request field
sessionScope Map<String,Object> It can obtain the data in the Session domain
applicationScope Map<String,Object> It can get the data in the ServletContext field
param Map<String,String> It can get the value of the request parameter
paramValues Map<String,String[]> It can also obtain the value of the request parameter, which can be used when obtaining multiple values.
header Map<String,String> It can get the information of the request header
headerValues Map<String,String[]> It can get the information of the request header, and it can get multiple values
cookie Map<String,Cookie> It can get the Cookie information of the current request
initParam Map<String,String> It can be obtained on the web < context param > context parameters configured in XML

Get data in four fields


Use of pageContext object

1.agreement: ${ req.scheme }<br> 
2.The server ip: ${ pageContext.request.serverName }<br> 
3.Server port: ${ pageContext.request.serverPort }<br>
4.Get project path: ${ pageContext.request.contextPath }<br> 
5.Get request method: ${ pageContext.request.method }<br> 
6.Get client ip Address: ${ pageContext.request.remoteHost }<br>
7.Gets the of the session id No.: ${ }<br>

JSTL tag library

The full name of JSTL tag library refers to JSP standard tag library. Is a constantly improving open source JSP tag library.

EL expression is mainly used to replace the expression script in jsp, while tag library is used to replace the code script. This makes the whole jsp page more concise.

JSTL five tag libraries

Functional scope URL prefix
Core tag library (key) c
format fmt
function fn
Database (not used) sql
XML (not used) x

Steps for using JSTL

  1. Import the jar package of JSTL tag library



  1. Using taglib instruction to import tag library

Use of core library

  • < C: set / >: data can be saved to the domain
// scope: set field var: set Key value: set value
<c:set scope="session" var="key" value="2020"/>
  • < C: if / >: it can be used to judge if
<c:if test="${ sessionScope.key == 2020 }"> 
    <h1>2020 year</h1>
<c:if test="${ sessionScope.key == 2021 }">
    <h1>2021 year</h1>
  • < C: choose > < C: when > < C: otherwise >: Branch judgment, similar to switch statement
choose: Tag start selection judgment 
when: The label indicates each judgment
test: Property represents the value of the current judgment 
otherwise: The label indicates the rest 
<c:choose> <c:when> <c:otherwise>Points needing attention when using the label: 1. It cannot be used in the label html Notes, to use jsp notes 
2,when The parent label of the label must be choose label
    <c:when test="${a%4==0}">
        <h1>leap year</h1>
    <c:when test="${a%4!=0}">
        <h1>Ordinary year</h1>
        Make up
  • < C: foreach / >: traversal output
1.You can traverse the loop
begin: Set start index
end: Set end index
var: The variable representing the loop (also the data currently being traversed)
items: Represents the traversed data source (traversed Collection)
step: Represents the step value of traversal
varStatus: Indicates the data status currently traversed (you can obtain the data currently traversed, index, number of traversed, etc.)
<table border="1">
    <c:forEach begin="1" end="10" var="i">
        <tr><td>The first ${i}that 's ok</td></tr>
2.Can traverse Object array
request.setAttribute("arr", new String[]{"xxxxxxx","yyyyyyy","zzzzzzz"});
<c:forEach items="${ requestScope.arr }" var="item"> ${ item }  
3.ergodic Map aggregate
Map<String,Object> map = new HashMap<String, Object>(); 
map.put("key1", "value1"); 
map.put("key2", "value2"); 
map.put("key3", "value3");
request.setAttribute("map", map);
<c:forEach items="${ }" var="entry"> <h1>${entry.key} = ${entry.value}</h1> 

Tags: Java JSP

Posted by TechGuru on Mon, 02 May 2022 16:53:44 +0300