Skip to content

Commit ee43836

Browse files
JSTL Demo
1 parent bc4df0e commit ee43836

File tree

4 files changed

+330
-1
lines changed

4 files changed

+330
-1
lines changed

‎online-store.web/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
<scope>provided</scope>
2222
</dependency>
2323

24+
<dependency>
25+
<groupId>org.glassfish.web</groupId>
26+
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
27+
<version>2.0.0</version>
28+
</dependency>
2429

2530
<dependency>
2631
<groupId>com.learnit.itbulls</groupId>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.itbulls.learnit.onlinestore.web.servlets;
2+
3+
import jakarta.servlet.http.HttpServlet;
4+
import java.io.IOException;
5+
import java.util.List;
6+
7+
import com.itbulls.learnit.onlinestore.core.facades.UserFacade;
8+
import com.itbulls.learnit.onlinestore.core.facades.impl.DefaultUserFacade;
9+
import com.itbulls.learnit.onlinestore.persistence.enteties.User;
10+
11+
import jakarta.servlet.ServletException;
12+
import jakarta.servlet.annotation.WebServlet;
13+
import jakarta.servlet.http.HttpServletRequest;
14+
import jakarta.servlet.http.HttpServletResponse;
15+
16+
@WebServlet("/jstl-demo")
17+
public class JstlDemoServlet extends HttpServlet {
18+
19+
private UserFacade userFacade = DefaultUserFacade.getInstance();
20+
21+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
22+
List<User> users = userFacade.getUsers();
23+
request.setAttribute("users", users);
24+
request.getRequestDispatcher("/jstl-demo.jsp").forward(request, response);
25+
}
26+
}

