1. 程式人生 > >通過指令碼和EL表示式獲取域中資料

通過指令碼和EL表示式獲取域中資料

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" %> <html> <head> <title>Title</title> </head> <body> <% //request域中,存了一個小明 request.setAttribute("name","小明"); //session域中存了一個小紅 User user = new User(); user.setName("小紅"); user.setSex("女"); session.setAttribute("user"
,user); //application域中存了一個list集合 List <User> 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<User> list1 = (List<User>) 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} </body> </html>

這就是el表示式在jsp中最常用的功能