Skip to content

Commit

Permalink
Add a limit of 10 attributes per session to the session example
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed Dec 2, 2024
1 parent 721544e commit 84065e2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 34 deletions.
4 changes: 4 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@
Examples. Add the ability to delete session attributes in the servlet
session example. (markt)
</add>
<add>
Examples. Add a hard coded limit of 10 attributes per session for the
servlet session example. (markt)
</add>
</changelog>
</subsection>
<subsection name = "Other">
Expand Down
94 changes: 60 additions & 34 deletions webapps/examples/WEB-INF/classes/SessionExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class SessionExample extends HttpServlet {

private static final long serialVersionUID = 1L;

private static final int SESSION_ATTRIBUTE_COUNT_LIMIT = 10;


@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
ResourceBundle rb = ResourceBundle.getBundle("LocalStrings", request.getLocale());
Expand Down Expand Up @@ -76,15 +79,34 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
out.println(rb.getString("sessions.lastaccessed") + " ");
out.println(new Date(session.getLastAccessedTime()));

// Count the existing attributes
int sessionAttributeCount = 0;
Enumeration<String> names = session.getAttributeNames();
while (names.hasMoreElements()) {
names.nextElement();
sessionAttributeCount++;
}

String dataName = request.getParameter("dataname");
String dataValue = request.getParameter("datavalue");
if (dataName != null) {
session.setAttribute(dataName, dataValue);
if (dataValue == null) {
session.removeAttribute(dataName);
sessionAttributeCount--;
} else if (sessionAttributeCount < SESSION_ATTRIBUTE_COUNT_LIMIT) {
session.setAttribute(dataName, dataValue);
sessionAttributeCount++;
} else {
out.print("<p> Session attribute [");
out.print(HTMLFilter.filter(dataName));
out.print("] not added as there are already "+ SESSION_ATTRIBUTE_COUNT_LIMIT + " attributes in the ");
out.println("session. Delete an attribute before adding another.");
}
}

out.println("<P>");
out.println("<p>");
out.println(rb.getString("sessions.data") + "<br>");
Enumeration<String> names = session.getAttributeNames();
names = session.getAttributeNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
String value = session.getAttribute(name).toString();
Expand All @@ -96,37 +118,41 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
out.println("<br>");
}

out.println("<P>");
out.print("<form action=\"");
out.print(response.encodeURL("SessionExample"));
out.print("\" ");
out.println("method=POST>");
out.println(rb.getString("sessions.dataname"));
out.println("<input type=text size=20 name=dataname>");
out.println("<br>");
out.println(rb.getString("sessions.datavalue"));
out.println("<input type=text size=20 name=datavalue>");
out.println("<br>");
out.println("<input type=submit>");
out.println("</form>");

out.println("<P>GET based form:<br>");
out.print("<form action=\"");
out.print(response.encodeURL("SessionExample"));
out.print("\" ");
out.println("method=GET>");
out.println(rb.getString("sessions.dataname"));
out.println("<input type=text size=20 name=dataname>");
out.println("<br>");
out.println(rb.getString("sessions.datavalue"));
out.println("<input type=text size=20 name=datavalue>");
out.println("<br>");
out.println("<input type=submit>");
out.println("</form>");

out.print("<p><a href=\"");
out.print(HTMLFilter.filter(response.encodeURL("SessionExample?dataname=exampleName&datavalue=exampleValue")));
out.println("\" >URL encoded </a>");
if (sessionAttributeCount < SESSION_ATTRIBUTE_COUNT_LIMIT) {
out.println("<p>");
out.print("<form action=\"");
out.print(response.encodeURL("SessionExample"));
out.print("\" ");
out.println("method=POST>");
out.println(rb.getString("sessions.dataname"));
out.println("<input type=text size=20 name=dataname>");
out.println("<br>");
out.println(rb.getString("sessions.datavalue"));
out.println("<input type=text size=20 name=datavalue>");
out.println("<br>");
out.println("<input type=submit>");
out.println("</form>");

out.println("<p>GET based form:<br>");
out.print("<form action=\"");
out.print(response.encodeURL("SessionExample"));
out.print("\" ");
out.println("method=GET>");
out.println(rb.getString("sessions.dataname"));
out.println("<input type=text size=20 name=dataname>");
out.println("<br>");
out.println(rb.getString("sessions.datavalue"));
out.println("<input type=text size=20 name=datavalue>");
out.println("<br>");
out.println("<input type=submit>");
out.println("</form>");

out.print("<p><a href=\"");
out.print(HTMLFilter.filter(response.encodeURL("SessionExample?dataname=exampleName&datavalue=exampleValue")));
out.println("\" >URL encoded </a>");
} else {
out.print("<p>You may not add more than " + SESSION_ATTRIBUTE_COUNT_LIMIT + " attributes to this session.");
}

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

0 comments on commit 84065e2

Please sign in to comment.