[HTML] 纯文本查看 复制代码
<h3>存取是普通的单值数据</h3>
<%
//pageContext.setAttribute("name", "pValue");
//request.setAttribute("name", "rValue");
//session.setAttribute("name", "sValue");
application.setAttribute("name", "aValue");
%>
<%=pageContext.getAttribute("name") %> <!-- 如果没找到 返回null -->
<%=request.getAttribute("name") %>
<%=session.getAttribute("name") %>
<%=application.getAttribute("name") %>
<hr/>
${ pageScope.name } <!-- 返回的是"" -->
${ requestScope.name }
${ sessionScope.name }
${ applicationScope.name }
<hr/>
${ name } <!-- 类似findAttribute("name") 先从page域中查找,没找到去request域中查询,没有找到去session域中找,没有找到就去application域中找 -->
<h3>获取数组的数据</h3>
<%
String[] arrs = {"李旭华","李冠希","杨凤","杨如花"};
pageContext.setAttribute("arrs", arrs);
%>
${ arrs[0] }
${ arrs[1] }
${ arrs[2] }
${ arrs[3] }
<h3>获取List集合的数据</h3>
<%
List<String> list = new ArrayList<String>();
list.add("李芙蓉");
list.add("杨芙蓉");
list.add("王凤");
pageContext.setAttribute("list", list);
%>
${ list[0] }
${ list[1] }
${ list[2] }
<h3>获取Map集合的数据</h3>
<%
Map<String,String> map = new HashMap<String,String>();
map.put("aaa","李旭华");
map.put("bbb","杨久君");
map.put("ccc","李芮");
map.put("ddd","李凤");
pageContext.setAttribute("map", map);
%>
${ map.aaa }
${ map.bbb }
${ map.ccc }
${ map.ddd }
<h3>获取对象的数据</h3>
<%
User user = new User(1,"aaa","123");
pageContext.setAttribute("user", user);
%>
${ user.id }
${ user.username }
${ user.password }
<h3>获取对象的集合的数据</h3>
<%
User user1 = new User(1,"aaa","123");
User user2 = new User(2,"bbb","123");
User user3 = new User(3,"ccc","123");
List<User> userList = new ArrayList<User>();
userList.add(user1);
userList.add(user2);
userList.add(user3);
pageContext.setAttribute("userList", userList);
%>
${ userList[0].id } - ${ userList[0].username } - ${ userList[0].password }<br/>
${ userList[1].id } - ${ userList[1].username } - ${ userList[1].password }<br/>
${ userList[2].id } - ${ userList[2].username } - ${ userList[2].password }<br/>
[HTML] 纯文本查看 复制代码
<h1>EL的功能二:执行运算</h1>
<h3>EL执行算数运算</h3>
<%
pageContext.setAttribute("n1", "10");
pageContext.setAttribute("n2", "20");
pageContext.setAttribute("n3", "30");
pageContext.setAttribute("n4", "40");
%>
${ n1 + n2 + n3 }
<h3>EL执行逻辑运算</h3>
${ n1 < n2 } - ${ n1 lt n2 } <!-- less than --><br/>
${ n1 > n2 } - ${ n1 gt n2 } <!-- great than --><br/>
${ n1 <= n2 } - ${ n1 le n2 } <!-- less equal --><br/>
${ n1 >= n2 } - ${ n1 ge n2 } <!-- great equal --><br/>
${ n1 == n2 } - ${ n1 eq n2 } <!-- equal --><br/>
<h3>EL执行关系运算</h3>
${ n1<n2 && n3 < n4 } - ${ n1<n2 and n3 < n4 }<br/>
${ n1<n2 || n3 < n4 } - ${ n1<n2 or n3 < n4 }<br/>
${ !(n1 < n2) } - ${ not(n1<n2) }
<h3>EL执行三元运算</h3>
${ n1 < n2 ? "正确":"错误" }
<h3>empty运算</h3>
${ user == null } - ${ empty user }
${ user != null } - ${ not empty user }
【EL操作WEB开发的常用对象11个】
<h1>EL功能三:操作WEB开发常用的对象</h1>
<!--
pageScope,requestScope,sessionScope,applicationScope - 获取JSP中域中的数据
param,paramValues - 接收参数.
header,headerValues - 获取请求头信息
initParam - 获取全局初始化参数
cookie - WEB开发中cookie
pageContext - WEB开发中的pageContext.
-->
<h3>接收请求的参数</h3>
<%= request.getParameter("id") %>
<%= request.getParameter("name") %>
<%= Arrays.toString(request.getParameterValues("hobby")) %>
<hr/>
${ param.id }
${ param.name }
${ paramValues.hobby[0] }
${ paramValues.hobby[1] }
<h3>获取请求头</h3>
<%= request.getHeader("User-Agent") %>
<hr/>
${ header["User-Agent"] }
<h3>获取全局初始化参数</h3>
${ initParam.username }
<h3>获取Cookie中的值</h3>
${ cookie.history.value }
<h3>获取PageContext中的对象</h3>
IP地址:${ pageContext.request.remoteAddr }
工程路径:${ pageContext.request.contextPath }