This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/10.1.x by this push: new d63a10afc1 Limit shopping cart to pre-defined items d63a10afc1 is described below commit d63a10afc142b12f462a15f7d10f79fd80ff94eb Author: Mark Thomas <ma...@apache.org> AuthorDate: Wed Dec 4 15:14:17 2024 +0000 Limit shopping cart to pre-defined items --- webapps/docs/changelog.xml | 4 +++ .../WEB-INF/classes/sessions/DummyCart.java | 42 +++++++++++++--------- .../examples/WEB-INF/classes/sessions/Item.java | 37 +++++++++++++++++++ webapps/examples/jsp/index.html | 2 +- webapps/examples/jsp/sessions/carts.jsp | 8 ++--- webapps/examples/jsp/sessions/crt.html | 7 ++-- .../jsp/sessions/{carts.html => shopping.jsp} | 41 ++++++++++----------- 7 files changed, 95 insertions(+), 46 deletions(-) diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index f6c16129ea..f3a303c953 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -278,6 +278,10 @@ coded limit of 10 attributes per session for the JSP form authentication example. (markt) </add> + <add> + Examples. Limit the shopping cart example to only allow adding the + pre-defined items to the cart. (markt) + </add> </changelog> </subsection> <subsection name = "Other"> diff --git a/webapps/examples/WEB-INF/classes/sessions/DummyCart.java b/webapps/examples/WEB-INF/classes/sessions/DummyCart.java index 44decc98a9..7088557b95 100644 --- a/webapps/examples/WEB-INF/classes/sessions/DummyCart.java +++ b/webapps/examples/WEB-INF/classes/sessions/DummyCart.java @@ -16,42 +16,50 @@ */ package sessions; -import java.util.ArrayList; import java.util.Collections; -import java.util.List; +import java.util.HashSet; +import java.util.Set; public class DummyCart { - final List<String> items = Collections.synchronizedList(new ArrayList<>()); + final Set<Item> items = Collections.synchronizedSet(new HashSet<>()); + int itemId = -1; String submit = null; - String item = null; - private void addItem(String name) { - items.add(name); + public void setItemId(int itemId) { + this.itemId = itemId; } - private void removeItem(String name) { - items.remove(name); + public void setSubmit(String s) { + submit = s; } - public void setItem(String name) { - item = name; + private void addItem(int itemId) { + try { + items.add(Item.values()[itemId]); + } catch (ArrayIndexOutOfBoundsException e) { + // Ignore. Can only happen if user edits URL directly. + } } - public void setSubmit(String s) { - submit = s; + private void removeItem(int itemId) { + try { + items.remove(Item.values()[itemId]); + } catch (ArrayIndexOutOfBoundsException e) { + // Ignore. Can only happen if user edits URL directly. + } } - public String[] getItems() { - return items.toArray(new String[0]); + public Item[] getItems() { + return items.toArray(new Item[0]); } public void processRequest() { // null value for submit - user hit enter instead of clicking on // "add" or "remove" if (submit == null || submit.equals("add")) { - addItem(item); + addItem(itemId); } else if (submit.equals("remove")) { - removeItem(item); + removeItem(itemId); } // reset at the end of the request @@ -61,6 +69,6 @@ public class DummyCart { // reset private void reset() { submit = null; - item = null; + itemId = -1; } } diff --git a/webapps/examples/WEB-INF/classes/sessions/Item.java b/webapps/examples/WEB-INF/classes/sessions/Item.java new file mode 100644 index 0000000000..447d044437 --- /dev/null +++ b/webapps/examples/WEB-INF/classes/sessions/Item.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package sessions; + +public enum Item { + + VIDEO("Beavis & Butt-head Video collection"), + MOVIE("X-files movie"), + TAPES("Twin peaks tapes"), + CD("NIN CD"), + BOOK("JSP Book"), + TICKETS("Concert tickets"); + + private final String title; + + Item(String title) { + this.title = title; + } + + public String getTitle() { + return title; + } +} diff --git a/webapps/examples/jsp/index.html b/webapps/examples/jsp/index.html index 3d5ee31c02..cda01d62fd 100644 --- a/webapps/examples/jsp/index.html +++ b/webapps/examples/jsp/index.html @@ -228,7 +228,7 @@ This can be done using browser options.</p> <tr> <td>Carts</td> -<td style="width: 30%;"><a href="sessions/carts.html"><img src="images/execute.gif" alt=""></a><a href="sessions/carts.html">Execute</a></td> +<td style="width: 30%;"><a href="sessions/shopping.jsp"><img src="images/execute.gif" alt=""></a><a href="sessions/shopping.jsp">Execute</a></td> <td style="width: 30%;"><a href="sessions/crt.html"><img src="images/code.gif" alt=""></a><a href="sessions/crt.html">Source</a></td> </tr> diff --git a/webapps/examples/jsp/sessions/carts.jsp b/webapps/examples/jsp/sessions/carts.jsp index dc51495c4b..fa5624dee6 100644 --- a/webapps/examples/jsp/sessions/carts.jsp +++ b/webapps/examples/jsp/sessions/carts.jsp @@ -27,10 +27,10 @@ <br> You have the following items in your cart: <ol> <% - String[] items = cart.getItems(); - for (String item : items) { + Item[] items = cart.getItems(); + for (Item item : items) { %> -<li> <% out.print(util.HTMLFilter.filter(item)); %> +<li> <% out.print(util.HTMLFilter.filter(item.getTitle())); %> <% } %> @@ -39,5 +39,5 @@ </FONT> <hr> -<%@ include file ="carts.html" %> +<%@ include file ="shopping.jsp" %> </html> diff --git a/webapps/examples/jsp/sessions/crt.html b/webapps/examples/jsp/sessions/crt.html index 11e6edafe0..28ace66012 100644 --- a/webapps/examples/jsp/sessions/crt.html +++ b/webapps/examples/jsp/sessions/crt.html @@ -22,9 +22,12 @@ </head> <body bgcolor="#FFFFFF"> -<p><font color="#0000FF"><a href="carts.html"><img src="../images/execute.gif" align="right" border="0"></a><a href="../index.html"><img src="../images/return.gif" width="24" height="24" align="right" border="0"></a></font></p> +<p><font color="#0000FF"><a href="shopping.jsp"><img src="../images/execute.gif" align="right" border="0"></a><a href="../index.html"><img src="../images/return.gif" width="24" height="24" align="right" border="0"></a></font></p> -<h3><a href="carts.jsp.html">Source Code for Cart Example<font color="#0000FF"></a> +<h3><a href="shopping.jsp.html">Source Code for Cart Example Shopping Page<font color="#0000FF"></a> + </font> </h3> + +<h3><a href="carts.jsp.html">Source Code for Cart Example Cart Page<font color="#0000FF"></a> </font> </h3> <h3><a href="DummyCart.html">Property Sheet for DummyCart diff --git a/webapps/examples/jsp/sessions/carts.html b/webapps/examples/jsp/sessions/shopping.jsp similarity index 75% rename from webapps/examples/jsp/sessions/carts.html rename to webapps/examples/jsp/sessions/shopping.jsp index 834ee0a594..3487b10295 100644 --- a/webapps/examples/jsp/sessions/carts.html +++ b/webapps/examples/jsp/sessions/shopping.jsp @@ -1,5 +1,4 @@ -<html> -<!-- +<%-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. @@ -14,33 +13,31 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ---> - +--%> +<%@ page import="sessions.Item" %> +<html> <head> - <title>carts</title> +<title>Shopping Cart Example</title> </head> - <body bgcolor="white"> +<body bgcolor="white"> <font size = 5 color="#CC0000"> <form type=POST action=carts.jsp> -<BR> +<br> Please enter item to add or remove: <br> -Add Item: - -<SELECT NAME="item"> -<OPTION>Beavis & Butt-head Video collection -<OPTION>X-files movie -<OPTION>Twin peaks tapes -<OPTION>NIN CD -<OPTION>JSP Book -<OPTION>Concert tickets -<OPTION>Love life -<OPTION>Switch blade -<OPTION>Rex, Rugs & Rock n' Roll -</SELECT> - +Select Item: + +<select name="itemId"> +<% + for (Item item : Item.values()) { +%> + <option value="<%= item.ordinal() %>"><%= item.getTitle() %></option> +<% + } +%> +</select> <br> <br> <INPUT TYPE=submit name="submit" value="add"> @@ -48,6 +45,6 @@ Add Item: </form> -</FONT> +</font> </body> </html> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org