質問編集履歴

1

詳しい説明とファイルのコードを張りました。

2016/08/06 06:39

投稿

yamanoharu0129
yamanoharu0129

スコア47

test CHANGED
File without changes
test CHANGED
@@ -9,3 +9,249 @@
9
9
  だけではいけないのはわかるのですが、何をどう付け加えればいいのかわかりません。
10
10
 
11
11
  御教授お願いします。
12
+
13
+
14
+
15
+ 修正します。
16
+
17
+
18
+
19
+ 同一のjspのformからキーと値を入力して、POSTで送ってhashmapのcartに登録して、現在のカート内容のdiv内に表示しています。クリアボタンを押すcartの中身が削除されて、下記のtable内の値の表示が消えるようにしたいです。
20
+
21
+ 関係性ですが、サーブレットでの処理はすべて同じサーブレット内で行い。フォームからの入力、表示、クリアボタンもshow.jsp内にあります。
22
+
23
+ show.jpのフォームからPOSTでdoPOST内でsessionでhashmap内に名前と個数を登録します。
24
+
25
+ 登録した情報をカートの内容の部分に追加していきます。クリアのボタンを押すとcart内の値をすべて削除し、show.jspのカート内容の部分が消えるようにしたいです。処理はこのCart.javaとshow.jspだけで行っています。
26
+
27
+
28
+
29
+ Cart.java
30
+
31
+ ```import java.io.IOException;
32
+
33
+ import java.util.HashMap;
34
+
35
+
36
+
37
+ import javax.servlet.ServletException;
38
+
39
+ import javax.servlet.annotation.WebServlet;
40
+
41
+ import javax.servlet.http.HttpServlet;
42
+
43
+ import javax.servlet.http.HttpServletRequest;
44
+
45
+ import javax.servlet.http.HttpServletResponse;
46
+
47
+ import javax.servlet.http.HttpSession;
48
+
49
+
50
+
51
+ /**
52
+
53
+ * Servlet implementation class Cart
54
+
55
+ */
56
+
57
+ @WebServlet("/Cart")
58
+
59
+ public class Cart extends HttpServlet {
60
+
61
+ private static final long serialVersionUID = 1L;
62
+
63
+
64
+
65
+ /**
66
+
67
+ * @see HttpServlet#HttpServlet()
68
+
69
+ */
70
+
71
+ public Cart() {
72
+
73
+ super();
74
+
75
+ // TODO Auto-generated constructor stub
76
+
77
+ }
78
+
79
+
80
+
81
+ /**
82
+
83
+ * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
84
+
85
+ */
86
+
87
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
88
+
89
+ getServletContext().getRequestDispatcher("/WEB-INF/show.jsp").forward(request,response);
90
+
91
+ HttpSession session = request.getSession();
92
+
93
+ HashMap<String,Integer> cart = (HashMap<String,Integer>)session.getAttribute("cart");//保持されてる値を持ってきている
94
+
95
+ if(cart == null){//マップが作られていなかったらマップを作る
96
+
97
+ cart = new HashMap();
98
+
99
+ };
100
+
101
+ session.removeAttribute("cart");
102
+
103
+ //getServletContext().getRequestDispatcher("/WEB-INF/show.jsp").forward(request,response);
104
+
105
+ }
106
+
107
+
108
+
109
+ /**
110
+
111
+ * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
112
+
113
+ */
114
+
115
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
116
+
117
+ request.setCharacterEncoding("utf-8");
118
+
119
+ String name = request.getParameter("name");
120
+
121
+ String countString = request.getParameter("count");
122
+
123
+ Integer count = Integer.valueOf(countString);
124
+
125
+ HttpSession session = request.getSession();
126
+
127
+ HashMap<String,Integer> cart = (HashMap<String,Integer>)session.getAttribute("cart");//保持されてる値を持ってきている
128
+
129
+ if(cart == null){//マップが作られていなかったらマップを作る
130
+
131
+ cart = new HashMap();
132
+
133
+ };
134
+
135
+ //名前(key)が登録されていなかったら、マップにそのまま名前と個数を登録
136
+
137
+ //名前が登録していたら、値をプラスする
138
+
139
+ if(!(cart.containsKey(name))){//同じ名前のKeyがなければ実行
140
+
141
+ //get(name)はname(key)に対応する値(count)を求める
142
+
143
+ System.out.println("名前:"+name+" 個数:"+count);
144
+
145
+ cart.put(name,count);//名前と個数を登録
146
+
147
+ }else{//同じ名前のKeyがあった時に実行
148
+
149
+ System.out.println("名前:"+name+" 入力個数:"+count+" 合計:"+(count+cart.get(name)));
150
+
151
+ cart.put(name,count+cart.get(name));
152
+
153
+ };
154
+
155
+ System.out.println("cart.keySet():"+cart.keySet());
156
+
157
+ System.out.println("cart.get(name"+cart.get(count));
158
+
159
+ for (String str : cart.keySet()) {
160
+
161
+ System.out.println(str + ":" + cart.get(str));
162
+
163
+ }
164
+
165
+ session.setAttribute("cart", cart);
166
+
167
+ getServletContext().getRequestDispatcher("/WEB-INF/show.jsp").forward(request,response);
168
+
169
+ }
170
+
171
+
172
+
173
+ }
174
+
175
+ ```
176
+
177
+
178
+
179
+ show.jsp
180
+
181
+ ```<%@ page language="java" contentType="text/html; charset=UTF-8"
182
+
183
+ pageEncoding="UTF-8"%>
184
+
185
+ <%@ page import="java.util.HashMap" %>
186
+
187
+ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
188
+
189
+ <html>
190
+
191
+ <head>
192
+
193
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
194
+
195
+ <title>ショッピングカート</title>
196
+
197
+ </head>
198
+
199
+ <body>
200
+
201
+ <div style="border:#000000 solid 1px;">
202
+
203
+ <form action="Cart" method="POST">
204
+
205
+ <table>
206
+
207
+ <tr><td>名前:</td><td><input type="text" name="name" value="ごはん"></td><td></td></tr>
208
+
209
+ <tr><td>個数:</td><td><input type="text" name="count" value="1"></td><td></td></tr>
210
+
211
+ <tr><td></td><td></td><td><button>更新</button></td></tr>
212
+
213
+ </table>
214
+
215
+ </form>
216
+
217
+ </div>
218
+
219
+ <br>
220
+
221
+ <div style="border:#000000 solid 1px;">
222
+
223
+ <table>
224
+
225
+ <% HashMap<String,Integer> cart = (HashMap<String,Integer>)session.getAttribute("cart"); %>
226
+
227
+ <thead><tr><td>現在のカートの内容</td></tr></thead>
228
+
229
+ <tbody>
230
+
231
+ <% if(cart != null){
232
+
233
+ for(String name : cart.keySet()){
234
+
235
+ %>
236
+
237
+ <tr><td bgcolor="#00bfff"><%= name %></td><td><%= cart.get(name) %></td><td></td></tr>
238
+
239
+ <% }
240
+
241
+ };%>
242
+
243
+ </tbody>
244
+
245
+ </table>
246
+
247
+ <a href="cart?action=cart">
248
+
249
+ <button>カートをクリア</button></a>
250
+
251
+ </div>
252
+
253
+ </body>
254
+
255
+ </html>
256
+
257
+ ```