質問編集履歴
1
ソースコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -42,6 +42,412 @@
|
|
42
42
|
|
43
43
|
|
44
44
|
|
45
|
+
### ソースコード
|
46
|
+
|
47
|
+
以下が、今回で使うファイルのソースコードです。
|
48
|
+
|
49
|
+
- GachaServlet.java
|
50
|
+
|
51
|
+
```
|
52
|
+
|
53
|
+
import java.io.*;
|
54
|
+
|
55
|
+
import java.util.*;
|
56
|
+
|
57
|
+
import javax.servlet.*;
|
58
|
+
|
59
|
+
import javax.servlet.http.*;
|
60
|
+
|
61
|
+
import gacha.*;
|
62
|
+
|
63
|
+
import java.util.Random;
|
64
|
+
|
65
|
+
import java.sql.*;
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
public class GachaServlet extends HttpServlet {
|
70
|
+
|
71
|
+
private static String DB_NAME = "gacha_db";
|
72
|
+
|
73
|
+
private static String DB_USER = "root";
|
74
|
+
|
75
|
+
private static String DB_PASS = "root";
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
private static String LOGIN_JSP = "WEB-INF/Login.jsp";
|
80
|
+
|
81
|
+
private static String START_JSP = "WEB-INF/Start.jsp";
|
82
|
+
|
83
|
+
private static String MISS_JSP = "WEB-INF/Miss.jsp";
|
84
|
+
|
85
|
+
private static String HIT_JSP = "WEB-INF/Hit.jsp";
|
86
|
+
|
87
|
+
private static String LOG_JSP = "WEB-INF/log.jsp";
|
88
|
+
|
89
|
+
private static String RANKING_JSP = "WEB-INF/ranking.jsp";
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
94
|
+
|
95
|
+
doProcess(req, res);
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
102
|
+
|
103
|
+
doProcess(req, res);
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
protected void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
110
|
+
|
111
|
+
String nextView = "";
|
112
|
+
|
113
|
+
DatabaseConnector dc = null;
|
114
|
+
|
115
|
+
UserManager um = null;
|
116
|
+
|
117
|
+
RankingManager rm = null;
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
Random r = new Random();
|
122
|
+
|
123
|
+
int N = r.nextInt(100);
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
try {
|
128
|
+
|
129
|
+
dc = new DatabaseConnector(DB_NAME, DB_USER, DB_PASS);
|
130
|
+
|
131
|
+
dc.openConnection();
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
um = new UserManager(dc);
|
136
|
+
|
137
|
+
rm = new RankingManager(dc);
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
req.setCharacterEncoding("UTF-8");
|
142
|
+
|
143
|
+
res.setContentType("text/html; charset=UTF-8");
|
144
|
+
|
145
|
+
PrintWriter out = res.getWriter();
|
146
|
+
|
147
|
+
String action = req.getParameter("action");
|
148
|
+
|
149
|
+
if(action == null) {
|
150
|
+
|
151
|
+
action = "";
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
if(action.equals("") || action.equals("loginPage")) {
|
158
|
+
|
159
|
+
nextView = LOGIN_JSP;
|
160
|
+
|
161
|
+
} else if(action.equals("login")) {
|
162
|
+
|
163
|
+
if(login(um, req)) {
|
164
|
+
|
165
|
+
nextView = START_JSP;
|
166
|
+
|
167
|
+
} else {
|
168
|
+
|
169
|
+
nextView = LOGIN_JSP;
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
} else if(action.equals("logout")) {
|
174
|
+
|
175
|
+
nextView = logout(req);
|
176
|
+
|
177
|
+
} else if(action.equals("registration")) {
|
178
|
+
|
179
|
+
nextView = registration(um, req);
|
180
|
+
|
181
|
+
} else if(action.equals("gacha_result")) {
|
182
|
+
|
183
|
+
if(N >= 0 && 95 > N){
|
184
|
+
|
185
|
+
nextView = miss(req);
|
186
|
+
|
187
|
+
} else {
|
188
|
+
|
189
|
+
Connection con=null; Statement st=null;
|
190
|
+
|
191
|
+
String login="root"; String passwd="root";
|
192
|
+
|
193
|
+
ResultSet rs = null;
|
194
|
+
|
195
|
+
nextView = hit(req, rm);
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
} else if(action.equals("start")) {
|
200
|
+
|
201
|
+
nextView = START_JSP;
|
202
|
+
|
203
|
+
} else if(action.equals("log")) {
|
204
|
+
|
205
|
+
nextView = LOG_JSP;
|
206
|
+
|
207
|
+
} else if(action.equals("log_reset")) {
|
208
|
+
|
209
|
+
Connection con=null; Statement st=null;
|
210
|
+
|
211
|
+
String login="root"; String passwd="root";
|
212
|
+
|
213
|
+
ResultSet rs = null;
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
try {
|
218
|
+
|
219
|
+
Class.forName("com.mysql.jdbc.Driver");
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
String url = "jdbc:mysql://localhost/gacha_db" + "?useUnicode=true&characterEncoding=UTF-8" + "&autoReconnect=true";
|
224
|
+
|
225
|
+
con = DriverManager.getConnection(url, login, passwd);
|
226
|
+
|
227
|
+
st = con.createStatement();
|
228
|
+
|
229
|
+
String sql = "truncate table log_tbl";
|
230
|
+
|
231
|
+
rs = st.executeQuery(sql);
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
} catch (Exception e) {
|
236
|
+
|
237
|
+
} finally {
|
238
|
+
|
239
|
+
try {
|
240
|
+
|
241
|
+
rs.close();
|
242
|
+
|
243
|
+
con.close();
|
244
|
+
|
245
|
+
st.close();
|
246
|
+
|
247
|
+
} catch (Exception e) {
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
nextView = LOG_JSP;
|
254
|
+
|
255
|
+
} else if(action.equals("ranking")) {
|
256
|
+
|
257
|
+
nextView = RANKING_JSP;//showRanking(rm, req);
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
if(nextView.equals("")) {
|
264
|
+
|
265
|
+
req.setAttribute("message", "不正なアクションが要求されました(" + req.getParameter("action") + ")");
|
266
|
+
|
267
|
+
nextView = LOGIN_JSP;
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
dc.closeConnetion();
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
} catch(Exception e) {
|
276
|
+
|
277
|
+
e.printStackTrace();
|
278
|
+
|
279
|
+
req.setAttribute("message", "例外が発生しました:" + e.toString());
|
280
|
+
|
281
|
+
nextView = LOGIN_JSP;
|
282
|
+
|
283
|
+
} finally {
|
284
|
+
|
285
|
+
req.getRequestDispatcher(nextView).forward(req, res);
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
private boolean login(UserManager um, HttpServletRequest req) throws Exception {
|
294
|
+
|
295
|
+
String userName = req.getParameter("uname");
|
296
|
+
|
297
|
+
String password = req.getParameter("pass");
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
if(!isValid(userName) || !isValid(password)) {
|
302
|
+
|
303
|
+
req.setAttribute("message", "記入漏れがあります");
|
304
|
+
|
305
|
+
return false;
|
306
|
+
|
307
|
+
} else if(um.authenticate(userName, password) == false) {
|
308
|
+
|
309
|
+
req.setAttribute("message", "ユーザ名またはパスワードが違います");
|
310
|
+
|
311
|
+
return false;
|
312
|
+
|
313
|
+
} else {
|
314
|
+
|
315
|
+
HttpSession session = req.getSession(true);
|
316
|
+
|
317
|
+
int add = 0;
|
318
|
+
|
319
|
+
session.setAttribute("userName", userName);
|
320
|
+
|
321
|
+
session.setAttribute("add", add);
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
req.setAttribute("message", "認証に成功しました");
|
326
|
+
|
327
|
+
}
|
328
|
+
|
329
|
+
return true;
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
private String registration(UserManager um, HttpServletRequest req) throws Exception {
|
336
|
+
|
337
|
+
String userName = req.getParameter("uname");
|
338
|
+
|
339
|
+
String password = req.getParameter("pass");
|
340
|
+
|
341
|
+
String password2 = req.getParameter("pass2");
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
if(!isValid(userName) || !isValid(password) || !isValid(password2)) {
|
346
|
+
|
347
|
+
req.setAttribute("message", "記入漏れがあります");
|
348
|
+
|
349
|
+
} else if(!password.equals(password2)) {
|
350
|
+
|
351
|
+
req.setAttribute("message", "パスワードが確認用と一致しません");
|
352
|
+
|
353
|
+
} else if(um.registration(userName, password)) {
|
354
|
+
|
355
|
+
req.setAttribute("message", "登録に成功しました");
|
356
|
+
|
357
|
+
} else {
|
358
|
+
|
359
|
+
req.setAttribute("message", "すでに登録されています");
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
return LOGIN_JSP;
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
private String logout(HttpServletRequest req) {
|
370
|
+
|
371
|
+
HttpSession session = req.getSession(false);
|
372
|
+
|
373
|
+
if(session != null) {
|
374
|
+
|
375
|
+
session.invalidate();
|
376
|
+
|
377
|
+
req.setAttribute("message", "ログアウトしました");
|
378
|
+
|
379
|
+
}
|
380
|
+
|
381
|
+
return LOGIN_JSP;
|
382
|
+
|
383
|
+
}
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
private String miss(HttpServletRequest req){
|
388
|
+
|
389
|
+
HttpSession session = req.getSession(true);
|
390
|
+
|
391
|
+
int add = (Integer)session.getAttribute("add");
|
392
|
+
|
393
|
+
add += 100;
|
394
|
+
|
395
|
+
session.setAttribute("add", add);
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
return MISS_JSP;
|
400
|
+
|
401
|
+
}
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
private String hit(HttpServletRequest req, RankingManager rm) {
|
406
|
+
|
407
|
+
HttpSession session = req.getSession(true);
|
408
|
+
|
409
|
+
int add = (Integer)session.getAttribute("add");
|
410
|
+
|
411
|
+
add += 100;
|
412
|
+
|
413
|
+
session.setAttribute("add", add);
|
414
|
+
|
415
|
+
int MONEY = add;
|
416
|
+
|
417
|
+
String UNAME = (String)session.getAttribute("userName");
|
418
|
+
|
419
|
+
rm.RankingSet(MONEY, UNAME);
|
420
|
+
|
421
|
+
session.setAttribute("add", 0);
|
422
|
+
|
423
|
+
return HIT_JSP;
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
protected boolean isValid(String str) {
|
430
|
+
|
431
|
+
if(str != null && !str.equals("")) {
|
432
|
+
|
433
|
+
return true;
|
434
|
+
|
435
|
+
} else {
|
436
|
+
|
437
|
+
return false;
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
}
|
442
|
+
|
443
|
+
}
|
444
|
+
|
445
|
+
```
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
|
450
|
+
|
45
451
|
### 試したこと
|
46
452
|
|
47
453
|
今回、環境を整えるにあたり、行ったこと、確認したことを書きます。
|