1.本周思维导图
2.个人总结
操作域
- page域
<%
//往pageContext域中存储了一个msg变量
pageContext.setAttribute("msg" ,"hello page msg");
%>
<%
//往pageContext域中存储了一个msg变量
Object msg = pageContext.getAttribute("msg");
System.out.println(msg);
%>
- request域
<%
//定义变量的意义! 提高复用性! 提高可维护性!
//String name : 参数名称
//Object value : 参数值
//int scope : 操作的域
pageContext.setAttribute("msg1","hello page1",PageContext.REQUEST_SCOPE);
//请求转发
request.getRequestDispatcher("/demo06.jsp").forward(request,response);
%>
<%
//变量msg1定义到_jspService方法中
Object msg1 = request.getAttribute("msg1");
System.out.println("msg1 : "+msg1);
Object msg11 = pageContext.getAttribute("msg1", PageContext.REQUEST_SCOPE);
System.out.println(msg11);
%>
- session域
<%
pageContext.setAttribute("msg2","hello page2",PageContext.SESSION_SCOPE);
%>
- application域
<%
pageContext.setAttribute("msg3","hello page3",PageContext.APPLICATION_SCOPE);
%>
el获取域数据
- 获取page域数据
<%
pageContext.setAttribute("msg1","hello page1");
%>
${pageScope.msg1}
- 获取request域数据
<%
request.setAttribute("msg1","hello page1");
%>
${requestScope.msg1}
- 获取session域数据
<%
session.setAttribute("msg1","hello page1");
%>
${sessionScope.msg1}
- 获取application域数据
<%
application.setAttribute("msg1","hello page1");
%>
${applicationScope.msg1}
el获取复杂域数据
- 获取数组
<%
//数组静态初始化
String[] msgs = {"获取","复杂","域"};
pageContext.setAttribute("msgs",msgs);
%>
<%--jsp输出脚本--%>
<%=
((String[])pageContext.getAttribute("msgs"))[0]
%>
${msgs[1]}
- 获取List集合
<%
List<String> msgs1 = new ArrayList<>();
msgs1.add("复杂的");
msgs1.add("域数据");
request.setAttribute("msgs1",msgs1);
%>
<%=
((List<String>)request.getAttribute("msgs1")).get(0)
%>
${msgs1[1]}
- 获取map集合
<%
HashMap<String,Object> map = new HashMap<>();
map.put("username","白鸽");
map.put("age",18);
session.setAttribute("map",map);
%>
<%=
((HashMap<String,Object>)session.getAttribute("map")).get("username")
%>
${map.age}
- 获取java对象
<%
User user = new User();
user.setId(1);
user.setUsername("黑鸦");
user.setPassword("123456");
application.setAttribute("user",user);
%>
<%=
((User)application.getAttribute("user")).getUsername()
%>
${user.password}
jstl标签库
- 使用taglib指令导入
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
int num = 1;
request.setAttribute("num" , num);
%>
<c:if test="${num eq 1}">
num等于1
</c:if>
<c:if test="${num ne 1}">
num不等于1
</c:if>
- set标签
- 向域对象(page域、request域、session域、application域)中存储数据
<c:set var="msg" scope="request" value="hello jstl"></c:set>
${msg}
- remove标签
- 移除域对象中的数据
<c:remove var="msg" scope="request"></c:remove>
${msg}<br>
- catch标签
- 捕获jsp页面的异常,相当于try...catch,var:声明异常对象名称,比如:var="e" ,变量e就可以接收异常对象
<c:catch var="e">
<%
int num = 1 / 0;
%>
</c:catch>
${e}<br>
- if标签
- 条件判断
<c:set var="num" value="2" scope="request"></c:set>
<c:if test="${num == 1}">
num 等于 1
</c:if>
<c:if test="${num != 1}