Skip to content

Commit a95bf2b

Browse files
committed
Limit to 10 attributes. Add option to delete attribute.
1 parent c28b542 commit a95bf2b

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

webapps/docs/changelog.xml

+5
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@
278278
Examples. Add a hard coded limit of 10 attributes per session for the
279279
servlet session example. (markt)
280280
</add>
281+
<add>
282+
Examples. Add the ability to delete session attributes and add a hard
283+
coded limit of 10 attributes per session for the JSP form authentication
284+
example. (markt)
285+
</add>
281286
</changelog>
282287
</subsection>
283288
<subsection name = "Other">

webapps/examples/jsp/security/protected/index.jsp

+41-8
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
--%>
17-
<%@ page import="java.util.Enumeration" %>
17+
<%@ page import="java.net.URLEncoder" %>
18+
<%@ page import="java.nio.charset.StandardCharsets" %>
1819
<%@ page import="java.security.Principal" %>
20+
<%@ page import="java.util.Enumeration" %>
1921
<%@ page import="org.apache.catalina.TomcatPrincipal" %>
2022
<%
2123
if (request.getParameter("logoff") != null) {
@@ -121,31 +123,62 @@ enter it here:
121123
%>
122124
<br><br>
123125

126+
<%
127+
// Count the existing attributes
128+
int sessionAttributeCount = 0;
129+
Enumeration<String> names = session.getAttributeNames();
130+
while (names.hasMoreElements()) {
131+
names.nextElement();
132+
sessionAttributeCount++;
133+
}
134+
135+
String dataName = request.getParameter("dataName");
136+
String dataValue = request.getParameter("dataValue");
137+
if (dataName != null) {
138+
if (dataValue == null) {
139+
session.removeAttribute(dataName);
140+
sessionAttributeCount--;
141+
} else if (sessionAttributeCount < 10) {
142+
session.setAttribute(dataName, dataValue);
143+
sessionAttributeCount++;
144+
} else {
145+
%>
146+
<p>Session attribute [<%= util.HTMLFilter.filter(dataName) %>] not added as there are already 10 attributes in the
147+
session. Delete an attribute before adding another.</p>
148+
<%
149+
}
150+
}
151+
152+
if (sessionAttributeCount < 10) {
153+
%>
124154
To add some data to the authenticated session, enter it here:
125155
<form method="GET" action='<%= response.encodeURL("index.jsp") %>'>
126156
<input type="text" name="dataName">
127157
<input type="text" name="dataValue">
128158
<input type="submit" >
129159
</form>
130-
<br><br>
131-
132160
<%
133-
String dataName = request.getParameter("dataName");
134-
if (dataName != null) {
135-
session.setAttribute(dataName, request.getParameter("dataValue"));
161+
} else {
162+
%>
163+
<p>You may not add more than 10 attributes to this session.</p>
164+
<%
136165
}
137166
%>
167+
<br><br>
168+
138169
<p>The authenticated session contains the following attributes:</p>
139170
<table>
140171
<tr><th>Name</th><th>Value</th></tr>
141172
<%
142-
Enumeration<String> names = session.getAttributeNames();
173+
names = session.getAttributeNames();
143174
while (names.hasMoreElements()) {
144175
String name = names.nextElement();
176+
String value = session.getAttribute(name).toString();
145177
%>
146178
<tr>
147179
<td><%= util.HTMLFilter.filter(name) %></td>
148-
<td><%= util.HTMLFilter.filter(String.valueOf(session.getAttribute(name))) %></td>
180+
<td><%= util.HTMLFilter.filter(value) %></td>
181+
<td><a href='<%= response.encodeURL("index.jsp?dataName=" + URLEncoder.encode(name, StandardCharsets.UTF_8)) %>'>delete</a></td>
149182
</tr>
150183
<%
151184
}

0 commit comments

Comments
 (0)