Skip to content

Commit

Permalink
Add the ability to delete session attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed Dec 2, 2024
1 parent 4f02366 commit c0a2392
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@
Examples. Expand the obfuscation of session cookie values in the request
header example to JSON responses. (markt)
</fix>
<add>
Examples. Add the ability to delete session attributes in the servlet
session example. (markt)
</add>
</changelog>
</subsection>
<subsection name = "Other">
Expand Down
14 changes: 11 additions & 3 deletions webapps/examples/WEB-INF/classes/SessionExample.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
Expand All @@ -16,6 +17,8 @@
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Enumeration;
import java.util.ResourceBundle;
Expand Down Expand Up @@ -75,7 +78,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro

String dataName = request.getParameter("dataname");
String dataValue = request.getParameter("datavalue");
if (dataName != null && dataValue != null) {
if (dataName != null) {
session.setAttribute(dataName, dataValue);
}

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

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

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

out.println("</body>");
Expand Down

0 comments on commit c0a2392

Please sign in to comment.