<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--导入jar包--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--进行判断--%>
<c:if test="${xingming !=null}">
${xingming}
</c:if>
<c:if test="${empty xingming}">
<a href="index.jsp">未登录请登陆</a>
</c:if>
</body>
</html>
<%@ 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>
<%
int arr[]={1,2,3,4,5,6};
pageContext.setAttribute("arr",arr);
%>
<%--begin起始的索引值,end终止的索引值,step步长,var为当前遍历的索引值--%>
<c:forEach begin="0" end="5" step="1" var="index">
${arr[index]}
</c:forEach>
</body>
</html>
<%@ 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>
<%
int arr[]={1,2,3,4,5,6};
pageContext.setAttribute("arr",arr);
%>
<%--items是当前容器,var是当前的对象,varstatus是当前对象的属性(索引值等)--%>
<c:forEach items="${arr}" var="ele" varStatus="v">
${ele}
${v.index}
</c:forEach>
</body>
</html>
<%@ 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>
<%
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
pageContext.setAttribute("list",list);
%>
<c:forEach items="${list}" var="ele">
${ele}
</c:forEach>
<body>
</body>
</html>
<%@ 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>
<%
User user1 = new User("zhangsan", 20);
User user2 = new User("lisi", 21);
User user3 = new User("wangwu", 22);
ArrayList<User> list = new ArrayList<>();
list.add(user1);
list.add(user2);
list.add(user3);
pageContext.setAttribute("list",list);
%>
<c:forEach items="${list}" var="ele">
${ele.name}---${ele.age}<br>
</c:forEach>
<body>
</body>
</html>
实例1
<%@ 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>
<%
HashMap<Integer, String> map = new HashMap<>();
map.put(1,"zhangsan");
map.put(2,"lisi");
map.put(3,"wangwu");
pageContext.setAttribute("map",map);
%>
<%--ele为集合中的键值对--%>
<c:forEach items="${map}" var="ele">
${ele.key}---${ele.value}<br>
</c:forEach>
<body>
</body>
</html>
<%@ 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>
<c:redirect url="/demo"></c:redirect>
<body>
</body>
</html>
<%@ 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>
<%--scope设置什么域,var定义键,定义值--%>
<c:set scope="application" var="username" value="username"></c:set>
<body>
${username}
</body>
</html>
<%@ 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>
<c:set scope="application" var="name" value="username1"></c:set>
<%--default 设置默认值--%>
<c:out value="${name}" default="lisi"></c:out>
<%--escapeXml设置是否转译为html标签--%>
<c:out value="你好
" escapeXml="ture"></c:out>
<body>
</body>
</html>
<%@ 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>
<%--设置域中的值为60--%>
<c:set var="value" value="80" scope="page"></c:set>
<c:choose>
<c:when test="${value>=60&&value<80}">
及格
</c:when>
<c:when test="${value>=80&&value<100}">
优秀
</c:when>
<c:otherwise>
不及格
</c:otherwise>
</c:choose>
<body>
</body>
</html>