From 4d5cc6538d91386f950373ac8120e98c2c78ed3a Mon Sep 17 00:00:00 2001 From: Mark Thomas Date: Wed, 4 Dec 2024 15:14:17 +0000 Subject: [PATCH] Limit shopping cart to pre-defined items Origin: https://github.com/apache/tomcat/commit/4d5cc6538d91386f950373ac8120e98c2c78ed3a --- webapps/docs/changelog.xml | 4 ++ .../WEB-INF/classes/sessions/DummyCart.java | 42 +++++++++++-------- .../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(-) create mode 100644 webapps/examples/WEB-INF/classes/sessions/Item.java rename webapps/examples/jsp/sessions/{carts.html => shopping.jsp} (75%) diff --git a/webapps/examples/WEB-INF/classes/sessions/DummyCart.java b/webapps/examples/WEB-INF/classes/sessions/DummyCart.java index 44decc98a9a2..7088557b9595 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 items = Collections.synchronizedList(new ArrayList<>()); + final Set 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 void processRequest() { // 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 000000000000..447d04443744 --- /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 b2371207cfe7..dc25005b2710 100644 --- a/webapps/examples/jsp/index.html +++ b/webapps/examples/jsp/index.html @@ -228,7 +228,7 @@

JSP 1.2 Examples

Carts -Execute +Execute Source diff --git a/webapps/examples/jsp/sessions/carts.jsp b/webapps/examples/jsp/sessions/carts.jsp index dc51495c4bec..fa5624dee6bd 100644 --- a/webapps/examples/jsp/sessions/carts.jsp +++ b/webapps/examples/jsp/sessions/carts.jsp @@ -27,10 +27,10 @@
You have the following items in your cart:
    <% - String[] items = cart.getItems(); - for (String item : items) { + Item[] items = cart.getItems(); + for (Item item : items) { %> -
  1. <% out.print(util.HTMLFilter.filter(item)); %> +
  2. <% out.print(util.HTMLFilter.filter(item.getTitle())); %> <% } %> @@ -39,5 +39,5 @@
    -<%@ include file ="carts.html" %> +<%@ include file ="shopping.jsp" %> diff --git a/webapps/examples/jsp/sessions/crt.html b/webapps/examples/jsp/sessions/crt.html index 11e6edafe0e9..28ace66012c7 100644 --- a/webapps/examples/jsp/sessions/crt.html +++ b/webapps/examples/jsp/sessions/crt.html @@ -22,9 +22,12 @@ -

    +

    -

    Source Code for Cart Example +

    Source Code for Cart Example Shopping Page +

    + +

    Source Code for Cart Example Cart Page

    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 834ee0a594fd..3487b1029547 100644 --- a/webapps/examples/jsp/sessions/carts.html +++ b/webapps/examples/jsp/sessions/shopping.jsp @@ -1,5 +1,4 @@ - - - +--%> +<%@ page import="sessions.Item" %> + - carts +Shopping Cart Example - +
    -
    +
    Please enter item to add or remove:
    -Add Item: - - - +Select Item: + +

    @@ -48,6 +45,6 @@
    -
    +