1.c:if
1.1代码示例:
<h4>c:if,没有else,但可以把判断的结果储存起来</h4>
<c:set value="20" var="age" scope="request"></c:set>
<c:if test="${requestScope.age>18 }">成年了</c:if>
<br>
<c:if test="${param.age>18 }" var="isAdult" scope="request"></c:if>
isAdult:<c:out value="${requestScope.isAdult }"></c:out>
<br> <br>
1.2运行结果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190423151310677.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hkMTUwMTAxMzAwMjU=,size_16,color_FFFFFF,t_70)
2.c:choose,c:when,c:other
2.1代码示例
<h4>c:choose,c:when,c:otherwise:可以实现if...else if...else的效果,但较为麻烦
c:when,c:otherwise不能脱离c:choose单独使用
c:otherwise必须在c:when之后使用
</h4>
<c:choose>
<c:when test="${param.age>60 }">
old people
</c:when>
<c:when test="${param.age>40 }">
middle people
</c:when>
<c:when test="${param.age>18 }">
youth people
</c:when>
<c:otherwise>
未成年
</c:otherwise>
</c:choose>
<br> <br>
2.2运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190423151523104.png)
3.完整的代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h4>c:if,没有else,但可以把判断的结果储存起来</h4>
<c:set value="20" var="age" scope="request"></c:set>
<c:if test="${requestScope.age>18 }">成年了</c:if>
<br>
<c:if test="${param.age>18 }" var="isAdult" scope="request"></c:if>
isAdult:<c:out value="${requestScope.isAdult }"></c:out>
<br> <br>
<h4>c:choose,c:when,c:otherwise:可以实现if...else if...else的效果,但较为麻烦
c:when,c:otherwise不能脱离c:choose单独使用
c:otherwise必须在c:when之后使用
</h4>
<c:choose>
<c:when test="${param.age>60 }">
old people
</c:when>
<c:when test="${param.age>40 }">
middle people
</c:when>
<c:when test="${param.age>18 }">
youth people
</c:when>
<c:otherwise>
未成年
</c:otherwise>
</c:choose>
<br> <br>
<c:set var="name" value="yyzhang" scope="page"></c:set>
<%--
pageContext.setAttribute("name","yyzhang");
--%>
name: ${pageScope.name}
<br><br>
<c:set var="subject" value="${param.subject }" scope="session"></c:set>
subject: ${sessionScope.subject }
<br><br>
<%
request.setAttribute("book", "<<Java>>");
%>
book:${requestScope.book }
<br><br>
book:<c:out value="${ requestScope.book }"></c:out>
</body>
</html>