質問編集履歴
2
サーブレット側のソースコードとパッケージの関連図を追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,6 +18,12 @@
|
|
18
18
|
|
19
19
|
|
20
20
|
|
21
|
+
主要ファイルの拡大図
|
22
|
+
|
23
|
+
![イメージ説明](ad62b0f0a1206cf5f318d99aaeb29aca.png)
|
24
|
+
|
25
|
+
|
26
|
+
|
21
27
|
### 発生している問題・エラーメッセージ
|
22
28
|
|
23
29
|
ブラウザから動作させると該当のレコードは取得できています。が、画像が表示できておりません。
|
@@ -86,6 +92,8 @@
|
|
86
92
|
|
87
93
|
|
88
94
|
|
95
|
+
■product.jsp
|
96
|
+
|
89
97
|
```jsp
|
90
98
|
|
91
99
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
@@ -150,6 +158,362 @@
|
|
150
158
|
|
151
159
|
|
152
160
|
|
161
|
+
①FrontController
|
162
|
+
|
163
|
+
```java
|
164
|
+
|
165
|
+
package tool;
|
166
|
+
|
167
|
+
import java.io.IOException;
|
168
|
+
|
169
|
+
import java.io.PrintWriter;
|
170
|
+
|
171
|
+
import java.lang.reflect.Constructor;
|
172
|
+
|
173
|
+
import javax.servlet.ServletException;
|
174
|
+
|
175
|
+
import javax.servlet.annotation.WebServlet;
|
176
|
+
|
177
|
+
import javax.servlet.http.HttpServlet;
|
178
|
+
|
179
|
+
import javax.servlet.http.HttpServletRequest;
|
180
|
+
|
181
|
+
import javax.servlet.http.HttpServletResponse;
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
@WebServlet(urlPatterns= {"*.action"})
|
186
|
+
|
187
|
+
public class FrontController extends HttpServlet {
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
public void doPost(HttpServletRequest request,
|
192
|
+
|
193
|
+
HttpServletResponse response)
|
194
|
+
|
195
|
+
throws ServletException, IOException {
|
196
|
+
|
197
|
+
PrintWriter out = response.getWriter();
|
198
|
+
|
199
|
+
try {
|
200
|
+
|
201
|
+
String path = request.getServletPath().substring(1);
|
202
|
+
|
203
|
+
String name = path.replace(".a","A").replace('/', '.');
|
204
|
+
|
205
|
+
//ProductActionを生成
|
206
|
+
|
207
|
+
Class<?> action = null;
|
208
|
+
|
209
|
+
action = Class.forName(name);
|
210
|
+
|
211
|
+
Constructor constructor = action.getDeclaredConstructor();
|
212
|
+
|
213
|
+
FrontAction faction = (FrontAction) constructor.newInstance();
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
String url = faction.execute(request,response);
|
218
|
+
|
219
|
+
request.getRequestDispatcher(url).forward(request, response);
|
220
|
+
|
221
|
+
} catch ( Exception e) {
|
222
|
+
|
223
|
+
e.printStackTrace(out);
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
public void doGet(HttpServletRequest request,
|
232
|
+
|
233
|
+
HttpServletResponse response)
|
234
|
+
|
235
|
+
throws ServletException, IOException {
|
236
|
+
|
237
|
+
doPost(request,response);
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
```
|
246
|
+
|
247
|
+
②ProductAction
|
248
|
+
|
249
|
+
```Java
|
250
|
+
|
251
|
+
package purchase;
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
import java.util.List;
|
256
|
+
|
257
|
+
import javax.servlet.http.HttpServletRequest;
|
258
|
+
|
259
|
+
import javax.servlet.http.HttpServletResponse;
|
260
|
+
|
261
|
+
import javax.servlet.http.HttpSession;
|
262
|
+
|
263
|
+
import bean.Product;
|
264
|
+
|
265
|
+
import dao.ProductDAO;
|
266
|
+
|
267
|
+
import tool.FrontAction;
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
public class ProductAction extends FrontAction {
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
public String execute (HttpServletRequest request,
|
276
|
+
|
277
|
+
HttpServletResponse response)
|
278
|
+
|
279
|
+
throws Exception {
|
280
|
+
|
281
|
+
HttpSession session=request.getSession();
|
282
|
+
|
283
|
+
String keyword = request.getParameter("keyword");
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
if (keyword==null) keyword="";
|
288
|
+
|
289
|
+
ProductDAO dao=new ProductDAO();
|
290
|
+
|
291
|
+
List<Product> list=dao.search(keyword);
|
292
|
+
|
293
|
+
session.setAttribute("list", list);
|
294
|
+
|
295
|
+
return "product.jsp";
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
```
|
302
|
+
|
303
|
+
③FrontAction
|
304
|
+
|
305
|
+
```java
|
306
|
+
|
307
|
+
package tool;
|
308
|
+
|
309
|
+
import javax.servlet.http.HttpServletRequest;
|
310
|
+
|
311
|
+
import javax.servlet.http.HttpServletResponse;
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
public abstract class FrontAction {
|
316
|
+
|
317
|
+
public abstract String execute (HttpServletRequest request,
|
318
|
+
|
319
|
+
HttpServletResponse response) throws Exception ;
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
```
|
324
|
+
|
325
|
+
④ProductDAO
|
326
|
+
|
327
|
+
```java
|
328
|
+
|
329
|
+
package dao;
|
330
|
+
|
331
|
+
import java.sql.Connection;
|
332
|
+
|
333
|
+
import java.sql.PreparedStatement;
|
334
|
+
|
335
|
+
import java.sql.ResultSet;
|
336
|
+
|
337
|
+
import java.util.ArrayList;
|
338
|
+
|
339
|
+
import java.util.List;
|
340
|
+
|
341
|
+
import bean.Product;
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
public class ProductDAO extends DAO {
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
public List<Product> search(String keyword) throws Exception {
|
350
|
+
|
351
|
+
List<Product> list=new ArrayList<>();
|
352
|
+
|
353
|
+
Connection con = getConnection();
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
PreparedStatement st = con.prepareStatement(
|
358
|
+
|
359
|
+
"select * from product where name like ?");
|
360
|
+
|
361
|
+
st.setString(1, "%"+keyword+"%");
|
362
|
+
|
363
|
+
ResultSet rs = st.executeQuery();
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
while(rs.next()) {
|
368
|
+
|
369
|
+
Product p=new Product();
|
370
|
+
|
371
|
+
p.setId(rs.getInt("id"));
|
372
|
+
|
373
|
+
p.setName(rs.getString("name"));
|
374
|
+
|
375
|
+
p.setPrice(rs.getInt("price"));
|
376
|
+
|
377
|
+
list.add(p);
|
378
|
+
|
379
|
+
}
|
380
|
+
|
381
|
+
st.close();
|
382
|
+
|
383
|
+
con.close();
|
384
|
+
|
385
|
+
return list;
|
386
|
+
|
387
|
+
}
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
```
|
392
|
+
|
393
|
+
⑤DAO
|
394
|
+
|
395
|
+
```java
|
396
|
+
|
397
|
+
package dao;
|
398
|
+
|
399
|
+
import java.sql.Connection;
|
400
|
+
|
401
|
+
import javax.naming.InitialContext;
|
402
|
+
|
403
|
+
import javax.sql.DataSource;
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
public class DAO {
|
408
|
+
|
409
|
+
static DataSource ds;
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
public Connection getConnection() throws Exception {
|
414
|
+
|
415
|
+
if (ds==null) {
|
416
|
+
|
417
|
+
InitialContext ic=new InitialContext();
|
418
|
+
|
419
|
+
ds=(DataSource)ic.lookup("java:/comp/env/jdbc/SampleDB");
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
return ds.getConnection();
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
}
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
```
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
⑥Product
|
436
|
+
|
437
|
+
```java
|
438
|
+
|
439
|
+
package bean;
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
public class Product implements java.io.Serializable{
|
444
|
+
|
445
|
+
private int id;
|
446
|
+
|
447
|
+
private String name;
|
448
|
+
|
449
|
+
private int price;
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
public int getId() {
|
454
|
+
|
455
|
+
return id;
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
public String getName ( ) {
|
462
|
+
|
463
|
+
return name;
|
464
|
+
|
465
|
+
}
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
public int getPrice() {
|
470
|
+
|
471
|
+
return price;
|
472
|
+
|
473
|
+
}
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
public void setId(int id) {
|
478
|
+
|
479
|
+
this.id = id;
|
480
|
+
|
481
|
+
}
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
public void setName(String name) {
|
486
|
+
|
487
|
+
this.name = name;
|
488
|
+
|
489
|
+
}
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
public void setPrice(int price) {
|
494
|
+
|
495
|
+
this.price = price;
|
496
|
+
|
497
|
+
}
|
498
|
+
|
499
|
+
}
|
500
|
+
|
501
|
+
```
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
②のjspを返却する前のリストの値をウオッチで値を確認したら以下のようになっております。
|
506
|
+
|
507
|
+
session.setAttribute("list", list);
|
508
|
+
|
509
|
+
<検索値にまぐろを設定>
|
510
|
+
|
511
|
+
id:1
|
512
|
+
|
513
|
+
name:まぐろ
|
514
|
+
|
515
|
+
price:100
|
516
|
+
|
153
517
|
|
154
518
|
|
155
519
|
### 試したこと
|
1
Eclipseの表記に誤字があった為に修正致しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
|
3
|
+
Eclipseでショッピングサイトの構築を学習しておりDBから取得した情報をビューとして
|
4
4
|
|
5
5
|
ブラウザに表示させようと考えております。具体的には以下の[現在の構成]の中の「puroduct.jsp」を実行し
|
6
6
|
|