EL表达式没有空指针异常、索引越界异常;
EL表达式没有字符串的拼接;
<%@ page import="www.hw.demo.Student" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.HashMap" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>EL表达式</title> </head> <body> <%-- 获取基本类型 --%> <% request.setAttribute("number", 11); %> 基本数据类型:${number}<br> <%--获取自定义对象类型--%> <% Student stu = new Student("张三", 23); pageContext.setAttribute("stu", stu); %> 自定义对象:${stu}<br> 学生姓名:${stu.name}<br> 学生年龄:${stu.age}<br> <%--获取数组类型数据--%> <% int[] arr = {1, 2, 3, 4, 5, 6}; pageContext.setAttribute("arr", arr); %> 数组:${arr}<br> 数组索引0的值:${arr[0]}<br> <%--获取list集合--%> <% ArrayList<String> list = new ArrayList<>(); list.add("aaa"); list.add("bbb"); pageContext.setAttribute("list", list); %> list集合:${list}<br> list集合0索引的值${list[0]}<br> <%--获取map集合--%> <% HashMap<String, Student> map = new HashMap<>(); Student s1 = new Student("zs", 23); Student s2 = new Student("ls", 21); map.put("s1", s1); map.put("s2", s2); pageContext.setAttribute("map", map); %> map集合:${map}<br> 第一个学生对象数据:${map.s1.name}<br> 第二个学生对象数据:${map.s2}<br> </body> </html>