JSP本质:JSP就是方便书写html代码的Servlet,是一种特殊的Servlet.
jsp有七大组成部分:
1. 静态内容:前端内容
2. 指令:<%@指令名 属性名=”值”%>
3. 表达式:<%=表达式%>
4. 代码片段: <%java代码%>
5. 声明:<%! 类成员 %>
6. 注释:<%-- jsp注释内容 --%>
7. 动作:<jsp:动作名></jsp:动作名>
jsp指令
page指令
格式: <%@page language="java"info="xxx"...%> 可以放在任意位置,但是一般放在最上上面;
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
include指令
<body>
<h1>这是include1页面</h1>
<%--静态包含include2.jsp页面--%>
<%@include file="include2.jsp" %>
</body>
taglib指令
taglib指令:是用来在当前jsp页面中导入第三方的标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
JSP内置对象
jsp中一共有9大内置对象。大家只需要记住最重要的PageContext即可。因为可以通过PageContext获取其他八个内置对象。
PageContext对象
PageContext域功能
JSP页面有四大域对象。我们学过ServletContext和Request两个域对象。还有两个是PageContext和Session。
pageContext域对象,它的范围是当前页面。它的范围也是四个域对象中最小的
<%
//存储数据
pageContext.setAttribute("name", "詹姆斯•高斯林");
//获取
Object name = pageContext.getAttribute("name");
//移除name
pageContext.removeAttribute("name");
//获取
Object name1 = pageContext.getAttribute("name");
%>
<%=name %>
<%=name1 %>
EL表达式
EL:Expression Language,它是可以在JSP页面中直接使用的语言。
EL的作用:它可以从jsp的域对象中获取数据,简化java脚本,还可以执行一些简单的计算。
EL的格式:${表达式}
例子:
@WebServlet("/demo3")
public class Demo3Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//存简单数据类型
//1.存字符串
request.setAttribute("name","zhangsan");
request.setAttribute("age",18);
request.setAttribute("score",18.5);
//2.存数组
String[] arr = {"rose","jack","tom","jerry"};
request.setAttribute("arr",arr);
//3.存对象(javaBean)
User user = new User("lisi","1234");
request.setAttribute("user",user);
//4.list集合
List<User> userList = new ArrayList<>();
User user1 = new User("wangwu","12345");
User user2 = new User("zhaoliu","123456");
userList.add(user);
userList.add(user1);
userList.add(user2);
request.setAttribute("userList",userList);
//5.map集合
Map<String,String> map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
request.setAttribute("map",map);
//请求转发
request.getRequestDispatcher("demo5.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>EL表达式从域对象取值</title>
</head>
<body>
<h3>取基本数据类型</h3>
${name}<br>
${age}<br>
${score}<br>
<h3>取数组数据类型</h3>
${arr} ------ ${arr[1]}
<h3>取对象数据类型</h3>
${user.name} ---- ${user.password}
<h3>取list集合数据类型</h3>
<%-- 获取对象属性值,其实是调用getXxx() 对象.去掉get方法后中get单词首字母小写--%>
${userList} --- ${userList[1].name} --- ${userList[1].password}
<h3>取map集合数据类型</h3>
${map} --- ${map.key1} --- ${map["key2"]}
</body>
</html>
JSTL标签库
JSTL标签core库
JSTL是Jsp 标准标签库(Jsp Standard Tag Library),是由Apache组织提供的开源的jsp标签库
jstl其实本质就是标签, 含一定逻辑的标签
作用:主要使用他们在页面做判断和循环操作
JSTL标准标签库有5个子库,但随着发展,目前常使用的是它的核心库 core。
使用步骤
导入jstl相关jar包
在使用的页面中通过<%@taglib prefix="标签前缀" uri="标签库的uri"%>导入标签库
JSTL核心标签库也提供了很多标签,但是核心操作是使用他们在页面做判断和循环操作。所以,今天我们重点学习JSTL的if判断和for循环操作。JSTL标签往往是与EL表达式结合使用的。
if和choose标签
例子:
<head>
<title>if判断</title>
</head>
<body>
<%-- jstl标签库实现if判断功能
必须设置test属性 test属性满足条件后,进入c:if判断成立的条件中
test属性支持el表达式
只有 <c:if> 没有<c:else>
--%>
<c:if test="${2>1}">
大于
</c:if>
<c:if test="${2<1}">
小于
</c:if>
</body>
</html>
choose:choose标签对应Java中的switch case结构。when标签的test为true时,会执行这个when的内容。当所有when标签的test都为false时,才会执行otherwise标签的内容
例子:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>choose标签</title>
</head>
<body>
<%
//定义score变量
int score = 85;
//存入request域对象
request.setAttribute("score",score);
%>
<c:choose>
<c:when test="${score > 100 || score < 0}">错误的分数:${score }</c:when>
<c:when test="${score >= 90 }">A级</c:when>
<c:when test="${score >= 80 }">B级</c:when>
<c:when test="${score >= 70 }">C级</c:when>
<c:when test="${score >= 60 }">D级</c:when>
<c:otherwise>E级</c:otherwise>
</c:choose>
</body>
</html>
forEach标签
<%@ page import="cn.jiyun.pojo.User" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>foreach遍历操作</title>
</head>
<body>
<h3>普通for循环</h3>
<%--
普通for循环
for(int i=0;i<10;i++){
}
begin:开始遍历起始数据
end:遍历的结束数据
step:步长 每次遍历时,遍历值加1
--%>
<c:forEach begin="0" end="9" step="2" var="i">
${i}
</c:forEach>
<hr>
<%
//数据准备
//2.数组
String[] arr = {"rose","jack","tom","jerry"};
request.setAttribute("arr",arr);
//4.list集合
List<User> userList = new ArrayList<>();
User user1 = new User("wangwu","12345");
User user2 = new User("zhaoliu","123456");
userList.add(user1);
userList.add(user2);
request.setAttribute("userList",userList);
//5.map集合
Map<String,String> map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
request.setAttribute("map",map);
%>
<h3>增强for循环</h3>
<%--
c:forEach:
属性:items 遍历的数据 支持el表达式
var:声明一个变量,接收遍历的数据
${str}通过el表达式获取遍历的数据
--%>
<h3>遍历数组</h3>
<c:forEach items="${arr}" var="str">
${str}
</c:forEach><br>
<h3>遍历list集合</h3>
<c:forEach items="${userList}" var="user">
${user} ---- ${user.name} ----${user.password}<br>
</c:forEach>
<h3>遍历map集合</h3>
<%--遍历map集合 keyset entryset entry.getKey() entry.getValue()--%>
<c:forEach items="${map}" var="entry">
${entry.key} ==== ${entry.value} <br>
</c:forEach>
</body>
</html>
forEach标签循环状态变量
forEach标签还有一个属性:varStatus,这个属性用来指定接收“循环状态”的变量名,例如:<forEach varStatus=”vs” …/>,这时就可以使用vs这个变量来获取循环的状态了。
count:int类型,当前已遍历元素的个数;
index:int类型,当前元素的下标;
first:boolean类型,是否为第一个元素;
last:boolean类型,是否为最后一个元素;
current:Object类型,表示当前项。
例子:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>foreach循环状态属性操作</title>
</head>
<body>
<%
//定义数组,准备遍历的数据
String[] names = {"zhangSan", "liSi", "wangWu", "zhaoLiu"};
pageContext.setAttribute("ns", names);
%>
<c:forEach var="item" items="${ns}" varStatus="vs">
${vs.count} --- ${vs.index} --- ${vs.current} <br/>
</c:forEach>
</body>
</html>