Sei sulla pagina 1di 14

JSTL Tags

1. Iteration
2. Decision making with scriplets
3. Decision making with JSTL tags

1
Sample of Iterations

2
1. Sample of Iteration
1. <%@page contentType="text/html"%>
2. <%@page pageEncoding="UTF-8"%>

3. <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>

4. <%-- Declaring an array of cars --%>


5. <% String[] cars = { "Ferrari", "Porsche", "Toyota" }; %>

6. <%-- Assigns values to the variables in scope --%>


7. <c:set var="simple" value="${2+1}" scope="page" />
8. <c:set var="bike" value="Kawasaki:Honda;Ducati|BMW" scope="page" />

9. <%-- Creating and setting the object referred to by the variables


10. into a context accessible from the page --%>
11.
12. <% pageContext.setAttribute("Example", "CoreTag", PageContext.PAGE_SCOPE); %>
13. <HTML>
14. <HEAD>
15. <TITLE>Using Core Tag Library</TITLE>
16. </HEAD>

3
2. Sample of Iteration [cont]
17. <BODY>
18. <%--Print "There are 3 cars. The Names are:" --%>
19. There are <c:out value="${pageScope.simple}"/> cars. The Names are: <br/>
20.
21. <%-- Shows the names of the cars --%>
22. <UL>
23. <c:forEach var="i" items="<%= cars %>">
24. <LI><c:out value="${i}" />
25. </c:forEach>
26.
27. </UL>
28. The Names of the Bikes are: <br/>
29. <%-- Iterate over a collection of tokens separated by user-specified delimiters--%>
30. <c:forTokens items="${pageScope.bike}" delims=":;|" var="bikenames">
31. <UL>
32. <LI> <c:out value="${bikenames}" /> <br/>
33. </UL>
34. </c:forTokens>

4
3. Sample of Iteration [cont]
35. <%-- Removes the variable and the scope --%>
36. <c:remove var="simple" scope="page"/>
37.
38. <%-- Checking of the condition --%>
39. <c:if test="${pageScope.Example == param.example}">
40. <c:set var="Success" value="true" scope="page" />
41. </c:if>
42.
43. <%-- Gives the output if the condition is true --%>
44. <c:choose>
45. <c:when test="${pageScope.Success == true}">
46. Successfully Shown the Names
47. </c:when>
48. <c:otherwise>
49. Unsuccessful To Show the Names
50. </c:otherwise>
51. </c:choose>
52. </BODY>
53. </HTML>

5
Add the JSTL library to the project

 Right click on the project, select Property

6
Add the JSTL library to the project

 Select the library. Then click on Add Library


button

 Click on OK button to go back the main screen of


NetBean IDE

7
JSTL Tags

1. Iteration
2. Decision making with scriptlets
3. Decision making with JSTL tags

8
2. Sample of decision.jsp (1/3)

1. <%@page contentType="text/html"%>
2. <%@page pageEncoding="UTF-8"%>
3. <%@page import="java.util.StringTokenizer" %>
4. <html>
5. <head>
6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7. <title>Word Counter</title>
8. </head>
9. <body>
10. <%! String paraValue; %>
11. <%
12. int nWords = 0;
13. paraValue = (String) request.getParameter("para");
14. %>
15. <h1>Word Counter</h1>
16. <form action="decision.jsp">
17. Nhập đoạn văn bản yêu thích:
18. <br/>
9
2. Sample of decision.jsp (2/3)
19. <textarea name="para" rows="4" cols="20"><%= paraValue %></textarea>
20. <br/>
21. <input type="submit" value="Count" name="Count" />
22. <br/>
23. <%
24.
25. if (paraValue != null) {
26. StringTokenizer tokens = new StringTokenizer(paraValue, " ,.;'");
27. while (tokens.hasMoreTokens()) {
28. nWords++;
29. tokens.nextToken();
30. }
31. }
32. %>

10
2. Sample of decision.jsp (3/3)
33. <%
34. if (paraValue != null) {
35.
36. %>
37. Số từ của văn bản: <%= nWords %>
38. <% if (nWords <= 10) { %>
39. Văn bản rất ngắn!
40. <% } else { %>
41. Văn bản rất dài!
42. <% } %>
43. <% } %>
44. </form>
45. </body>
46. </html>

11
JSTL Tags

1. Iteration
2. Decision making with scriplets
3. Decision making with JSTL tags

12
Sample code of decision-tag.jsp (1/2)
1. <%@page contentType="text/html"%>
2. <%@page pageEncoding="UTF-8"%>
3. <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
4. <c:set var="nWords" value="0" scope="page"/>
5. <html>
6. <head>
7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8. <title>Word Counter</title>
9. </head>
10. <body>
11. <h1>Word Counter</h1>
12. <form action="decision-tag.jsp">
13. Nhập đoạn văn bản yêu thích:
14. <br/>
15. <textarea name="para" rows="4" cols="20">${param.para}</textarea>
16. <br/>
17. <input type="submit" value="Count" name="Count" />
18. <br/>

13
Sample code of decision-tag.jsp (2/2)
19. <c:if test="${not empty param.para}">
20. <c:forTokens items="${param.para}" delims=" ,.;'" var="token">
21. <c:set var="nWords" value="${pageScope.nWords + 1}"
scope="page"/>
22. </c:forTokens>
23. </c:if>
24. <c:if test="${not empty param.para}">
25. Số từ của văn bản: ${pageScope.nWords}
26. <br/>
27. <c:choose>
28. <c:when test="${pageScope.nWords <= 10}">
29. Văn bản rất ngắn!
30. </c:when>
31. <c:otherwise>
32. Văn bản rất dài!
33. </c:otherwise>
34. </c:choose>
35. </c:if>
36. </form>
37. </body>
38. </html>

14

Potrebbero piacerti anche