‎online-store.web/src/main/webapp/jsp-demo.jsp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
21
<%@ page language="java" contentType="text/html; charset=UTF-8"
32
pageEncoding="UTF-8"%>
43
<%@ page import="java.util.Arrays" %>
Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
4+
<%@ taglib prefix="fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
5+
<%@ taglib prefix="fn" uri = "http://java.sun.com/jsp/jstl/functions" %>
6+
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
7+
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
8+
9+
<!DOCTYPE>
10+
<html>
11+
<head>
12+
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
13+
<title>JSTL Demo</title>
14+
</head>
15+
<body>
16+
17+
<p>========== Set and If Tags Demo ==========</p>
18+
<%-- scopes: page, request, session, application --%>
19+
<c:set var="testVar" value="100" scope="request"/>
20+
21+
<c:if test="${testVar > 5}">
22+
<p>Test Var exist on the page and it is more than 5</p>
23+
</c:if>
24+
25+
26+
<p>========== For each loop Demo ==========</p>
27+
<c:if test="${users != null}">
28+
<table border="1">
29+
<c:forEach items="${users}" var="user">
30+
<tr>
31+
<td>
32+
${user.id}
33+
</td>
34+
<td>
35+
${user.firstName}
36+
</td>
37+
<td>
38+
${user.lastName}
39+
</td>
40+
<td>
41+
${user.email}
42+
</td>
43+
</tr>
44+
</c:forEach>
45+
</table>
46+
</c:if>
47+
48+
49+
<p>========== Remove Tag Demo ==========</p>
50+
<c:remove var="testVar" />
51+
<p>After Remove Value: <c:out value="${testVar}" /></p>
52+
53+
54+
<p>========== Out Tag Demo ==========</p>
55+
<p>
56+
Let's print "<html> text" with "escapeXml" set to "false":
57+
<c:out value="<html> text" escapeXml="false"/>
58+
</p>
59+
<p>
60+
Let's print "<html> text" with "escapeXml" set to "true":
61+
<c:out value="<html> text" escapeXml="true"/>
62+
</p>
63+
<p>
64+
Let's print null value with "deafult" set to "default":
65+
<c:out value="${null}" default="default"></c:out>
66+
</p>
67+
68+
69+
70+
<p>========== Exception Handling Demo ==========</p>
71+
<%-- CATCH DEMO --%>
72+
<c:catch var="catchException">
73+
<%
74+
int x = 5 / 0;
75+
%>
76+
</c:catch>
77+
78+
<c:if test="${not empty catchException}">
79+
<p>
80+
The exception is : ${catchException} <br /> There is an exception:
81+
${catchException.message}
82+
</p>
83+
</c:if>
84+
85+
86+
<p>========== Choose Tag Demo ==========</p>
87+
<c:set var="salary" scope="session" value="10000" />
88+
<p>
89+
Your salary is :
90+
<c:out value="${salary}" />
91+
</p>
92+
<c:choose>
93+
<c:when test="${salary le 1000}">
94+
You have a good salary.
95+
</c:when>
96+
97+
<c:when test="${(salary gt 1000) and (salary lt 5000)}">
98+
Salary is very good.
99+
</c:when>
100+
101+
<c:otherwise>
102+
I'm proud of you, sir :)
103+
</c:otherwise>
104+
</c:choose>
105+
106+
<p>========== URL and Import Tags Demo ==========</p>
107+
<c:url value="jsp-include-demo.jsp" var="url" scope="session">
108+
<c:param name="partnerCode" value="123456" />
109+
<c:param name="couponCode" value="ASDFG" />
110+
</c:url>
111+
<p>URL is = ${url}</p>
112+
<c:import url="${url}" />
113+
114+
115+
<p>========== Formatting Tags Demo ==========</p>
116+
<h3>Number Format:</h3>
117+
<c:set var = "balance" value = "120000.2309" />
118+
119+
<p>Formatted Number (1): <fmt:formatNumber value = "${balance}"
120+
type = "currency"/></p>
121+
122+
<p>Formatted Number (2): <fmt:formatNumber type = "number"
123+
maxIntegerDigits = "3" value = "${balance}" /></p>
124+
125+
<p>Formatted Number (3): <fmt:formatNumber type = "number"
126+
maxFractionDigits = "3" value = "${balance}" /></p>
127+
128+
<p>Formatted Number (4): <fmt:formatNumber type = "number"
129+
groupingUsed = "false" value = "${balance}" /></p>
130+
131+
<p>Formatted Number (5): <fmt:formatNumber type = "percent"
132+
maxIntegerDigits="3" value = "${balance}" /></p>
133+
134+
<p>Formatted Number (6): <fmt:formatNumber type = "percent"
135+
minFractionDigits = "10" value = "${balance}" /></p>
136+
137+
<p>Formatted Number (7): <fmt:formatNumber type = "percent"
138+
maxIntegerDigits = "3" value = "${balance}" /></p>
139+
140+
<p>Formatted Number (8): <fmt:formatNumber type = "number"
141+
pattern = "###.###E0" value = "${balance}" /></p>
142+
143+
<p>Currency in USA :
144+
<fmt:setLocale value = "en_US"/>
145+
<fmt:formatNumber value = "${balance}" type = "currency"/>
146+
</p>
147+
148+
<%-- FORMAT DATE DEMO --%>
149+
<h3>Date Format:</h3>
150+
<c:set var = "now" value = "<%= new java.util.Date()%>" />
151+
152+
<p>Formatted Date (1): <fmt:formatDate type = "time"
153+
value = "${now}" /></p>
154+
155+
<p>Formatted Date (2): <fmt:formatDate type = "date"
156+
value = "${now}" /></p>
157+
158+
<p>Formatted Date (3): <fmt:formatDate type = "both"
159+
value = "${now}" /></p>
160+
161+
<p>Formatted Date (4): <fmt:formatDate type = "both"
162+
dateStyle = "short" timeStyle = "short" value = "${now}" /></p>
163+
164+
<p>Formatted Date (5): <fmt:formatDate type = "both"
165+
dateStyle = "medium" timeStyle = "medium" value = "${now}" /></p>
166+
167+
<p>Formatted Date (6): <fmt:formatDate type = "both"
168+
dateStyle = "long" timeStyle = "long" value = "${now}" /></p>
169+
170+
<p>Formatted Date (7): <fmt:formatDate pattern = "yyyy-MM-dd"
171+
value = "${now}" /></p>
172+
173+
174+
175+
<p>========== Function Tags Demo ==========</p>
176+
<%--FN DEMO contains --%>
177+
<c:set var = "theString" value = "I am a test String"/>
178+
179+
<c:if test = "${fn:contains(theString, 'test')}">
180+
<p>Found test string<p>
181+
</c:if>
182+
183+
<c:if test = "${fn:contains(theString, 'TEST')}">
184+
<p>Found TEST string<p>
185+
</c:if>
186+
187+
<c:if test = "${fn:containsIgnoreCase(theString, 'test')}">
188+
<p>Found test string<p>
189+
</c:if>
190+
191+
<c:if test = "${fn:containsIgnoreCase(theString, 'TEST')}">
192+
<p>Found TEST string<p>
193+
</c:if>
194+
195+
<%--FN ends with --%>
196+
<c:set var = "theString" value = "I am a test String 123"/>
197+
198+
<c:if test = "${fn:endsWith(theString, '123')}">
199+
<p>String ends with 123<p>
200+
</c:if>
201+
202+
<c:if test = "${fn:endsWith(theString, 'TEST')}">
203+
<p>String ends with TEST<p>
204+
</c:if>
205+
206+
<%-- DEMO index of --%>
207+
<c:set var = "string1" value = "This is first String."/>
208+
<c:set var = "string2" value = "This <abc>is second String.</abc>"/>
209+
<p>indexOf tag Demo in String 1: ${fn:indexOf(string1, "first")}</p>
210+
<p>indexOf tag Demo in String 2: ${fn:indexOf(string2, "second")}</p>
211+
212+
<%-- SPLIT and JOIN FN --%>
213+
<c:set var = "string1" value = "This is first String."/>
214+
<c:set var = "string2" value = "${fn:split(string1, ' ')}" />
215+
<c:set var = "string3" value = "${fn:join(string2, '-')}" />
216+
<p>Split and Join tags Demo: ${string3}</p>
217+
218+
<%--FN LENGTH --%>
219+
220+
<c:set var = "string1" value = "This is first String."/>
221+
<c:set var = "string2" value = "This is second String." />
222+
<p>Length of String (1) : ${fn:length(string1)}</p>
223+
<p>Length of String (2) : ${fn:length(string2)}</p>
224+
225+
226+
<%-- FN replace --%>
227+
<c:set var = "string1" value = "This is first String."/>
228+
<c:set var = "string2" value = "${fn:replace(string1, 'first', 'second')}" />
229+
<p>Replace tag demo: ${string2}</p>
230+
231+
<%-- FN starts with --%>
232+
<c:set var = "string" value = "Second: This is first String."/>
233+
234+
<c:if test = "${fn:startsWith(string, 'First')}">
235+
<p>String starts with First</p>
236+
</c:if>
237+
238+
<c:if test = "${fn:startsWith(string, 'Second')}">
239+
<p>String starts with Second</p>
240+
</c:if>
241+
242+
243+
<p>========== SQL Tags Demo ==========</p>
244+
<sql:setDataSource var="dataSource" driver="com.mysql.cj.jdbc.Driver"
245+
url="jdbc:mysql://localhost:3306/learn_it_db" user="root" password="root"/>
246+
247+
<sql:query dataSource="${dataSource}" var="rs">
248+
SELECT * FROM user;
249+
</sql:query>
250+
251+
<table border="1">
252+
<tr>
253+
<th>ID</th>
254+
<th>First Name</th>
255+
<th>Last Name</th>
256+
<th>Email</th>
257+
</tr>
258+
<c:forEach var="row" items="${rs.rows}">
259+
<tr>
260+
<td><c:out value="${row.id}"/></td>
261+
<td><c:out value="${row.first_name}"/></td>
262+
<td><c:out value="${row.last_name}"/></td>
263+
<td><c:out value="${row.email}"/></td>
264+
</tr>
265+
</c:forEach>
266+
</table>
267+
268+
269+
270+
<p>========== XML Tags Demo ==========</p>
271+
<c:set var="employees">
272+
<employees>
273+
<employee>
274+
<firstname>John</firstname>
275+
<lastname>Smith</lastname>
276+
<role>Employee</role>
277+
<salary>110000</salary>
278+
</employee>
279+
<employee>
280+
<firstname>Jack</firstname>
281+
<lastname>Jackson</lastname>
282+
<role>Manager</role>
283+
<salary>170000</salary>
284+
</employee>
285+
</employees>
286+
</c:set>
287+
<x:parse xml="${employees}" var="xml"/>
288+
<p>The first name of the first employee from the XML:
289+
<x:out select="$xml/employees/employee[1]/firstname/text()" />
290+
</p>
291+
<p>The last name of the first employee from the XML:
292+
<x:out select="$xml/employees/employee[1]/lastname/text()" />
293+
</p>
294+
295+
296+
297+
298+
</body>
299+
</html>

0 commit comments

Comments
 (0)