E L(Expression
Language) 目的:为了使JSP写起来更加简单。它提供了在 JSP 中简化表达式的方法
pageScope
requestScope sessionScope applicationScope的权限问题。
1、Servlet的代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//设置单个值
request.setAttribute("uid", "MolStore");
//设置对象
Stu stu = new Stu();
stu.setName("wgr");
stu.setAge(30);
request.setAttribute("student", stu);
HttpSession session = request.getSession();
session.setAttribute("sex", "man");
//设置List<String>
List<String> list = new ArrayList<String>();
list.add("One");
list.add("Two");
list.add("Three");
session.setAttribute("list", list);
//设置List<Object>
// List<Stu> stuList = new ArrayList<Stu>();
// stuList.add(new Stu("aa", 11));
// stuList.add(new Stu("bb", 12));
// request.setAttribute("stuList", stuList);
//设置Set<Object>
Set<Stu> stuList = new HashSet<Stu>();
stuList.add(new Stu("aa", 11));
stuList.add(new Stu("bb", 12));
request.setAttribute("stuList", stuList);
//设置Map<Object, Object>;
Map<String, Stu> map = new HashMap<String, Stu>();
map.put("one", new Stu("aa", 11));
map.put("two", new Stu("bb", 12));
request.setAttribute("mapStu", map);
request.getRequestDispatcher("user.jsp").forward(request,response);
}
2、JSP代码:
使用EL表达式,要导入标签库<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %><body>
<!-- pageScope requestScope sessionScope applicationScope -->
//获取单个属性
${requestScope.uid} ----<br /> //EL表达式取值。
//获取对象中设置的
${requestScope.student.name} ----- <br /> //EL表达式取值。
${sessionScope.sex } ----- <br />
//遍历List与Set方式都一样。
<c:forEach var="li" items="${list}">
${li} <br />
</c:forEach>
//遍历List或Set集合中对象
<c:if test="${stuList != null }"> //判断空
<c:forEach var="stu" items="${stuList}">
${stu.name} ------ ${stu.age} <br />
</c:forEach>
</c:if>
//遍历Map集合中的对象
<c:if test="${mapStu != null }"> //判断空
<c:forEach var="stu" items="${mapStu}">
${stu.key} ------ ${stu.value.name} <br />
</c:forEach>
</c:if>
</body>