0
# JSTL Core Tags
1
2
The JSTL Core tag library provides fundamental JSP functionality including conditionals, loops, URL manipulation, variable management, and exception handling. These tags form the foundation of server-side logic in JSP pages.
3
4
## Tag Library Declaration
5
6
```jsp
7
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
8
```
9
10
## Capabilities
11
12
### URL Generation and Manipulation
13
14
Creates and manipulates URLs with automatic encoding and session handling, supporting both absolute and relative URL generation with parameter addition.
15
16
```jsp { .api }
17
<!-- Basic URL generation -->
18
<c:url value="/path/to/resource" />
19
20
<!-- URL with variable -->
21
<c:url value="/path/to/resource" var="urlVar" />
22
23
<!-- URL with parameters -->
24
<c:url value="/servlet">
25
<c:param name="param1" value="value1" />
26
<c:param name="param2" value="value2" />
27
</c:url>
28
```
29
30
**Usage Example:**
31
32
```jsp
33
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
34
35
<!-- Create a URL with parameters -->
36
<c:url value="/user/profile" var="profileUrl">
37
<c:param name="userId" value="${currentUser.id}" />
38
<c:param name="tab" value="settings" />
39
</c:url>
40
41
<!-- Use the generated URL -->
42
<a href="${profileUrl}">User Profile</a>
43
44
<!-- Direct inline usage -->
45
<form action="<c:url value='/submit'/>">
46
<!-- form content -->
47
</form>
48
```
49
50
### Parameter Management
51
52
Adds parameters to parent URL tags with automatic encoding and proper URL formatting.
53
54
```jsp { .api }
55
<c:param name="parameterName" value="parameterValue" />
56
<c:param name="parameterName" value="${dynamicValue}" />
57
```
58
59
### Exception Handling
60
61
Catches and handles exceptions that occur within the tag body, providing graceful error handling for JSP operations.
62
63
```jsp { .api }
64
<c:catch var="exceptionVariable">
65
<!-- Code that might throw exceptions -->
66
</c:catch>
67
```
68
69
**Usage Example:**
70
71
```jsp
72
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
73
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
74
75
<c:catch var="parseError">
76
<fmt:parseNumber var="number" value="${userInput}" />
77
</c:catch>
78
79
<c:if test="${parseError != null}">
80
<div class="error">
81
Invalid number format: ${parseError.message}
82
</div>
83
</c:if>
84
85
<c:if test="${parseError == null}">
86
<div class="success">
87
Parsed number: ${number}
88
</div>
89
</c:if>
90
```
91
92
### Conditional Processing
93
94
Evaluates conditions and conditionally includes content based on boolean expressions using EL (Expression Language).
95
96
```jsp { .api }
97
<c:if test="${condition}">
98
<!-- Content to include if condition is true -->
99
</c:if>
100
101
<c:if test="${condition}" var="resultVariable">
102
<!-- Content with result stored in variable -->
103
</c:if>
104
```
105
106
**Usage Example:**
107
108
```jsp
109
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
110
111
<!-- Simple condition -->
112
<c:if test="${user.isAdmin}">
113
<div class="admin-panel">Admin Controls</div>
114
</c:if>
115
116
<!-- Complex condition with operators -->
117
<c:if test="${user.age >= 18 && user.verified}">
118
<div class="adult-content">Adult Content Available</div>
119
</c:if>
120
121
<!-- Null checking -->
122
<c:if test="${sessionScope.cart != null && not empty sessionScope.cart.items}">
123
<div class="cart-summary">Items in cart: ${fn:length(sessionScope.cart.items)}</div>
124
</c:if>
125
```
126
127
### Variable Assignment
128
129
Sets and manages variables in various JSP scopes (page, request, session, application).
130
131
```jsp { .api }
132
<c:set var="variableName" value="value" />
133
<c:set var="variableName" value="${expression}" />
134
<c:set var="variableName" value="value" scope="page|request|session|application" />
135
<c:set var="variableName">
136
<!-- Body content as value -->
137
</c:set>
138
```
139
140
### Output with Escaping
141
142
Outputs values with automatic XML/HTML escaping to prevent XSS attacks.
143
144
```jsp { .api }
145
<c:out value="${expression}" />
146
<c:out value="${expression}" default="defaultValue" />
147
<c:out value="${expression}" escapeXml="true|false" />
148
```
149
150
## Error Handling
151
152
JSTL core tags can throw the following types of exceptions:
153
154
- **JspException**: General JSP processing errors
155
- **NumberFormatException**: When used with formatting operations
156
- **IllegalArgumentException**: Invalid parameter values
157
- **ELException**: Expression Language evaluation errors
158
159
Common error scenarios:
160
- Invalid EL expressions in test conditions
161
- Null pointer exceptions when accessing undefined variables
162
- Type conversion errors in parameter values
163
- Invalid scope specifications in variable assignments