JSP 与EL 的对比与EL的使用
<%@page import="java.util.*"%>
<%@page import="com.neuedu.entity.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%--
EL表达式:
语法:${....}
1、访问作用域中的对象(pageContext,request,session,application)
2、计算并输出,当值为null或" "时页面显示空白
--%>
<h1>1、访问作用域中的对象--------访问对象</h1>
<%-- ① 存储值--%>
<%
pageContext.setAttribute("pageAttri", 100);
request.setAttribute("requestAttri", 200);
session.setAttribute("sessionAttri", 300);
application.setAttribute("applicationAttri", 400);
pageContext.setAttribute("attri", 100);
request.setAttribute("attri", 200);
session.setAttribute("attri", 300);
application.setAttribute("attri", 400);
%>
<%--② jsp中取值 --%>
<%=pageContext.getAttribute("pageAttri") %><br/><br/>
<%--② EL取值 --%>
${pageAttri}<br/>
${requestAttri}<br/>
${sessionAttri}<br/>
${applicationAttri}<br/><br/>
${attri}<br/><!-- 当在不同的作用域中,属性名称重复时,默认获取最小作用域范围的值 ,之前有我的博客中有这些对象的说明即使用-->
<h1>2.访问作用域中的对象-----精确访问对象</h1>
<%--page也就是pageContext,scope是范围的意思,这pageContext这个范围直接中的attri --%>
${pageScope.attri}<br/>
${requestScope.attri}<br/>
${sessionScope.attri}<br/>
${applicationScope.attri}<br/>
<h1>3.访问作用域中的对象-----访问JavaBean对象</h1>
<%
User user = new User();
user.setUsername("tomcat");
user.setPassword("123456");
session.setAttribute("user", user);
%>
${user}<br/><br/>
${user.username} | ${user.password}<br/><br/> <%-- 相当于user.getUsername() --%>
${user["username"]} | ${user["password"]}<br/><br/> <%-- 相当于user.getUsername() --%>
<h1>4.访问作用域中的对象-----访问集合对象</h1>
<%
int[] array = new int[]{1,2,3,4,5};
pageContext.setAttribute("array", array);
List list = new ArrayList();
list.add(12);
list.add(true);
list.add("abc");
pageContext.setAttribute("list", list);
Set set = new HashSet();
set.add(12);
set.add(true);
set.add("abc");
pageContext.setAttribute("set", set);
%>
${array}<br/>
${array[0]} | ${array[4]}<br/><br/>
${list}<br/>
${list[2]}<br/><br/>
${set}<br/><br/>
<%-- ${set[2]} --%><%--错误语法,set集合无序,不能使用索引访问。Property '2' not found on type java.util.HashSet --%>
<h1>5.JSP内置对象:pageContext(作用域对象),request,session,application,response,out,config,exception,page(当前对象)</h1>
<h1>5.EL中的隐式对象:pageScope,requestScope,sessionScope,applicationScope,pageContext(当前对象),param,paramValues,header,headerValues,cookie</h1>
${pageContext}<br/><br/> <!-- 相当于jsp内置对象的page -->
${param}<br/> <!-- ?username=tomcat&password=123456 -->
${param.username}<br/><br/> <!-- 相当于request.getParameter("username") -->
${paramValues}<br/> <!-- ?username=tomcat&password=123456&hobby=lol&hobby=dota-->
${paramValues.hobby[1]}<br/><br/> <!-- 相当于request.getParameterValues("hobby") -->
${header}<br/>
${header.host}<br/><br/> <!-- 相当于request.getHeader("host") -->
${headerValues}<br/>
${headerValues.host[0]}<br/><br/>
${cookie}<br/><br/> <!-- 相当于request.getCookies(); -->
${cookie.JSESSIONID.value}<br/><br/>
<h1>6.简单的运算</h1>
<h3>6.1 算数运算</h3>
${5+2*3}<br/>
${10/3}<br/>
${10 div 3}<br/><br/>
${10%3}<br/>
${10 mod 3}<br/>
<h3>6.2 比较运算</h3>
${10 >= 3}<br/>
${10 ge 3}<br/><br/>
${10 != 3}<br/>
${10 ne 3}<br/><br/>
<h3>6.3 逻辑运算</h3>
${true && true}<br/>
${true and true}<br/><br/>
${true || false}<br/>
${true or true}<br/><br/>
${not false}<br/>
${!false}<br/><br/>
<h3>6.4 其他运算符</h3>
${empty param.username}<br/> <!-- 判断表达式是否为null或者"" -->
${ 5>3 ? true : false }
</body>
</html>