Skip to content

Commit 4d5cc65

Browse files
committed
Limit shopping cart to pre-defined items
1 parent f88283b commit 4d5cc65

File tree

7 files changed

+95
-46
lines changed

7 files changed

+95
-46
lines changed

webapps/docs/changelog.xml

+4
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@
278278
coded limit of 10 attributes per session for the JSP form authentication
279279
example. (markt)
280280
</add>
281+
<add>
282+
Examples. Limit the shopping cart example to only allow adding the
283+
pre-defined items to the cart. (markt)
284+
</add>
281285
</changelog>
282286
</subsection>
283287
<subsection name = "Other">

webapps/examples/WEB-INF/classes/sessions/DummyCart.java

+25-17
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,50 @@
1616
*/
1717
package sessions;
1818

19-
import java.util.ArrayList;
2019
import java.util.Collections;
21-
import java.util.List;
20+
import java.util.HashSet;
21+
import java.util.Set;
2222

2323
public class DummyCart {
24-
final List<String> items = Collections.synchronizedList(new ArrayList<>());
24+
final Set<Item> items = Collections.synchronizedSet(new HashSet<>());
25+
int itemId = -1;
2526
String submit = null;
26-
String item = null;
2727

28-
private void addItem(String name) {
29-
items.add(name);
28+
public void setItemId(int itemId) {
29+
this.itemId = itemId;
3030
}
3131

32-
private void removeItem(String name) {
33-
items.remove(name);
32+
public void setSubmit(String s) {
33+
submit = s;
3434
}
3535

36-
public void setItem(String name) {
37-
item = name;
36+
private void addItem(int itemId) {
37+
try {
38+
items.add(Item.values()[itemId]);
39+
} catch (ArrayIndexOutOfBoundsException e) {
40+
// Ignore. Can only happen if user edits URL directly.
41+
}
3842
}
3943

40-
public void setSubmit(String s) {
41-
submit = s;
44+
private void removeItem(int itemId) {
45+
try {
46+
items.remove(Item.values()[itemId]);
47+
} catch (ArrayIndexOutOfBoundsException e) {
48+
// Ignore. Can only happen if user edits URL directly.
49+
}
4250
}
4351

44-
public String[] getItems() {
45-
return items.toArray(new String[0]);
52+
public Item[] getItems() {
53+
return items.toArray(new Item[0]);
4654
}
4755

4856
public void processRequest() {
4957
// null value for submit - user hit enter instead of clicking on
5058
// "add" or "remove"
5159
if (submit == null || submit.equals("add")) {
52-
addItem(item);
60+
addItem(itemId);
5361
} else if (submit.equals("remove")) {
54-
removeItem(item);
62+
removeItem(itemId);
5563
}
5664

5765
// reset at the end of the request
@@ -61,6 +69,6 @@ public void processRequest() {
6169
// reset
6270
private void reset() {
6371
submit = null;
64-
item = null;
72+
itemId = -1;
6573
}
6674
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package sessions;
18+
19+
public enum Item {
20+
21+
VIDEO("Beavis & Butt-head Video collection"),
22+
MOVIE("X-files movie"),
23+
TAPES("Twin peaks tapes"),
24+
CD("NIN CD"),
25+
BOOK("JSP Book"),
26+
TICKETS("Concert tickets");
27+
28+
private final String title;
29+
30+
Item(String title) {
31+
this.title = title;
32+
}
33+
34+
public String getTitle() {
35+
return title;
36+
}
37+
}

webapps/examples/jsp/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ <h2>JSP 1.2 Examples</h2>
228228
<tr>
229229
<td>Carts</td>
230230

231-
<td style="width: 30%;"><a href="sessions/carts.html"><img src="images/execute.gif" alt=""></a><a href="sessions/carts.html">Execute</a></td>
231+
<td style="width: 30%;"><a href="sessions/shopping.jsp"><img src="images/execute.gif" alt=""></a><a href="sessions/shopping.jsp">Execute</a></td>
232232

233233
<td style="width: 30%;"><a href="sessions/crt.html"><img src="images/code.gif" alt=""></a><a href="sessions/crt.html">Source</a></td>
234234
</tr>

webapps/examples/jsp/sessions/carts.jsp

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
<br> You have the following items in your cart:
2828
<ol>
2929
<%
30-
String[] items = cart.getItems();
31-
for (String item : items) {
30+
Item[] items = cart.getItems();
31+
for (Item item : items) {
3232
%>
33-
<li> <% out.print(util.HTMLFilter.filter(item)); %>
33+
<li> <% out.print(util.HTMLFilter.filter(item.getTitle())); %>
3434
<%
3535
}
3636
%>
@@ -39,5 +39,5 @@
3939
</FONT>
4040

4141
<hr>
42-
<%@ include file ="carts.html" %>
42+
<%@ include file ="shopping.jsp" %>
4343
</html>

webapps/examples/jsp/sessions/crt.html

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
</head>
2323

2424
<body bgcolor="#FFFFFF">
25-
<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>
25+
<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>
2626

27-
<h3><a href="carts.jsp.html">Source Code for Cart Example<font color="#0000FF"></a>
27+
<h3><a href="shopping.jsp.html">Source Code for Cart Example Shopping Page<font color="#0000FF"></a>
28+
</font> </h3>
29+
30+
<h3><a href="carts.jsp.html">Source Code for Cart Example Cart Page<font color="#0000FF"></a>
2831
</font> </h3>
2932

3033
<h3><a href="DummyCart.html">Property Sheet for DummyCart
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<html>
2-
<!--
1+
<%--
32
Licensed to the Apache Software Foundation (ASF) under one or more
43
contributor license agreements. See the NOTICE file distributed with
54
this work for additional information regarding copyright ownership.
@@ -14,40 +13,38 @@
1413
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1514
See the License for the specific language governing permissions and
1615
limitations under the License.
17-
-->
18-
16+
--%>
17+
<%@ page import="sessions.Item" %>
18+
<html>
1919
<head>
20-
<title>carts</title>
20+
<title>Shopping Cart Example</title>
2121
</head>
2222

23-
<body bgcolor="white">
23+
<body bgcolor="white">
2424
<font size = 5 color="#CC0000">
2525

2626
<form type=POST action=carts.jsp>
27-
<BR>
27+
<br>
2828
Please enter item to add or remove:
2929
<br>
30-
Add Item:
31-
32-
<SELECT NAME="item">
33-
<OPTION>Beavis & Butt-head Video collection
34-
<OPTION>X-files movie
35-
<OPTION>Twin peaks tapes
36-
<OPTION>NIN CD
37-
<OPTION>JSP Book
38-
<OPTION>Concert tickets
39-
<OPTION>Love life
40-
<OPTION>Switch blade
41-
<OPTION>Rex, Rugs & Rock n' Roll
42-
</SELECT>
43-
30+
Select Item:
31+
32+
<select name="itemId">
33+
<%
34+
for (Item item : Item.values()) {
35+
%>
36+
<option value="<%= item.ordinal() %>"><%= item.getTitle() %></option>
37+
<%
38+
}
39+
%>
40+
</select>
4441

4542
<br> <br>
4643
<INPUT TYPE=submit name="submit" value="add">
4744
<INPUT TYPE=submit name="submit" value="remove">
4845

4946
</form>
5047

51-
</FONT>
48+
</font>
5249
</body>
5350
</html>

0 commit comments

Comments
 (0)