Skip to content

Commit

Permalink
Limit shopping cart to pre-defined items
Browse files Browse the repository at this point in the history
  • Loading branch information
markt-asf committed Dec 4, 2024
1 parent 335c827 commit 4a335c6
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 46 deletions.
4 changes: 4 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,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">
Expand Down
42 changes: 25 additions & 17 deletions webapps/examples/WEB-INF/classes/sessions/DummyCart.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -61,6 +69,6 @@ public void processRequest() {
// reset
private void reset() {
submit = null;
item = null;
itemId = -1;
}
}
37 changes: 37 additions & 0 deletions webapps/examples/WEB-INF/classes/sessions/Item.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion webapps/examples/jsp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ <h2>JSP 1.2 Examples</h2>
<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>
Expand Down
8 changes: 4 additions & 4 deletions webapps/examples/jsp/sessions/carts.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -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())); %>
<%
}
%>
Expand All @@ -39,5 +39,5 @@
</FONT>

<hr>
<%@ include file ="carts.html" %>
<%@ include file ="shopping.jsp" %>
</html>
7 changes: 5 additions & 2 deletions webapps/examples/jsp/sessions/crt.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,40 +13,38 @@
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">
<INPUT TYPE=submit name="submit" value="remove">

</form>

</FONT>
</font>
</body>
</html>

0 comments on commit 4a335c6

Please sign in to comment.