質問編集履歴
1
ソースコードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -20,6 +20,209 @@
|
|
20
20
|
TomcatのサーバーでGachaServlet.javaを右クリック→サーバで実行をしましたところ、下記の様なエラーが出ました。
|
21
21
|

|
22
22
|
|
23
|
+
### ソースコード
|
24
|
+
以下が、今回で使うファイルのソースコードです。
|
25
|
+
- GachaServlet.java
|
26
|
+
```
|
27
|
+
import java.io.*;
|
28
|
+
import java.util.*;
|
29
|
+
import javax.servlet.*;
|
30
|
+
import javax.servlet.http.*;
|
31
|
+
import gacha.*;
|
32
|
+
import java.util.Random;
|
33
|
+
import java.sql.*;
|
34
|
+
|
35
|
+
public class GachaServlet extends HttpServlet {
|
36
|
+
private static String DB_NAME = "gacha_db";
|
37
|
+
private static String DB_USER = "root";
|
38
|
+
private static String DB_PASS = "root";
|
39
|
+
|
40
|
+
private static String LOGIN_JSP = "WEB-INF/Login.jsp";
|
41
|
+
private static String START_JSP = "WEB-INF/Start.jsp";
|
42
|
+
private static String MISS_JSP = "WEB-INF/Miss.jsp";
|
43
|
+
private static String HIT_JSP = "WEB-INF/Hit.jsp";
|
44
|
+
private static String LOG_JSP = "WEB-INF/log.jsp";
|
45
|
+
private static String RANKING_JSP = "WEB-INF/ranking.jsp";
|
46
|
+
|
47
|
+
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
48
|
+
doProcess(req, res);
|
49
|
+
}
|
50
|
+
|
51
|
+
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
52
|
+
doProcess(req, res);
|
53
|
+
}
|
54
|
+
|
55
|
+
protected void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
|
56
|
+
String nextView = "";
|
57
|
+
DatabaseConnector dc = null;
|
58
|
+
UserManager um = null;
|
59
|
+
RankingManager rm = null;
|
60
|
+
|
61
|
+
Random r = new Random();
|
62
|
+
int N = r.nextInt(100);
|
63
|
+
|
64
|
+
try {
|
65
|
+
dc = new DatabaseConnector(DB_NAME, DB_USER, DB_PASS);
|
66
|
+
dc.openConnection();
|
67
|
+
|
68
|
+
um = new UserManager(dc);
|
69
|
+
rm = new RankingManager(dc);
|
70
|
+
|
71
|
+
req.setCharacterEncoding("UTF-8");
|
72
|
+
res.setContentType("text/html; charset=UTF-8");
|
73
|
+
PrintWriter out = res.getWriter();
|
74
|
+
String action = req.getParameter("action");
|
75
|
+
if(action == null) {
|
76
|
+
action = "";
|
77
|
+
}
|
78
|
+
|
79
|
+
if(action.equals("") || action.equals("loginPage")) {
|
80
|
+
nextView = LOGIN_JSP;
|
81
|
+
} else if(action.equals("login")) {
|
82
|
+
if(login(um, req)) {
|
83
|
+
nextView = START_JSP;
|
84
|
+
} else {
|
85
|
+
nextView = LOGIN_JSP;
|
86
|
+
}
|
87
|
+
} else if(action.equals("logout")) {
|
88
|
+
nextView = logout(req);
|
89
|
+
} else if(action.equals("registration")) {
|
90
|
+
nextView = registration(um, req);
|
91
|
+
} else if(action.equals("gacha_result")) {
|
92
|
+
if(N >= 0 && 95 > N){
|
93
|
+
nextView = miss(req);
|
94
|
+
} else {
|
95
|
+
Connection con=null; Statement st=null;
|
96
|
+
String login="root"; String passwd="root";
|
97
|
+
ResultSet rs = null;
|
98
|
+
nextView = hit(req, rm);
|
99
|
+
}
|
100
|
+
} else if(action.equals("start")) {
|
101
|
+
nextView = START_JSP;
|
102
|
+
} else if(action.equals("log")) {
|
103
|
+
nextView = LOG_JSP;
|
104
|
+
} else if(action.equals("log_reset")) {
|
105
|
+
Connection con=null; Statement st=null;
|
106
|
+
String login="root"; String passwd="root";
|
107
|
+
ResultSet rs = null;
|
108
|
+
|
109
|
+
try {
|
110
|
+
Class.forName("com.mysql.jdbc.Driver");
|
111
|
+
|
112
|
+
String url = "jdbc:mysql://localhost/gacha_db" + "?useUnicode=true&characterEncoding=UTF-8" + "&autoReconnect=true";
|
113
|
+
con = DriverManager.getConnection(url, login, passwd);
|
114
|
+
st = con.createStatement();
|
115
|
+
String sql = "truncate table log_tbl";
|
116
|
+
rs = st.executeQuery(sql);
|
117
|
+
|
118
|
+
} catch (Exception e) {
|
119
|
+
} finally {
|
120
|
+
try {
|
121
|
+
rs.close();
|
122
|
+
con.close();
|
123
|
+
st.close();
|
124
|
+
} catch (Exception e) {
|
125
|
+
}
|
126
|
+
}
|
127
|
+
nextView = LOG_JSP;
|
128
|
+
} else if(action.equals("ranking")) {
|
129
|
+
nextView = RANKING_JSP;//showRanking(rm, req);
|
130
|
+
}
|
131
|
+
|
132
|
+
if(nextView.equals("")) {
|
133
|
+
req.setAttribute("message", "不正なアクションが要求されました(" + req.getParameter("action") + ")");
|
134
|
+
nextView = LOGIN_JSP;
|
135
|
+
}
|
136
|
+
dc.closeConnetion();
|
137
|
+
|
138
|
+
} catch(Exception e) {
|
139
|
+
e.printStackTrace();
|
140
|
+
req.setAttribute("message", "例外が発生しました:" + e.toString());
|
141
|
+
nextView = LOGIN_JSP;
|
142
|
+
} finally {
|
143
|
+
req.getRequestDispatcher(nextView).forward(req, res);
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
private boolean login(UserManager um, HttpServletRequest req) throws Exception {
|
148
|
+
String userName = req.getParameter("uname");
|
149
|
+
String password = req.getParameter("pass");
|
150
|
+
|
151
|
+
if(!isValid(userName) || !isValid(password)) {
|
152
|
+
req.setAttribute("message", "記入漏れがあります");
|
153
|
+
return false;
|
154
|
+
} else if(um.authenticate(userName, password) == false) {
|
155
|
+
req.setAttribute("message", "ユーザ名またはパスワードが違います");
|
156
|
+
return false;
|
157
|
+
} else {
|
158
|
+
HttpSession session = req.getSession(true);
|
159
|
+
int add = 0;
|
160
|
+
session.setAttribute("userName", userName);
|
161
|
+
session.setAttribute("add", add);
|
162
|
+
|
163
|
+
req.setAttribute("message", "認証に成功しました");
|
164
|
+
}
|
165
|
+
return true;
|
166
|
+
}
|
167
|
+
|
168
|
+
private String registration(UserManager um, HttpServletRequest req) throws Exception {
|
169
|
+
String userName = req.getParameter("uname");
|
170
|
+
String password = req.getParameter("pass");
|
171
|
+
String password2 = req.getParameter("pass2");
|
172
|
+
|
173
|
+
if(!isValid(userName) || !isValid(password) || !isValid(password2)) {
|
174
|
+
req.setAttribute("message", "記入漏れがあります");
|
175
|
+
} else if(!password.equals(password2)) {
|
176
|
+
req.setAttribute("message", "パスワードが確認用と一致しません");
|
177
|
+
} else if(um.registration(userName, password)) {
|
178
|
+
req.setAttribute("message", "登録に成功しました");
|
179
|
+
} else {
|
180
|
+
req.setAttribute("message", "すでに登録されています");
|
181
|
+
}
|
182
|
+
return LOGIN_JSP;
|
183
|
+
}
|
184
|
+
|
185
|
+
private String logout(HttpServletRequest req) {
|
186
|
+
HttpSession session = req.getSession(false);
|
187
|
+
if(session != null) {
|
188
|
+
session.invalidate();
|
189
|
+
req.setAttribute("message", "ログアウトしました");
|
190
|
+
}
|
191
|
+
return LOGIN_JSP;
|
192
|
+
}
|
193
|
+
|
194
|
+
private String miss(HttpServletRequest req){
|
195
|
+
HttpSession session = req.getSession(true);
|
196
|
+
int add = (Integer)session.getAttribute("add");
|
197
|
+
add += 100;
|
198
|
+
session.setAttribute("add", add);
|
199
|
+
|
200
|
+
return MISS_JSP;
|
201
|
+
}
|
202
|
+
|
203
|
+
private String hit(HttpServletRequest req, RankingManager rm) {
|
204
|
+
HttpSession session = req.getSession(true);
|
205
|
+
int add = (Integer)session.getAttribute("add");
|
206
|
+
add += 100;
|
207
|
+
session.setAttribute("add", add);
|
208
|
+
int MONEY = add;
|
209
|
+
String UNAME = (String)session.getAttribute("userName");
|
210
|
+
rm.RankingSet(MONEY, UNAME);
|
211
|
+
session.setAttribute("add", 0);
|
212
|
+
return HIT_JSP;
|
213
|
+
}
|
214
|
+
|
215
|
+
protected boolean isValid(String str) {
|
216
|
+
if(str != null && !str.equals("")) {
|
217
|
+
return true;
|
218
|
+
} else {
|
219
|
+
return false;
|
220
|
+
}
|
221
|
+
}
|
222
|
+
}
|
223
|
+
```
|
224
|
+
|
225
|
+
|
23
226
|
### 試したこと
|
24
227
|
今回、環境を整えるにあたり、行ったこと、確認したことを書きます。
|
25
228
|
- ウィンドウ→設定で、一般→ワークスペース→UTF-8を確認。
|