通过脚本和EL表达式获取域中数据

<%@ page import="net.wen.bean.User" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: wen
  Date: 2018/8/21
  Time: 23:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


<%
    //request域中,存了一个小明
    request.setAttribute("name","小明");

    //session域中存了一个小红
    User user = new User();
    user.setName("小红");
    user.setSex("女");
    session.setAttribute("user",user);

    //application域中存了一个list集合
    List  list = new ArrayList<>();
    User user1 = new User();
    user1.setName("小绿");
    user1.setSex("女");
    User user2 = new User();
    user2.setName("小黑");
    user2.setSex("男");
    list.add(user1);
    list.add(user2);
    application.setAttribute("list",list);
%>
<%--通过脚本获取request中name的值--%>
<%=request.getAttribute("name")%>
<%--通过脚本获取session中user中User对象的值--%>
<%
    User user3 = (User) session.getAttribute("user");
    String name = user3.getName();
    String sex = user3.getSex();
    out.write(name+","+sex);
%>
<%--通过脚本获取application中list的list对象中的值--%>
<%
    List list1 = (List) application.getAttribute("list");
    User user4 = list1.get(0);
    out.write(user4.getName()+","+user4.getSex());
%>
<%--通过el表达式获取request、session、application中的多对应的值--%>
${requestScope.name}
${sessionScope.user.name}
${applicationScope.list[0].name}
<%--更简便的写法--%>
${name}
${user.name}
${list[0].name}

这就是el表达式在jsp中最常用的功能

你可能感兴趣的:(JavaWeb)