Session Servlet

This servlet demonstrates the use of HTTP sessions. You can add or delete named values to a session. The same session can be retrieved next time the user visits the page via a browser cookie. Use this form to see your additions and deletions at work. Check the code to see how adding and deleting session name/value pairs works.

<% if (request.getParameter("AddValue") != null) { session.setAttribute(addPrefix(request.getParameter("NameField")), request.getParameter("ValueField")); } else if (request.getParameter("DeleteValue") != null) { session.removeAttribute(addPrefix(request.getParameter("NameField"))); } %>
<% Enumeration sessionNames = session.getAttributeNames(); String name; while (sessionNames.hasMoreElements()) { name = (String)sessionNames.nextElement(); %> <% } // end of while loop for session names %>
Name Value
<%= removePrefix(name) %> <%= (String)session.getAttribute(name) %>

Name to add/delete Value

Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved. <%! static final String PREFIX_LABEL="SessionServlet."; private String removePrefix(String name) { if (name.startsWith(PREFIX_LABEL)) name = name.substring(PREFIX_LABEL.length()); return name; } private String addPrefix(String name) { if (!name.startsWith(PREFIX_LABEL)) name = PREFIX_LABEL + name; return name; } %>