EL表達式替代jsp表達式。因為開發jsp頁面的時候遵守原則:在jsp頁面中盡量少些甚至不寫java代碼。
EL表達式作用:向瀏覽器輸出域對象中的變量或表達式計算的結果
基本語法: ${變量或表達式} 代替<%=變量或表達式%>
<body>
<%
String name = "eric";
//把變量放入域對象中
//pageContext.setAttribute("name",name);
pageContext.setAttribute("name",name,PageContext.REQUEST_SCOPE);
%>
<%=name %>
<br/>
EL表達式: ${name }<br/>
<%--
1)EL從四個域中自動搜素
${name} 等價于: <%=pageContext.findAttribute("name")%>
--%>
<%--
2)EL從指定域中獲取
pageScope: page域
requestScope: request域
sessionScope: session域
applicationScope: application域
--%>
指定域獲取的EL: ${pageScope.name }
</body>
<body>
<%--
1)普通的字符串
--%>
<%
String email = "zhangsan@qq.com";
//一定要把數據放入域中
pageContext.setAttribute("email",email);
%>
普通字符串: ${email }
<hr/>
<%--
2)普通的對象
EL表達式的屬性表示調用對象的getXX方法.例如 student.name 調用了Stduent對象的getName()方法
--%>
<%
Student student = new Student("eric","123456");
pageContext.setAttribute("student",student);
%>
普通的對象: ${student}<br/>
對象的屬性: ${student.name } - ${student.password }
<hr/>
<%--
3)數組或List集合
--%>
<%
//數組
Student[] stus = new Student[3];
stus[0] = new Student("jacky","123456");
stus[1] = new Student("rose","123456");
stus[2] = new Student("lucy","123456");
pageContext.setAttribute("stus",stus);
//List
List<Student> list = new ArrayList<Student>();
list.add(new Student("eric","123456"));
list.add(new Student("lily","123456"));
list.add(new Student("maxwell","123456"));
pageContext.setAttribute("list",list);
%>
數組: <br/>
${stus[0].name } - ${stus[0].password }<br/>
${stus[1].name } - ${stus[1].password }<br/>
${stus[2].name } - ${stus[2].password }<br/>
List集合<br/>
${list[0].name } - ${list[0].password }<br/>
${list[1].name } - ${list[1].password }<br/>
${list[2].name } - ${list[2].password }<br/>
<hr/>
<%--
4) Map集合
--%>
<%
Map<String,Student> map = new HashMap<String,Student>();
map.put("001",new Student("eric","123456"));
map.put("002",new Student("jacky","123456"));
map.put("003",new Student("rose","123456"));
pageContext.setAttribute("map",map);
%>
Map集合: <br/>
${map['001'].name } - ${map['001'].password }<br/>
${map['002'].name } - ${map['002'].password }<br/>
${map['003'].name } - ${map['003'].password }<br/>
</body>
<body>
<%--
1)算術表達式: + - * /
--%>
<%
int a = 10;
int b = 5;
pageContext.setAttribute("a",a);
pageContext.setAttribute("b",b);
%>
${a+b }<br/>
${a-b }<br/>
${a*b }<br/>
${a/b }
<hr/>
<%--
2)比較表達式: > < >= <= ==
--%>
${a>b }<br/>
${a==b }<br/>
<hr/>
<%--
3)邏輯表達式: && (與) || (或) !(非)
--%>
${true&&true }<br/>
${true||false }<br/>
${!true }<br/>
${!(a>b) }
<hr/>
<%--
4)判空表達式 empty
null: ==null
空字符串: ==""
--%>
<%
//String name1 = null;
String name1 = "";
pageContext.setAttribute("name1",name1);
%>
null: ${name1==null }<br/>
空字符串: ${name1=="" }<br/>
null或空字符串: ${name1==null || name1=="" }
<br/>
null或空字符串:${empty name1}
</body>