一.首先来了解jsp的四个域对象
application 应用级储存:属性的作用范围仅限于当前web应用(范围最大)
session 会话:属性的作用范围仅限于一次会话,浏览器打开直到关闭称为一次会话(在此期间会话不失效)
request 转发:属性的作用范围仅限同一个请求(转发有效)
pageContext: 属性的作用范围仅限当前的jsp页面(范围最小)
大小顺序排列:application>session>request>page
EL(Expression Language)表达式:
功能:替代jsp页面中的复杂代码
语法:${EL expression}
${bean.name}
EL中的隐含对象(pageScope、requestScope、sessionScope、applicationScope)
EL运算符(算术 关系 Empty运算符【判断集合中值是否为空 返回true或者false】)
二.代码展示
1.通过set将值存入这些对象中
<jsp:forward page="text.jsp"/>标准标签,跳转
<%
application.setAttribute("mgs","你好");
session.setAttribute("mgs","hello");
request.setAttribute("mgs", "hello world");
%>
<jsp:forward page="text.jsp"/>
2.跳转后页面的操作
pageContext.setAttribute("mgs", "hello 你好");只有在本界面能调用
${user.name.equals("root")?"yes":"no"}三元运算
${us.size()==0}判断集合有无数据
${empty us}有数据
${not empty us}没有数据
<%
pageContext.setAttribute("mgs", "hello 你好");
int sum=100;
pageContext.setAttribute("sum", sum);
User u=new User("root","123");
pageContext.setAttribute("user",u);
List<User> list=new ArrayList<>();
//将集合存入page
pageContext.setAttribute("us",list);
%>
${applicationScope.mgs}
<br>
${sessionScope.mgs}
<br>
${requestScope.mgs}
<br>
${mgs}
<br>
${sum}
<br>
${user}
<br>
${user.name}
<br>
${user.pwd}
<br>
${user.name.equals("root")?"yes":"no"}
<br>
${us.size()==0}--${empty us}---${not empty us}
3.JSTL
通用标签:set out remove
条件标签:if
迭代标签:forEach
需要导入两个jar包
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>导入标签
<c:if test="${yy==null}">if判断
<c:forEach items="${list}" var="user">forEach遍历
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String yy=request.getParameter("yy");
pageContext.setAttribute("yy", yy);
List<User> list=new ArrayList<>();
for(int i=0;i<5;i++){
User u=new User();
u.setName("好好"+i);
u.setPwd("123"+i);
list.add(u);
}
pageContext.setAttribute("list", list);
%>
<c:set scope="page" value="1" var="a"></c:set>
<c:out value="${a}"></c:out>
<c:remove var="a" scope="page"></c:remove>
<c:if test="${yy!=null}">
<h1>hhhh</h1>
</c:if>
<c:if test="${yy==null}">
<button>请登录</button>
</c:if>
<!-- 商品遍历 -->
<table border>
<tr>
<th>商品名字</th>
<th>商品价格</th>
</tr>
<c:forEach items="${list}" var="user">
<tr>
<th>${user.name}</th>
<th>${user.pwd}</th>
</tr>
</c:forEach>
</table>