先下载JSTL,然后将jstl.jar, standard.jar两个jar包copy到lib下, 将要使用的Core标签中的c.tld文件copy到WEB-INF下,修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<jsp-config>
<taglib>
<taglib-uri>jstl-c</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
</jsp-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
定义一个taglib.jsp用于引入标签
<%@ taglib prefix="c" uri="jstl-c"%>
先看out标签的使用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@include file="taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>c:out的使用</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h2 align="center">c:out 的作用就相当于<%="param1" %></h2><br>
<h3>输出value的值要是value为空就输出default的值,escapeXml属性的值是转义用的</h3>
<br><br>
<c:out value="学习JSTL"></c:out><br>
<c:out value="${sessionScope.username}" default="libinxuan"></c:out><br>
<c:out value="${username}" default="libinxuan"></c:out><br>
<c:out value="<h1>学习JSTL</h1>" escapeXml="false"></c:out><br>
<c:out value="<h1>学习JSTL</h1>" escapeXml="true"></c:out><br>
</body>
</html>
set的使用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="jstl-c" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>c:set的用法</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h2 align="center">作用是将变量保存到JavaBeans属性中或JSP页面的特定取值范围</h2><br><br>
<h2>各属性的含义:</h2><br>
<h3> value: 要保存的内容,可以是EL表达式或常量</h3><br>
<h3> target: 要修改属性的对象名,一般为JavaBeans对象名</h3><br>
<h3> property: 要修改的JavaBeans的属性</h3><br>
<h3> var: 要保存内容的变量的名称</h3><br>
<h3> scope: 保存内容的作用范围</h3><br><br><br>
<h2>注意: 指定了target属性就要同时指定property属性</h2><br><br>
<c:set value="libinxuan" var="username" scope="session"></c:set><br>
<c:set value="libinxuan" var="user" scope="session"></c:set><br>
<c:set var="username2" scope="session">libinxuan</c:set><br><br>
<h2>将value的值保存到target对象的propertyName属性中</h2><br>
<c:out value="${sessionScope.user}" default="sss"></c:out>
</body>
</html>
remove的使用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="jstl-c" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>c:remove的使用</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h2 align="center">作用是删除所设定的变量</h2><br><br>
<h3>删除session中名为username的属性的值</h3><br>
<c:remove var="username" scope="session"/>
</body>
</html>
if的使用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>c:if的使用</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<c:set value="libinxuan" var="name" scope="session"></c:set>
<c:out value="${sessionScope.name}"></c:out><br>
<c:if test="${sessionScope.name == 'libinxuan'}">
欢迎管理员登录
</c:if><br>
<c:if test="${1>0}">
你好
</c:if>
</body>
</html>
choose的使用,这里要借助一个jsp的请求input.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用于测试choose,when,otherwise</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="choose.jsp">
请输入你的年龄:<input type="text" name="age" id="age" >
<input type="submit" value="提交" >
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="jstl-c" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>c:choose,c:when,s:otherwise的使用</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<c:choose>
<c:when test="${param.age>70}">
老人
</c:when>
<c:when test="${param.age<=70 and param.age>=35 }">
中年人
</c:when>
<c:when test="${param.age>=0 and param.age<=35 }">
年轻人
</c:when>
<c:otherwise>
你不是人
</c:otherwise>
</c:choose>
</body>
</html>
forEach的使用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="taglib.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>c:forEach标签的使用</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<%
String names[] = new String[4];
names[0]="张三";
names[1]="李四";
names[2]="王五";
names[3]="张六";
pageContext.setAttribute("names",names);
%>
</head>
<body>
<h2 align="center">用于迭代字符串,数组,集合</h2>
<h2>各属性的介绍:</h2><br/>
<h3> begin: 开始的索引</h3><br>
<h3> end: 介绍的索引</h3><br>
<h3> step: 步长</h3><br>
<h3> var: 当前迭代变量的名称</h3><br>
<h3> items: 进行循环的集合</h3><br>
<h3> varStatus: 显示循环状态的变量</h3><br>
<c:forEach items="${names}" var="name" varStatus="i">
${name}<br>
${i.index}<br>
</c:forEach>
</body>
</html>