一、實驗內(nèi)容
1、JSTL標(biāo)簽庫的熟練使用
在頁面中接收用戶輸入的字符串,使用JSTL將此字符串反向輸出。不允許使用Java代碼。
2、JSTL標(biāo)簽庫的熟練使用
使用JSTL在頁面中輸出1到100的質(zhì)數(shù)。不允許使用Java代碼。。
二、實驗要求
源代碼和測試截圖(均直接輸入到答題框中)
三、實驗代碼
題目一:
分為兩個jsp,form.jsp用于表單輸入,reout.jsp用于逆序輸出
注意JSTL庫的導(dǎo)入(教程:http://www.runoob.com/jsp/jsp-jstl.html)
//form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>表單輸入</title>
</head>
<body>
<form action="reout.jsp" method="post">
<table border="1" width="50%" align="center">
<tr>
<td>輸入一串字符:</td>
<td><input type="text" name="str"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
//reout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>逆向輸出</title>
</head>
<body>
<!-- 利用EL表達(dá)式param獲取到表單傳入的str字符串 -->
<c:set var="str" value ="${param.str}" />
<!-- 輸出正序的str字符串 -->
<c:out value="${str}"></c:out>
<!--獲取字符串長度 -->
<c:set var="strlen" value="${fn:length(str)}" />
<!-- 獲JSTL的foreach循環(huán),從頭到字符串末尾,以1計數(shù)
fn:substring()函數(shù)返回字符串中指定開始和結(jié)束索引的子串
依次拼接在newstr的前面,最后輸出則為逆序 -->
<c:forEach var="i" begin="0" end="${strlen}" step="1">
<c:set var = "newstr" value="${fn:substring(str,i,i+1)}${newstr}" />
</c:forEach>
<c:out value="${newstr}"></c:out>
</body>
</html>

表單輸入

逆序輸出
題目二:
見注釋
//zhishu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>1到100的質(zhì)數(shù):</p>
<!-- 質(zhì)數(shù)定義為在大于1的自然數(shù)中,除了1和它本身以外不再有其他因數(shù) -->
<c:forEach begin="2" end="100" varStatus="vs">
<!-- 設(shè)置一個exitId用于避免重復(fù)輸出,即有一個數(shù)能整除你,則代表你不是質(zhì)數(shù) -->
<c:set var="exitId" value="1"></c:set>
<!-- 綁定的status封裝了當(dāng)前遍歷的狀態(tài),可以從該對象上查看是遍歷到了第幾個元素,從2開始到自己本身-->
<c:forEach begin="2" end="${vs.index}" varStatus="vs2">
<!-- 如果能被除自己以外的數(shù)整除(非質(zhì)數(shù)),則exitId賦值0 -->
<c:if test="${vs.index%vs2.index==0&&vs.index!=vs2.index}">
<c:set var="exitId" value="0"></c:set>
</c:if>
<!-- 如果只能被自己整除(質(zhì)數(shù))且exitId=1保留到了最后-->
<c:if test="${vs.index%vs2.index==0&&exitId==1 }">
<c:set var="exitId" value="1"></c:set>
${vs.index}
</c:if>
</c:forEach>
</c:forEach>
</body>
</html>

1到100的質(zhì)數(shù)