Skip to content

Commit 721544e

Browse files
committed
Add the ability to delete session attributes.
1 parent 1d88dd3 commit 721544e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

webapps/docs/changelog.xml

+4
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@
250250
Examples. Expand the obfuscation of session cookie values in the request
251251
header example to JSON responses. (markt)
252252
</fix>
253+
<add>
254+
Examples. Add the ability to delete session attributes in the servlet
255+
session example. (markt)
256+
</add>
253257
</changelog>
254258
</subsection>
255259
<subsection name = "Other">

webapps/examples/WEB-INF/classes/SessionExample.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/*
23
* Licensed to the Apache Software Foundation (ASF) under one or more
34
* contributor license agreements. See the NOTICE file distributed with
@@ -16,6 +17,8 @@
1617
*/
1718
import java.io.IOException;
1819
import java.io.PrintWriter;
20+
import java.net.URLEncoder;
21+
import java.nio.charset.StandardCharsets;
1922
import java.util.Date;
2023
import java.util.Enumeration;
2124
import java.util.ResourceBundle;
@@ -75,7 +78,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
7578

7679
String dataName = request.getParameter("dataname");
7780
String dataValue = request.getParameter("datavalue");
78-
if (dataName != null && dataValue != null) {
81+
if (dataName != null) {
7982
session.setAttribute(dataName, dataValue);
8083
}
8184

@@ -85,7 +88,12 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
8588
while (names.hasMoreElements()) {
8689
String name = names.nextElement();
8790
String value = session.getAttribute(name).toString();
88-
out.println(HTMLFilter.filter(name) + " = " + HTMLFilter.filter(value) + "<br>");
91+
out.println(HTMLFilter.filter(name) + " = " + HTMLFilter.filter(value));
92+
out.print("<a href=\"");
93+
out.print(HTMLFilter.filter(
94+
response.encodeURL("SessionExample?dataname=" + URLEncoder.encode(name, StandardCharsets.UTF_8))));
95+
out.println("\" >delete</a>");
96+
out.println("<br>");
8997
}
9098

9199
out.println("<P>");
@@ -117,7 +125,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
117125
out.println("</form>");
118126

119127
out.print("<p><a href=\"");
120-
out.print(HTMLFilter.filter(response.encodeURL("SessionExample?dataname=foo&datavalue=bar")));
128+
out.print(HTMLFilter.filter(response.encodeURL("SessionExample?dataname=exampleName&datavalue=exampleValue")));
121129
out.println("\" >URL encoded </a>");
122130

123131
out.println("</body>");

0 commit comments

Comments
 (0)