This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push: new 18ffbac13f Add the ability to delete session attributes. 18ffbac13f is described below commit 18ffbac13f035dccdb3c6f55d3f7b07a2a1e4946 Author: Mark Thomas <ma...@apache.org> AuthorDate: Mon Dec 2 17:39:05 2024 +0000 Add the ability to delete session attributes. --- webapps/docs/changelog.xml | 4 ++++ webapps/examples/WEB-INF/classes/SessionExample.java | 14 +++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 99c9839252..917dc262f0 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -276,6 +276,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"> diff --git a/webapps/examples/WEB-INF/classes/SessionExample.java b/webapps/examples/WEB-INF/classes/SessionExample.java index 92c3d6b7f9..76b8fba15b 100644 --- a/webapps/examples/WEB-INF/classes/SessionExample.java +++ b/webapps/examples/WEB-INF/classes/SessionExample.java @@ -1,3 +1,4 @@ + /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -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; @@ -75,7 +78,7 @@ public class SessionExample extends HttpServlet { String dataName = request.getParameter("dataname"); String dataValue = request.getParameter("datavalue"); - if (dataName != null && dataValue != null) { + if (dataName != null) { session.setAttribute(dataName, dataValue); } @@ -85,7 +88,12 @@ public class SessionExample extends HttpServlet { 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>"); @@ -117,7 +125,7 @@ public class SessionExample extends HttpServlet { 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>"); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org