1EL表达式
2JSTL标签库
3请求转发与重定向的区别
一EL表达式:替换<%= %>
1EL语法
%{表达式}
表达式 : 可以为运算的式子 10+5
也以为某个作用域的值
page --->${pageScope.key}
注意:作用域不写,如果不写,从小作用域到大作用域
简写形式${key}
demo1-->TwoServlet-->demo2
拿到demo2的request对象的值
可以简写为${param.name}
注意 ‘’+‘’ 不能用于字符串的拼接操作
2EL表达式的运算符
1) 算术运算符 :
+ - * / %
2) 比较运算符
== > < >= <=
? :
3) 逻辑运算符
and、&&、or、||、!、not
4) empty ,not empty
empty :判断是否为空,是true,否false
什么是 空 ?: null , "" ,相当于集合中isEmpty
3EL表达式如何访问对象的属性
${key.属性名}
属性名-->实体类中的getXXX()
EL表达式的隐式对象(11个)
pageScope,requestScope,sessionScope,applicationScope
param.name -->request.getParameter("name")
paramValues.name -->request.getParameterValues("name")
pageContext -->它可以用于访问 JSP 隐式对象
header :获取请求头的信息
headerValues :
cookie :获取cookie对象
initParam
二 JSTL标签库:替换java脚本 <% %>
类似于动态sql语句
jstl标签库写forEach循环
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %><%--
Created by IntelliJ IDEA.
User: rk
Date: 2024/4/11
Time: 13:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:if test=""></c:if>
<%
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
request.setAttribute("list",list);
Integer i = 0;
%>
<c:forEach items="${list}" var="num" >
<tr>
<td>${num}</td>
</tr>
</c:forEach>
<c:forEach begin="1" end="100" step="1" var="num">
<c:if test="${num%2!=0}">
${num}
<%-- ${i=i+1}--%>
<%
i=i+1;
%>
</c:if>
<c:if test="${i%10==0}">
<br>
</c:if>
</c:forEach>
</body>
</html>
jstl标签库写if判断 choose+when判断
<%--
Created by IntelliJ IDEA.
User: rk
Date: 2024/4/11
Time: 11:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
int score = 499;
request.setAttribute("score",score);
%>
<c:if test="${score>=90}">优</c:if>
<c:if test="${score>=80}">良</c:if>
<c:if test="${score>70}">中</c:if>
<c:if test="${score<=70}">差</c:if>
<c:choose>
<c:when test="${score >= 90}">
<p>优${score}</p>
</c:when>
<c:when test="${score >= 80}">
<p>良</p>
</c:when>
<c:when test="${score >= 70}">
<p>中</p>
</c:when>
<c:when test="${score < 70}">
<p>差</p>
</c:when>
</c:choose>
</body>
</html>
jstl标签库替换日期
<%@ page import="java.util.Date" %><%--
Created by IntelliJ IDEA.
User: rk
Date: 2024/4/11
Time: 14:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
Date date = new Date();
request.setAttribute("date",date);
%>
<fmt:formatDate value="${date}" pattern="yyyy-MM-dd HH" />
<fmt:formatDate value="${date}" type="date"/>
<fmt:formatDate value="${date}" type="time"/>
<fmt:formatDate value="${date}" type="both"/>
</body>
</html>
三 重定向与转发
one.jsp -->controller -->two.jsp
1 转发
request.getRequestDispatcher("two.jsp").forward(request,response);
路径不发生改变
转发后,request中的数据依然可以获取到
在服务器完成 ,一次请求
2 重定向
response.sendRedirect("two.jsp");
路径发生改变
重定向后,,request中的数据不能获取
在浏览器完成 ,两次请求
3 请求转发与重定向的区别
1) 语法
request.getRequestDispatcher("two.jsp").forward(request,response);
response.sendRedirect("two.jsp");
2) 请求的次数不同
转发,一次请求,因此request中数据可以共享
重定向是两次请求,因此两个request对象,数据不能共享
3) 发生在的位置不同
转发:在服务器完成 , 浏览器地址不发生改变
重定向在浏览器完成 ,浏览器地址发生改变
4 服务器路径,浏览器路径
http://localhost:8080/JSP_pro/two.jsp
路径 : "/路径"
1) 服务器路径 : 请求转发
http://localhost:8080/JSP_pro"/two.jsp"
2) 浏览器路径 (重定向,超链接 ,表单提交,直接写url)
http://localhost:8080"/JSP_pro/two.jsp"
"/项目名称/资源路径"
获取项目路径 : Controller : request.getContextPath() -->/项目名称
EL表达式: ${pageContext.request.contextPath}