foreach

1.<%@ page contentType="text/html; charset=GBK" %>    
2.<%@ page import="java.util.*" %>
3.
4.<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
5.<html>
6. <head>
7. <title>JSTL测试1--c:forEach循环</title>
8. </head>
9.
10. <body>
11.一、整数
12. <c:forEach var="i" begin="1" end="10" step="1">
13. <c:out value="${i}" />,
14. </c:forEach><p>
15.二、计算x的平方
16. <table>
17. <tr><th>Value</th>
18. <th>Square</th></tr>
19. <c:forEach var="x" begin="0" end="10" step="2">
20. <tr><td><c:out value="${x}"/></td>
21. <td><c:out value="${x * x}"/></td></tr>
22. </c:forEach>
23. </table> <p>
24.三、字符串"47,52,53,55,46,22,16,2" 分隔.
25. <table border="1">
26. <c:forTokens items="47,52,53,55,46,22,16,2" delims="," var="dailyPrice">
27. <tr><td><c:out value="${dailyPrice}"/></td></tr>
28. </c:forTokens>
29. </table><p>
30.四、使用步长
31. <table>
32. <tr><th>second</th>
33. <th>second</th></tr>
34. <c:forEach var="seconds" begin="0" end="${pageContext.session
35..maxInactiveInterval}" step="60">
36. <tr><td><c:out value="${seconds}"/></td>
37. <td><c:out value="${seconds}"/></td></tr>
38. </c:forEach>
39. </table> <p>
40.五、对数组进行循环<p>
41. <% int ai[] = {10, 20, 30, 40, 50};
42. pageContext.setAttribute("ary", ai);
43. %>
44. <c:forEach var="i" items="${ary}">
45. <c:out value="${i}"/>*
46. </c:forEach><p>
47.
48.<%
49. Cookie c=new Cookie("cookie1","one");
50. response.addCookie(c);
51.%>
52.
53.<%
54. Cookie cookies[]=request.getCookies();
55. Cookie sCookie=null;
56. String sname=null;
57. String name=null;
58. if(cookies==null) // 如果没有任何cookie
59. out.print("none any cookie");
60. else
61. {
62. //out.print(cookies.length + "<br>");
63. for(int i=0;i<cookies.length; i++) // 循环列出所有可用的Cookie
64. {
65. sCookie=cookies[i]; [转自:51item.net]
66. sname=sCookie.getName();
67. name = sCookie.getValue();
68. out.println(sname + "->" + name + "<br>");
69. }
70. }
71.%>
72.
73.<table border="1" align="center">
74. <tr><th>Cookie Name</th>
75. <th>Cookie Value</th></tr>
76. <c:forEach var="cook" items="${pageContext.request.cookies}">
77. <tr><td><c:out value="${cook.name}"/></td>
78. <td><c:out value="${cook.value}"/></td></tr>
79. </c:forEach>
80. </table> <p>
81.
82.六、字符串数组循环
83.<% String as[] = {
84. "A first string", "La deuxieme string", "Ella troisiemo stringo"
85. };
86. request.setAttribute("stringArray", as);
87.%><p>
88.<c:forEach var="string" items="${stringArray}">
89. <c:out value="${string}"/><br>
90.</c:forEach>
91.七、枚举
92.<%
93.Hashtable hashtable1 = new Hashtable();
94. pageContext.setAttribute("numberMap", hashtable1);
95.
96. hashtable1.put(new Integer(1), "uno");
97. hashtable1.put(new Integer(2), "dos");
98. hashtable1.put(new Integer(3), "tres");
99. hashtable1.put(new Integer(4), "cuatro");
100. hashtable1.put(new Integer(5), "cinco");
101. hashtable1.put(new Integer(6), "seis");
102. hashtable1.put(new Integer(7), "siete");
103. hashtable1.put(new Integer(8), "ocho");
104. hashtable1.put(new Integer(9), "nueve");
105. hashtable1.put(new Integer(10), "diez");
106.
107. java.util.Enumeration enumeration = hashtable1.keys();
108. pageContext.setAttribute("enumeration", enumeration);
109.%>
110.
111. <c:forEach var="item" items="${enumeration}" begin="2" end="10" step="2">
112. <c:out value="${item}"/><br>
113. </c:forEach><p>
114.八、map<p>
115. <c:forEach var="prop" items="${numberMap}" begin="1" end="5">
116. <c:out value="${prop.key}"/> = <c:out value="${prop.value}"/><br>
117. </c:forEach>
118.
119.
120.
121.</body>
122.</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值