This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 721544ea28 Add the ability to delete session attributes.
721544ea28 is described below

commit 721544ea28e92549824b106be954a9f411867a1c
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 1431d3ebd9..8d3b6900f6 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -250,6 +250,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 407e2b3c0b..b3de2f8669 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

Reply via email to