質問編集履歴
19
追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
servletでsnsを作ってみたのですが、コンパイルエラーが出ます。何か基本的なミスはないでしょうか?
|
1
|
+
更新しました。よろしくお願いします。servletでsnsを作ってみたのですが、コンパイルエラーが出ます。何か基本的なミスはないでしょうか?
|
test
CHANGED
File without changes
|
18
簡潔に変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,10 +1,6 @@
|
|
1
|
-
javaでsnsのservletなのですが、今までは本を写していたのですが、あとは拡張しながら作っていってください、となってしまい、
|
2
|
-
|
3
|
-
|
1
|
+
snsのservletなのですが、プログラム的には、新規登録でuserIdとパスワードを設定するときに、userIdがデータベースにない場合は、createメソッドによりaccountを作れて、その後、ログインしたあとに、そのaccountからすべてのツイート履歴が見れて、新しいコメントは、insertされるというコードを作りたいです。
|
4
|
-
|
2
|
+
|
5
|
-
|
3
|
+
データベースに保存するようにして、Main.javaを作ったのですが、コンパイルエラーが出ます。
|
6
|
-
|
7
|
-
LoginServlet.javaは以前使っていたコードを新しいクラスやフィールドにかえるだけだったので無事コードをコンパイルエラーなしにかけたのですが、Main.javaは、これ以上進めない気がして、教えていただきたく質問させていただきました。
|
8
4
|
|
9
5
|
|
10
6
|
|
@@ -12,9 +8,7 @@
|
|
12
8
|
|
13
9
|
⚫️これがファイル名です。
|
14
10
|
|
15
|
-
データベースはh2databaseです
|
11
|
+
データベースはh2databaseでeclipseを使っています
|
16
|
-
|
17
|
-
eclipseを使っていて、pcはmacです。どうぞよろしくお願いします。
|
18
12
|
|
19
13
|
|
20
14
|
|
@@ -380,8 +374,6 @@
|
|
380
374
|
|
381
375
|
|
382
376
|
|
383
|
-
|
384
|
-
|
385
377
|
//creteメソッド
|
386
378
|
|
387
379
|
public boolean create(Account account){
|
@@ -502,13 +494,7 @@
|
|
502
494
|
|
503
495
|
|
504
496
|
|
505
|
-
|
506
|
-
|
507
|
-
//getIdとtextだけにした。
|
497
|
+
//getIdとtextだけにした。いいのかな?しかも、Mutterと丸かぶりだけど?
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
498
|
|
513
499
|
}
|
514
500
|
|
@@ -662,6 +648,6 @@
|
|
662
648
|
|
663
649
|
|
664
650
|
|
665
|
-
関連するコードをのせました。
|
651
|
+
関連するコードをのせました。膨大で申し訳ありません。
|
666
|
-
|
652
|
+
|
667
|
-
|
653
|
+
どうぞご指導のほど宜しくお願いします。
|
17
質問の趣旨の追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
servletでsnsを作ってみたのですが、コンパイルエラーが出ます。何か基本的なミスはないでしょうか?
|
test
CHANGED
File without changes
|
16
executeメソッドの引数はloginのため、accountやmutterをすべてloginに変え、loginのフィールドをLogin\.javaでuserIdとtextに変更しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -188,7 +188,7 @@
|
|
188
188
|
|
189
189
|
|
190
190
|
|
191
|
-
|
191
|
+
loginLogic.execute(login);
|
192
192
|
|
193
193
|
|
194
194
|
|
15
改訂
test
CHANGED
File without changes
|
test
CHANGED
@@ -164,7 +164,7 @@
|
|
164
164
|
|
165
165
|
//つぶやきリストを取得して、リクエストスコープに保存
|
166
166
|
|
167
|
-
|
167
|
+
//これもあるからとりあえずコメントアウト LoginLogic loginLogic =new LoginLogic();
|
168
168
|
|
169
169
|
|
170
170
|
|
14
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -174,7 +174,7 @@
|
|
174
174
|
|
175
175
|
|
176
176
|
|
177
|
-
米
|
177
|
+
米LoginLogic.execute(login);米 account=LoginLogic.execute(login);
|
178
178
|
|
179
179
|
|
180
180
|
|
@@ -184,7 +184,7 @@
|
|
184
184
|
|
185
185
|
|
186
186
|
|
187
|
-
米getUserId米 Mutter mutter=new Mutter(account.getUserId)
|
187
|
+
米getUserId米 Mutter mutter=new Mutter(account.getUserId);
|
188
188
|
|
189
189
|
|
190
190
|
|
@@ -198,7 +198,7 @@
|
|
198
198
|
|
199
199
|
|
200
200
|
|
201
|
-
米
|
201
|
+
米LoginLogic.execute(login)米 account=LoginLogic.execute(login);
|
202
202
|
|
203
203
|
|
204
204
|
|
13
コードを修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -138,7 +138,7 @@
|
|
138
138
|
|
139
139
|
//セッションスコープに保存されたUser情報を取得
|
140
140
|
|
141
|
-
|
141
|
+
//これはさっきと同じだからコメントアウト HttpSession session=request.getSession();
|
142
142
|
|
143
143
|
|
144
144
|
|
12
文字の修正
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
11
コンパイルエラーの表示方法の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -80,9 +80,9 @@
|
|
80
80
|
|
81
81
|
//Accountももはや普通の意味のAccountではない、なぜなら、loginがuserIdとtextでできていて、その情報からselectして得た情報をAccountに代入しているから
|
82
82
|
|
83
|
-
米 Account account = loginLogic.execute(login);
|
83
|
+
米login米 Account account = loginLogic.execute(login);
|
84
|
-
|
85
|
-
|
84
|
+
|
85
|
+
|
86
86
|
|
87
87
|
request.setAttribute("Account", account);
|
88
88
|
|
@@ -138,13 +138,13 @@
|
|
138
138
|
|
139
139
|
//セッションスコープに保存されたUser情報を取得
|
140
140
|
|
141
|
-
米
|
141
|
+
米session米 HttpSession session=request.getSession();
|
142
|
-
|
143
|
-
|
142
|
+
|
144
|
-
|
143
|
+
|
144
|
+
|
145
|
-
米
|
145
|
+
米account米 Account account=(Account)session.getAttribute("userId");
|
146
|
-
|
147
|
-
|
146
|
+
|
147
|
+
|
148
148
|
|
149
149
|
|
150
150
|
|
@@ -164,9 +164,9 @@
|
|
164
164
|
|
165
165
|
//つぶやきリストを取得して、リクエストスコープに保存
|
166
166
|
|
167
|
-
米 LoginLogic loginLogic =new LoginLogic(); ////
|
167
|
+
米loginLogic米 LoginLogic loginLogic =new LoginLogic(); ////
|
168
|
-
|
169
|
-
|
168
|
+
|
169
|
+
|
170
170
|
|
171
171
|
//とりあえずAccount.javaとMutter.java全く引数同じになっちゃってる
|
172
172
|
|
@@ -174,9 +174,9 @@
|
|
174
174
|
|
175
175
|
|
176
176
|
|
177
|
-
米
|
177
|
+
米account LoginLogic.execute(login);米 Account account=LoginLogic.execute(login);
|
178
|
-
|
179
|
-
|
178
|
+
|
179
|
+
|
180
180
|
|
181
181
|
request.setAttribute("Account",account);
|
182
182
|
|
@@ -184,13 +184,13 @@
|
|
184
184
|
|
185
185
|
|
186
186
|
|
187
|
-
米 Mutter mutter=new Mutter(account.getUserId)
|
187
|
+
米getUserId米 Mutter mutter=new Mutter(account.getUserId)
|
188
|
-
|
189
|
-
|
188
|
+
|
190
|
-
|
189
|
+
|
190
|
+
|
191
|
-
米 loginLogic.execute(account);
|
191
|
+
米execute米 loginLogic.execute(account);
|
192
|
-
|
193
|
-
|
192
|
+
|
193
|
+
|
194
194
|
|
195
195
|
|
196
196
|
|
@@ -198,9 +198,9 @@
|
|
198
198
|
|
199
199
|
|
200
200
|
|
201
|
-
米 Account account=LoginLogic.execute(login);
|
201
|
+
米account LoginLogic.execute(login)米 Account account=LoginLogic.execute(login);
|
202
|
-
|
203
|
-
|
202
|
+
|
203
|
+
|
204
204
|
|
205
205
|
request.setAttribute("Account",account);
|
206
206
|
|
10
配置の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -140,11 +140,11 @@
|
|
140
140
|
|
141
141
|
米 HttpSession session=request.getSession();
|
142
142
|
|
143
|
-
----------
|
143
|
+
----------
|
144
144
|
|
145
145
|
米 Account account=(Account)session.getAttribute("userId");
|
146
146
|
|
147
|
-
-----------
|
147
|
+
-----------
|
148
148
|
|
149
149
|
|
150
150
|
|
@@ -166,7 +166,7 @@
|
|
166
166
|
|
167
167
|
米 LoginLogic loginLogic =new LoginLogic(); ////
|
168
168
|
|
169
|
-
--------------
|
169
|
+
--------------
|
170
170
|
|
171
171
|
//とりあえずAccount.javaとMutter.java全く引数同じになっちゃってる
|
172
172
|
|
@@ -176,7 +176,7 @@
|
|
176
176
|
|
177
177
|
米 Account account=LoginLogic.execute(login);
|
178
178
|
|
179
|
-
-----------
|
179
|
+
-----------
|
180
180
|
|
181
181
|
request.setAttribute("Account",account);
|
182
182
|
|
@@ -186,26 +186,26 @@
|
|
186
186
|
|
187
187
|
米 Mutter mutter=new Mutter(account.getUserId)
|
188
188
|
|
189
|
-
-------------
|
189
|
+
-------------
|
190
190
|
|
191
191
|
米 loginLogic.execute(account);
|
192
192
|
|
193
|
-
----------
|
193
|
+
----------
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
//つぶやきリストを取得して、リクエストスコープに保存
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
米 Account account=LoginLogic.execute(login);
|
202
|
+
|
203
|
+
----------- ----------------------------------
|
204
|
+
|
205
|
+
request.setAttribute("Account",account);
|
194
206
|
|
195
207
|
|
196
208
|
|
197
|
-
//つぶやきリストを取得して、リクエストスコープに保存
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
米 Account account=LoginLogic.execute(login);
|
202
|
-
|
203
|
-
----------- ----------------------------------
|
204
|
-
|
205
|
-
request.setAttribute("Account",account);
|
206
|
-
|
207
|
-
|
208
|
-
|
209
209
|
}else{
|
210
210
|
|
211
211
|
//エラーメッセージをれクエストスコープに保存
|
9
Login\.javaの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -612,6 +612,56 @@
|
|
612
612
|
|
613
613
|
|
614
614
|
|
615
|
+
⚫️Login.java
|
616
|
+
|
617
|
+
|
618
|
+
|
619
|
+
package model;
|
620
|
+
|
621
|
+
|
622
|
+
|
623
|
+
//ほとんどをloginにしてて、今から直したら、新しいエラーが大量に出そうだから、loginにmutterをいれるわ
|
624
|
+
|
625
|
+
public class Login {
|
626
|
+
|
627
|
+
private String userId;
|
628
|
+
|
629
|
+
private String text;
|
630
|
+
|
631
|
+
|
632
|
+
|
633
|
+
|
634
|
+
|
635
|
+
//Loginメソッド
|
636
|
+
|
637
|
+
public Login(String userId,String text){
|
638
|
+
|
639
|
+
this.userId=userId;
|
640
|
+
|
641
|
+
this.text=text;
|
642
|
+
|
643
|
+
|
644
|
+
|
645
|
+
}
|
646
|
+
|
647
|
+
|
648
|
+
|
649
|
+
//getUserId・getPassメソッド
|
650
|
+
|
651
|
+
public String getUserId(){return userId;}
|
652
|
+
|
653
|
+
public String getText(){return text;}
|
654
|
+
|
655
|
+
|
656
|
+
|
657
|
+
|
658
|
+
|
659
|
+
}
|
660
|
+
|
661
|
+
|
662
|
+
|
663
|
+
|
664
|
+
|
615
665
|
関連するコードをのせました。量が膨大でみずらく申し訳ありません。
|
616
666
|
|
617
667
|
次の一歩を踏み出したく、どうぞご指導のほど宜しくお願いします。
|
8
エラーの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,7 +20,7 @@
|
|
20
20
|
|
21
21
|
⚫️Main.java
|
22
22
|
|
23
|
-
|
23
|
+
package servlet; //色のついた部分が変更箇所
|
24
24
|
|
25
25
|
|
26
26
|
|
@@ -76,13 +76,15 @@
|
|
76
76
|
|
77
77
|
//つぶやきリスト取得して、リクエストスコープに保存
|
78
78
|
|
79
|
-
|
79
|
+
LoginLogic loginLogic=new LoginLogic();
|
80
|
+
|
80
|
-
|
81
|
+
//Accountももはや普通の意味のAccountではない、なぜなら、loginがuserIdとtextでできていて、その情報からselectして得た情報をAccountに代入しているから
|
82
|
+
|
81
|
-
|
83
|
+
米 Account account = loginLogic.execute(login);
|
82
|
-
|
84
|
+
|
83
|
-
|
85
|
+
-------
|
84
|
-
|
86
|
+
|
85
|
-
|
87
|
+
request.setAttribute("Account", account);
|
86
88
|
|
87
89
|
|
88
90
|
|
@@ -114,6 +116,10 @@
|
|
114
116
|
|
115
117
|
}
|
116
118
|
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
117
123
|
|
118
124
|
|
119
125
|
//リクエストパラメータの取得
|
@@ -132,13 +138,13 @@
|
|
132
138
|
|
133
139
|
//セッションスコープに保存されたUser情報を取得
|
134
140
|
|
135
|
-
|
141
|
+
米 HttpSession session=request.getSession();
|
136
|
-
|
142
|
+
|
137
|
-
|
143
|
+
----------
|
138
|
-
|
144
|
+
|
139
|
-
米 Account account=(Account)session.getAttribute("userId");
|
145
|
+
米 Account account=(Account)session.getAttribute("userId");
|
140
|
-
|
146
|
+
|
141
|
-
|
147
|
+
-----------
|
142
148
|
|
143
149
|
|
144
150
|
|
@@ -148,19 +154,19 @@
|
|
148
154
|
|
149
155
|
//つぶやきをつぶやきリストに追加
|
150
156
|
|
151
|
-
|
157
|
+
Login login=new Login(account.getUserId(),text);
|
152
|
-
|
158
|
+
|
153
|
-
|
159
|
+
loginLogic.execute(login);
|
154
|
-
|
155
|
-
|
160
|
+
|
156
|
-
|
157
|
-
//この中(executeメソッドの中にinsertが入っている。)
|
161
|
+
//この中(executeメソッドの中にinsertが入っている。)
|
158
162
|
|
159
163
|
|
160
164
|
|
161
165
|
//つぶやきリストを取得して、リクエストスコープに保存
|
162
166
|
|
163
|
-
LoginLogic loginLogic =new LoginLogic(); ////
|
167
|
+
米 LoginLogic loginLogic =new LoginLogic(); ////
|
168
|
+
|
169
|
+
--------------
|
164
170
|
|
165
171
|
//とりあえずAccount.javaとMutter.java全く引数同じになっちゃってる
|
166
172
|
|
@@ -168,23 +174,23 @@
|
|
168
174
|
|
169
175
|
|
170
176
|
|
171
|
-
|
177
|
+
米 Account account=LoginLogic.execute(login);
|
172
|
-
|
178
|
+
|
173
|
-
|
179
|
+
-----------
|
174
|
-
|
180
|
+
|
175
|
-
request.setAttribute("Account",account);
|
181
|
+
request.setAttribute("Account",account);
|
176
182
|
|
177
183
|
|
178
184
|
|
179
185
|
|
180
186
|
|
181
|
-
|
187
|
+
米 Mutter mutter=new Mutter(account.getUserId)
|
182
|
-
|
188
|
+
|
183
|
-
|
189
|
+
-------------
|
184
|
-
|
190
|
+
|
185
|
-
|
191
|
+
米 loginLogic.execute(account);
|
186
|
-
|
192
|
+
|
187
|
-
|
193
|
+
----------
|
188
194
|
|
189
195
|
|
190
196
|
|
@@ -192,11 +198,11 @@
|
|
192
198
|
|
193
199
|
|
194
200
|
|
195
|
-
|
201
|
+
米 Account account=LoginLogic.execute(login);
|
196
|
-
|
202
|
+
|
197
|
-
|
203
|
+
----------- ----------------------------------
|
198
|
-
|
204
|
+
|
199
|
-
|
205
|
+
request.setAttribute("Account",account);
|
200
206
|
|
201
207
|
|
202
208
|
|
@@ -230,9 +236,7 @@
|
|
230
236
|
|
231
237
|
}
|
232
238
|
|
233
|
-
|
234
|
-
|
235
|
-
|
239
|
+
|
236
240
|
|
237
241
|
⚫️Account.DAO
|
238
242
|
|
7
\(\)ではなくアンダーラインに変更しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -78,9 +78,11 @@
|
|
78
78
|
|
79
79
|
LoginLogic loginLogic=new LoginLogic();
|
80
80
|
|
81
|
-
米
|
81
|
+
米 Account account = loginLogic.execute(new login);
|
82
|
+
|
82
|
-
|
83
|
+
-------
|
84
|
+
|
83
|
-
|
85
|
+
request.setAttribute("Account", account);
|
84
86
|
|
85
87
|
|
86
88
|
|
@@ -112,590 +114,498 @@
|
|
112
114
|
|
113
115
|
}
|
114
116
|
|
117
|
+
|
118
|
+
|
119
|
+
//リクエストパラメータの取得
|
120
|
+
|
121
|
+
request.setCharacterEncoding("UTF-8");
|
122
|
+
|
123
|
+
String text =request.getParameter("text");
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
//入力値チェック
|
128
|
+
|
129
|
+
if(text !=null && text.length()!=0){
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
//セッションスコープに保存されたUser情報を取得
|
134
|
+
|
135
|
+
米 HttpSession session=request.getSession();
|
136
|
+
|
137
|
+
----------
|
138
|
+
|
139
|
+
米 Account account=(Account)session.getAttribute("userId");
|
140
|
+
|
141
|
+
-----------
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
//つぶやきをつぶやきリストに追加(コードを開いてくとcreateでh2databaseに保存している。
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
//つぶやきをつぶやきリストに追加
|
150
|
+
|
151
|
+
Mutter mutter =new Mutter(account.getUserId(),text);
|
152
|
+
|
153
|
+
米 LoginLogic.execute(account);
|
154
|
+
|
155
|
+
-----------
|
156
|
+
|
157
|
+
//この中(executeメソッドの中にinsertが入っている。)
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
//つぶやきリストを取得して、リクエストスコープに保存
|
162
|
+
|
163
|
+
LoginLogic loginLogic =new LoginLogic(); ////
|
164
|
+
|
165
|
+
//とりあえずAccount.javaとMutter.java全く引数同じになっちゃってる
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
米 Account account=LoginLogic.execute();
|
172
|
+
|
173
|
+
----------
|
174
|
+
|
175
|
+
request.setAttribute("Account",account);
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
米 Mutter mutter=new Mutter(account.getUserId)
|
182
|
+
|
183
|
+
--------- -------------
|
184
|
+
|
185
|
+
米 logicLogic.execute(account);
|
186
|
+
|
187
|
+
-----------
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
//つぶやきリストを取得して、リクエストスコープに保存
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
米 Account account=LoginLogic.execute();
|
196
|
+
|
197
|
+
----------- -----------
|
198
|
+
|
199
|
+
request.setAttribute("Account",account);
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
}else{
|
204
|
+
|
205
|
+
//エラーメッセージをれクエストスコープに保存
|
206
|
+
|
207
|
+
request.setAttribute("errorMsg", "つぶやきが入力されていません");
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
//エラーメッセージをれクエストスコープに保存
|
212
|
+
|
213
|
+
//じゃなくてh2database使ってんだから、エラーメッセージをinsert?create? エラーメッセージはsessio?
|
214
|
+
|
215
|
+
//request.setAttribute("errorMsg", "つぶやきが入力されていません");
|
216
|
+
|
115
217
|
|
116
218
|
|
117
219
|
|
118
220
|
|
119
|
-
|
120
|
-
|
121
|
-
//リクエストパラメータの取得
|
122
|
-
|
123
|
-
request.setCharacterEncoding("UTF-8");
|
124
|
-
|
125
|
-
String text =request.getParameter("text");
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
//
|
221
|
+
//フォワード
|
222
|
+
|
130
|
-
|
223
|
+
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
|
224
|
+
|
225
|
+
dispatcher.forward(request, response);
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
⚫️Account.DAO
|
238
|
+
|
239
|
+
package dao;
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
import java.sql.Connection;
|
244
|
+
|
245
|
+
import java.sql.DriverManager;
|
246
|
+
|
131
|
-
|
247
|
+
import java.sql.PreparedStatement;
|
248
|
+
|
132
|
-
|
249
|
+
import java.sql.ResultSet;
|
250
|
+
|
133
|
-
|
251
|
+
import java.sql.SQLException;
|
252
|
+
|
253
|
+
|
254
|
+
|
134
|
-
|
255
|
+
import model.Account;
|
256
|
+
|
257
|
+
import model.Login;
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
public class AccountDAO {
|
262
|
+
|
263
|
+
@SuppressWarnings("finally")
|
264
|
+
|
265
|
+
|
266
|
+
|
135
|
-
|
267
|
+
//findByLoginメソッド
|
268
|
+
|
136
|
-
|
269
|
+
public Account findByLogin(Login login){
|
270
|
+
|
271
|
+
Connection conn=null;
|
272
|
+
|
273
|
+
Account account=null;
|
274
|
+
|
275
|
+
try{
|
276
|
+
|
277
|
+
//JDBCドライバを読み込み
|
278
|
+
|
137
|
-
|
279
|
+
Class.forName("org.h2.Driver");
|
138
|
-
|
139
|
-
米(account) Account account=(Account)session.getAttribute("userId");
|
140
280
|
|
141
281
|
|
142
282
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
//
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
}
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
283
|
+
//データベースに接続
|
284
|
+
|
285
|
+
conn=DriverManager.getConnection("jdbc:h2:file:/Users/taichi/Documents/networkspace/newsns/h2data.dat","sa","");
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
//SELECT文を準備
|
290
|
+
|
291
|
+
String sql="SELECT USER_ID,,TEXT FROM ACCOUNT WHERE USER_ID=? ";
|
292
|
+
|
293
|
+
//自動配信のIDだからかぶることはない、だから、IDだけで大丈夫
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
//ちょっと複雑
|
298
|
+
|
299
|
+
PreparedStatement pStmt=conn.prepareStatement(sql);
|
300
|
+
|
301
|
+
pStmt.setString(1,login.getUserId());
|
302
|
+
|
303
|
+
//SELECT文を実行し、結果表を取得
|
304
|
+
|
305
|
+
ResultSet rs=pStmt.executeQuery();
|
306
|
+
|
307
|
+
//ここで止まってる。
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
//一致したユーザーが存在した場合
|
312
|
+
|
313
|
+
//そのユーザーを表すAccountインスタンスを生成
|
314
|
+
|
315
|
+
if(rs.next()){
|
316
|
+
|
317
|
+
//結果表からデータを取得
|
318
|
+
|
319
|
+
String userId=rs.getString("USER_ID");
|
320
|
+
|
321
|
+
String text=rs.getString("TEXT");
|
322
|
+
|
323
|
+
//passとnameは入れない(selectにも入れてない)
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
//これで作る、これはメソッドAccountでaccountを作る
|
330
|
+
|
331
|
+
account=new Account(userId,text);
|
332
|
+
|
333
|
+
//作ってexecuteで使用している。
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
}catch(SQLException e){
|
338
|
+
|
339
|
+
e.printStackTrace();
|
340
|
+
|
341
|
+
}catch(ClassNotFoundException e){
|
342
|
+
|
343
|
+
e.printStackTrace();
|
344
|
+
|
345
|
+
return null;
|
346
|
+
|
347
|
+
}finally{
|
348
|
+
|
349
|
+
//データベースを切断
|
350
|
+
|
351
|
+
if(conn!=null){
|
352
|
+
|
353
|
+
try{
|
354
|
+
|
355
|
+
conn.close();
|
356
|
+
|
357
|
+
}catch(SQLException e){
|
358
|
+
|
359
|
+
e.printStackTrace();
|
360
|
+
|
361
|
+
return null;
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
//見つかったユーザーまたは、nullを返す。
|
368
|
+
|
369
|
+
return account;
|
370
|
+
|
371
|
+
}
|
200
372
|
|
201
373
|
}
|
202
374
|
|
203
|
-
|
375
|
+
|
204
|
-
|
205
|
-
|
376
|
+
|
206
|
-
|
207
|
-
|
377
|
+
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
378
|
+
|
379
|
+
|
380
|
+
|
213
|
-
|
381
|
+
//creteメソッド
|
382
|
+
|
214
|
-
|
383
|
+
public boolean create(Account account){
|
384
|
+
|
385
|
+
Connection conn=null;
|
386
|
+
|
387
|
+
try{
|
388
|
+
|
389
|
+
//データベースへ接続
|
390
|
+
|
215
|
-
|
391
|
+
conn=DriverManager.getConnection("jdbc:h2:file:/Users/taichi/Documents/networkspace/newsns/h2data.dat","sa","");
|
392
|
+
|
393
|
+
|
394
|
+
|
216
|
-
|
395
|
+
//insert文の準備(idは自動連番なので、指定しなくてもいい)
|
396
|
+
|
397
|
+
String sql="INSERT INTO ACCOUNT (USER_ID,TEXT)VALUES(?,?,?)";
|
398
|
+
|
399
|
+
PreparedStatement pStmt=conn.prepareStatement(sql);
|
400
|
+
|
401
|
+
//insert文中の「?」に使用する値を指定し、SQL文を完成
|
402
|
+
|
403
|
+
//さっき作ったaccountをここで、使うinsertのときにgetUserIdとかしたやつを使う
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
pStmt.setString(1, account.getUserId());
|
408
|
+
|
409
|
+
pStmt.setString(2, account.getText());
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
//insert文を実行
|
416
|
+
|
217
|
-
|
417
|
+
int result=pStmt.executeUpdate();
|
418
|
+
|
419
|
+
|
420
|
+
|
218
|
-
|
421
|
+
if(result !=1){
|
422
|
+
|
219
|
-
|
423
|
+
return false;
|
220
|
-
|
424
|
+
|
221
|
-
|
425
|
+
}
|
426
|
+
|
427
|
+
}catch(SQLException e){
|
428
|
+
|
429
|
+
e.printStackTrace();
|
430
|
+
|
431
|
+
return false;
|
432
|
+
|
433
|
+
}finally{
|
434
|
+
|
435
|
+
//データベースを切断
|
436
|
+
|
437
|
+
if(conn!=null){
|
438
|
+
|
439
|
+
try{
|
440
|
+
|
441
|
+
conn.close();
|
442
|
+
|
443
|
+
}catch(SQLException e){
|
444
|
+
|
445
|
+
e.printStackTrace();
|
446
|
+
|
447
|
+
}
|
448
|
+
|
449
|
+
}
|
222
450
|
|
223
451
|
}
|
224
452
|
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
i
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
453
|
+
return true;
|
454
|
+
|
455
|
+
}
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
⚫️Account.java
|
462
|
+
|
463
|
+
package model;
|
464
|
+
|
465
|
+
//Accountは3つuserId,pass,mail
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
public class Account {
|
470
|
+
|
471
|
+
//フィールド
|
472
|
+
|
473
|
+
private String userId;
|
474
|
+
|
475
|
+
private String text;
|
476
|
+
|
477
|
+
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
//Accountメソッド引数全て、
|
482
|
+
|
483
|
+
public Account(String userId,String text){
|
484
|
+
|
485
|
+
this.userId=userId;
|
486
|
+
|
487
|
+
this.text=text;
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
}
|
492
|
+
|
493
|
+
//UserId.Passとか
|
494
|
+
|
495
|
+
public String getUserId(){return userId;}
|
496
|
+
|
497
|
+
public String getText(){return text;}
|
498
|
+
|
499
|
+
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
//getIdとtextだけにした。すっきりしたけどいいのかな?しかも、これって、Mutterと丸かぶりだよね?これでもいいのかな?MutterDAOを見てみよう
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
}
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
|
514
|
+
|
515
|
+
⚫️Mutter.java
|
516
|
+
|
517
|
+
package model; //色のついた部分が変更箇所
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
import java.io.Serializable;
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
public class Mutter implements Serializable{
|
526
|
+
|
527
|
+
/**
|
528
|
+
|
529
|
+
*
|
530
|
+
|
531
|
+
*/
|
532
|
+
|
533
|
+
private static final long serialVersionUID = 1L;
|
534
|
+
|
535
|
+
private String userId; //user内容
|
536
|
+
|
537
|
+
private String text; //つぶやき内容
|
538
|
+
|
539
|
+
|
540
|
+
|
541
|
+
public Mutter(){}
|
542
|
+
|
543
|
+
public Mutter(String userId,String text){
|
544
|
+
|
545
|
+
this.userId=userId;
|
546
|
+
|
547
|
+
this.text=text;
|
548
|
+
|
549
|
+
}
|
550
|
+
|
551
|
+
|
272
552
|
|
273
553
|
|
274
554
|
|
555
|
+
public String getUserId(){return userId;}
|
556
|
+
|
557
|
+
public String getText(){return text;}
|
558
|
+
|
559
|
+
//Account.javaとMutter.java同じになった
|
560
|
+
|
561
|
+
|
562
|
+
|
563
|
+
}
|
564
|
+
|
565
|
+
|
566
|
+
|
567
|
+
|
568
|
+
|
569
|
+
⚫️LoginLogic
|
570
|
+
|
571
|
+
|
572
|
+
|
573
|
+
package model;
|
574
|
+
|
575
|
+
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
import dao.AccountDAO;
|
580
|
+
|
581
|
+
|
582
|
+
|
583
|
+
|
584
|
+
|
585
|
+
public class LoginLogic{
|
586
|
+
|
587
|
+
|
588
|
+
|
275
|
-
|
589
|
+
//executメソッド
|
590
|
+
|
276
|
-
|
591
|
+
public boolean execute(Login login){
|
592
|
+
|
593
|
+
AccountDAO dao=new AccountDAO();
|
594
|
+
|
595
|
+
Account account=dao.findByLogin(login);
|
596
|
+
|
597
|
+
//public Account findByLogin(Login login){となっているが引数は一つでいいの?これって、引数を一つにするやつだっけ?
|
598
|
+
|
599
|
+
return account !=null;
|
600
|
+
|
277
|
-
c
|
601
|
+
//目的は、taichiのGetMutterListLogicと同じ、つまり、insertするために、account=get.UserIdとかとするためであって、それがGetMutterListLogicでは、mutterListに追加するだけである。
|
278
602
|
|
279
603
|
|
280
604
|
|
281
|
-
//SELECT文を準備
|
282
|
-
|
283
|
-
String sql="SELECT USER_ID,,TEXT FROM ACCOUNT WHERE USER_ID=? ";
|
284
|
-
|
285
|
-
//自動配信のIDだからかぶることはない、だから、IDだけで大丈夫
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
//ちょっと複雑
|
290
|
-
|
291
|
-
PreparedStatement pStmt=conn.prepareStatement(sql);
|
292
|
-
|
293
|
-
pStmt.setString(1,login.getUserId());
|
294
|
-
|
295
|
-
//SELECT文を実行し、結果表を取得
|
296
|
-
|
297
|
-
ResultSet rs=pStmt.executeQuery();
|
298
|
-
|
299
|
-
//ここで止まってる。
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
//一致したユーザーが存在した場合
|
304
|
-
|
305
|
-
//そのユーザーを表すAccountインスタンスを生成
|
306
|
-
|
307
|
-
if(rs.next()){
|
308
|
-
|
309
|
-
//結果表からデータを取得
|
310
|
-
|
311
|
-
String userId=rs.getString("USER_ID");
|
312
|
-
|
313
|
-
String text=rs.getString("TEXT");
|
314
|
-
|
315
|
-
//passとnameは入れない(selectにも入れてない)
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
//これで作る、これはメソッドAccountでaccountを作る
|
322
|
-
|
323
|
-
account=new Account(userId,text);
|
324
|
-
|
325
|
-
//作ってexecuteで使用している。
|
326
|
-
|
327
|
-
}
|
328
|
-
|
329
|
-
}catch(SQLException e){
|
330
|
-
|
331
|
-
e.printStackTrace();
|
332
|
-
|
333
|
-
}catch(ClassNotFoundException e){
|
334
|
-
|
335
|
-
e.printStackTrace();
|
336
|
-
|
337
|
-
return null;
|
338
|
-
|
339
|
-
}finally{
|
340
|
-
|
341
|
-
//データベースを切断
|
342
|
-
|
343
|
-
if(conn!=null){
|
344
|
-
|
345
|
-
try{
|
346
|
-
|
347
|
-
conn.close();
|
348
|
-
|
349
|
-
}catch(SQLException e){
|
350
|
-
|
351
|
-
e.printStackTrace();
|
352
|
-
|
353
|
-
return null;
|
354
|
-
|
355
|
-
}
|
356
|
-
|
357
|
-
}
|
358
|
-
|
359
|
-
//見つかったユーザーまたは、nullを返す。
|
360
|
-
|
361
|
-
return account;
|
362
|
-
|
363
605
|
}
|
364
606
|
|
365
607
|
}
|
366
608
|
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
//creteメソッド
|
374
|
-
|
375
|
-
public boolean create(Account account){
|
376
|
-
|
377
|
-
Connection conn=null;
|
378
|
-
|
379
|
-
try{
|
380
|
-
|
381
|
-
//データベースへ接続
|
382
|
-
|
383
|
-
conn=DriverManager.getConnection("jdbc:h2:file:/Users/taichi/Documents/networkspace/newsns/h2data.dat","sa","");
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
//insert文の準備(idは自動連番なので、指定しなくてもいい)
|
388
|
-
|
389
|
-
String sql="INSERT INTO ACCOUNT (USER_ID,TEXT)VALUES(?,?,?)";
|
390
|
-
|
391
|
-
PreparedStatement pStmt=conn.prepareStatement(sql);
|
392
|
-
|
393
|
-
//insert文中の「?」に使用する値を指定し、SQL文を完成
|
394
|
-
|
395
|
-
//さっき作ったaccountをここで、使うinsertのときにgetUserIdとかしたやつを使う
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
pStmt.setString(1, account.getUserId());
|
400
|
-
|
401
|
-
pStmt.setString(2, account.getText());
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
//insert文を実行
|
408
|
-
|
409
|
-
int result=pStmt.executeUpdate();
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
if(result !=1){
|
414
|
-
|
415
|
-
return false;
|
416
|
-
|
417
|
-
}
|
418
|
-
|
419
|
-
}catch(SQLException e){
|
420
|
-
|
421
|
-
e.printStackTrace();
|
422
|
-
|
423
|
-
return false;
|
424
|
-
|
425
|
-
}finally{
|
426
|
-
|
427
|
-
//データベースを切断
|
428
|
-
|
429
|
-
if(conn!=null){
|
430
|
-
|
431
|
-
try{
|
432
|
-
|
433
|
-
conn.close();
|
434
|
-
|
435
|
-
}catch(SQLException e){
|
436
|
-
|
437
|
-
e.printStackTrace();
|
438
|
-
|
439
|
-
}
|
440
|
-
|
441
|
-
}
|
442
|
-
|
443
|
-
}
|
444
|
-
|
445
|
-
return true;
|
446
|
-
|
447
|
-
}
|
448
|
-
|
449
|
-
}
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
⚫️Account.java
|
454
|
-
|
455
|
-
package model;
|
456
|
-
|
457
|
-
//Accountは3つuserId,pass,mail
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
public class Account {
|
462
|
-
|
463
|
-
//フィールド
|
464
|
-
|
465
|
-
private String userId;
|
466
|
-
|
467
|
-
private String text;
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
//Accountメソッド引数全て、
|
474
|
-
|
475
|
-
public Account(String userId,String text){
|
476
|
-
|
477
|
-
this.userId=userId;
|
478
|
-
|
479
|
-
this.text=text;
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
}
|
484
|
-
|
485
|
-
//UserId.Passとか
|
486
|
-
|
487
|
-
public String getUserId(){return userId;}
|
488
|
-
|
489
|
-
public String getText(){return text;}
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
//getIdとtextだけにした。すっきりしたけどいいのかな?しかも、これって、Mutterと丸かぶりだよね?これでもいいのかな?MutterDAOを見てみよう
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
}
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
⚫️Mutter.java
|
508
|
-
|
509
|
-
package model; //色のついた部分が変更箇所
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
import java.io.Serializable;
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
public class Mutter implements Serializable{
|
518
|
-
|
519
|
-
/**
|
520
|
-
|
521
|
-
*
|
522
|
-
|
523
|
-
*/
|
524
|
-
|
525
|
-
private static final long serialVersionUID = 1L;
|
526
|
-
|
527
|
-
private String userId; //user内容
|
528
|
-
|
529
|
-
private String text; //つぶやき内容
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
public Mutter(){}
|
534
|
-
|
535
|
-
public Mutter(String userId,String text){
|
536
|
-
|
537
|
-
this.userId=userId;
|
538
|
-
|
539
|
-
this.text=text;
|
540
|
-
|
541
|
-
}
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
public String getUserId(){return userId;}
|
548
|
-
|
549
|
-
public String getText(){return text;}
|
550
|
-
|
551
|
-
//Account.javaとMutter.java同じになった
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
}
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
⚫️newRegistration.Servlet.java
|
562
|
-
|
563
|
-
package servlet;
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
import java.io.IOException;
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
import javax.servlet.RequestDispatcher;
|
572
|
-
|
573
|
-
import javax.servlet.ServletException;
|
574
|
-
|
575
|
-
import javax.servlet.annotation.WebServlet;
|
576
|
-
|
577
|
-
import javax.servlet.http.HttpServlet;
|
578
|
-
|
579
|
-
import javax.servlet.http.HttpServletRequest;
|
580
|
-
|
581
|
-
import javax.servlet.http.HttpServletResponse;
|
582
|
-
|
583
|
-
import javax.servlet.http.HttpSession;
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
import model.Account;
|
588
|
-
|
589
|
-
import model.Login;
|
590
|
-
|
591
|
-
import model.User;
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
@WebServlet("/newRegistrationServlet")
|
596
|
-
|
597
|
-
public class newRegistrationServlet extends HttpServlet {
|
598
|
-
|
599
|
-
private static final long serialVersionUID = 1L;
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
604
|
-
|
605
|
-
//フォワード
|
606
|
-
|
607
|
-
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/newRegistration.jsp");
|
608
|
-
|
609
|
-
dispatcher.forward(request, response);
|
610
|
-
|
611
|
-
}
|
612
|
-
|
613
|
-
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
//リクエストパラメータの取得
|
618
|
-
|
619
|
-
request.setCharacterEncoding("UTF-8");
|
620
|
-
|
621
|
-
String userId=request.getParameter("userId");
|
622
|
-
|
623
|
-
String pass=request.getParameter("pass");
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
//Userインスタンス(ユーザー情報)の生成
|
628
|
-
|
629
|
-
Login login=new Login(userId,pass);
|
630
|
-
|
631
|
-
//新しいメソッドを作る
|
632
|
-
|
633
|
-
//アカウントは、乱数で強制的にこっちからしてして、パスワードだけの登録にさせる。乱数を与えるときに保存して、パスワードとセットでデータベースに保存しておく
|
634
|
-
|
635
|
-
//ユーザー情報をセッションスコープに保存
|
636
|
-
|
637
|
-
HttpSession session=request.getSession();
|
638
|
-
|
639
|
-
//sessionスコープに保存して、今後のようこそ〜さんという時に使う
|
640
|
-
|
641
|
-
session.setAttribute("userId",userId);
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
//ログイン結果画面にフォワード
|
646
|
-
|
647
|
-
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/login.jsp");
|
648
|
-
|
649
|
-
dispatcher.forward(request, response);
|
650
|
-
|
651
|
-
}
|
652
|
-
|
653
|
-
}
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
⚫️LoginLogic
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
package model;
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
import dao.AccountDAO;
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
public class LoginLogic{
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
//executメソッド
|
680
|
-
|
681
|
-
public boolean execute(Login login){
|
682
|
-
|
683
|
-
AccountDAO dao=new AccountDAO();
|
684
|
-
|
685
|
-
Account account=dao.findByLogin(login);
|
686
|
-
|
687
|
-
//public Account findByLogin(Login login){となっているが引数は一つでいいの?これって、引数を一つにするやつだっけ?
|
688
|
-
|
689
|
-
return account !=null;
|
690
|
-
|
691
|
-
//目的は、taichiのGetMutterListLogicと同じ、つまり、insertするために、account=get.UserIdとかとするためであって、それがGetMutterListLogicでは、mutterListに追加するだけである。
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
}
|
696
|
-
|
697
|
-
}
|
698
|
-
|
699
609
|
|
700
610
|
|
701
611
|
関連するコードをのせました。量が膨大でみずらく申し訳ありません。
|
6
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -166,7 +166,7 @@
|
|
166
166
|
|
167
167
|
|
168
168
|
|
169
|
-
米(excute)
|
169
|
+
米(excute) account=LoginLogic.execute();
|
170
170
|
|
171
171
|
request.setAttribute("Account",account);
|
172
172
|
|
5
題名を修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
javaのservletでsnsの機能を初めて
|
1
|
+
javaのservletでsnsの機能を、初めて本を写すのではなく自分の力で作ってみたのですが、大量のコンパイルエラーが出てしまいました。
|
test
CHANGED
File without changes
|
4
一つ目の米のloginlogicをloginLogicに変更して、その影響も考慮
test
CHANGED
File without changes
|
test
CHANGED
@@ -76,9 +76,9 @@
|
|
76
76
|
|
77
77
|
//つぶやきリスト取得して、リクエストスコープに保存
|
78
78
|
|
79
|
-
LoginLogic login
|
79
|
+
LoginLogic loginLogic=new LoginLogic();
|
80
|
-
|
80
|
+
|
81
|
-
米(login
|
81
|
+
米(login) Account account = loginLogic.execute(new login);
|
82
82
|
|
83
83
|
request.setAttribute("Account", account);
|
84
84
|
|
3
修正しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -78,7 +78,7 @@
|
|
78
78
|
|
79
79
|
LoginLogic loginlogic=new LoginLogic();
|
80
80
|
|
81
|
-
米 Account account = loginLogic.execute(new login);
|
81
|
+
米(loginLogic) Account account = loginLogic.execute(new login);
|
82
82
|
|
83
83
|
request.setAttribute("Account", account);
|
84
84
|
|
@@ -202,7 +202,7 @@
|
|
202
202
|
|
203
203
|
//エラーメッセージをれクエストスコープに保存
|
204
204
|
|
205
|
-
//じゃなくてh2database使ってんだから、エラーメッセージをinsert?creat?
|
205
|
+
//じゃなくてh2database使ってんだから、エラーメッセージをinsert?create? エラーメッセージはsessio?
|
206
206
|
|
207
207
|
//request.setAttribute("errorMsg", "つぶやきが入力されていません");
|
208
208
|
|
2
新しいMain\.javaに変更しました。どうぞよろしくお願いします。
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,7 +20,7 @@
|
|
20
20
|
|
21
21
|
⚫️Main.java
|
22
22
|
|
23
|
-
package servlet;
|
23
|
+
package servlet; //色のついた部分が変更箇所
|
24
24
|
|
25
25
|
|
26
26
|
|
@@ -78,7 +78,7 @@
|
|
78
78
|
|
79
79
|
LoginLogic loginlogic=new LoginLogic();
|
80
80
|
|
81
|
-
米
|
81
|
+
米 Account account = loginLogic.execute(new login);
|
82
82
|
|
83
83
|
request.setAttribute("Account", account);
|
84
84
|
|
@@ -90,7 +90,7 @@
|
|
90
90
|
|
91
91
|
//セッションスコープからUser情報を取得
|
92
92
|
|
93
|
-
|
93
|
+
HttpSession session=request.getSession();
|
94
94
|
|
95
95
|
User userId=(User)session.getAttribute("userId");
|
96
96
|
|
@@ -100,147 +100,487 @@
|
|
100
100
|
|
101
101
|
//リダイレクト
|
102
102
|
|
103
|
-
|
103
|
+
response.sendRedirect("/newsns/");
|
104
104
|
|
105
105
|
}else{//ログイン済み
|
106
106
|
|
107
107
|
//フォワード
|
108
108
|
|
109
|
-
|
109
|
+
RequestDispatcher dispatcher =request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
|
110
|
-
|
110
|
+
|
111
|
-
|
111
|
+
dispatcher.forward(request, response);
|
112
112
|
|
113
113
|
}
|
114
114
|
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
//リクエストパラメータの取得
|
122
|
+
|
123
|
+
request.setCharacterEncoding("UTF-8");
|
124
|
+
|
125
|
+
String text =request.getParameter("text");
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
//入力値チェック
|
130
|
+
|
131
|
+
if(text !=null && text.length()!=0){
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
//セッションスコープに保存されたUser情報を取得
|
136
|
+
|
137
|
+
米(session) HttpSession session=request.getSession();
|
138
|
+
|
139
|
+
米(account) Account account=(Account)session.getAttribute("userId");
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
//つぶやきをつぶやきリストに追加(コードを開いてくとcreateでh2databaseに保存している。
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
//つぶやきをつぶやきリストに追加
|
150
|
+
|
151
|
+
Mutter mutter =new Mutter(account.getUserId(),text);
|
152
|
+
|
153
|
+
米(excute) LoginLogic.execute(account);
|
154
|
+
|
155
|
+
//この中(executeメソッドの中にinsertが入っている。)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
//つぶやきリストを取得して、リクエストスコープに保存
|
160
|
+
|
161
|
+
LoginLogic loginLogic =new LoginLogic(); ////
|
162
|
+
|
163
|
+
//とりあえずAccount.javaとMutter.java全く引数同じになっちゃってる
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
米(excute) Account=LoginLogic.execute();
|
170
|
+
|
171
|
+
request.setAttribute("Account",account);
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
米(mutter,getUserId) Mutter mutter=new Mutter(account.getUserId)
|
178
|
+
|
179
|
+
米(loginLogic) logicLogic.execute(account);
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
//つぶやきリストを取得して、リクエストスコープに保存
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
米(account,execute) Account account=LoginLogic.execute();
|
190
|
+
|
191
|
+
request.setAttribute("Account",account);
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
}else{
|
196
|
+
|
197
|
+
//エラーメッセージをれクエストスコープに保存
|
198
|
+
|
199
|
+
request.setAttribute("errorMsg", "つぶやきが入力されていません");
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
//エラーメッセージをれクエストスコープに保存
|
204
|
+
|
205
|
+
//じゃなくてh2database使ってんだから、エラーメッセージをinsert?creat? エラーメッセージはsessionでいいってた
|
206
|
+
|
207
|
+
//request.setAttribute("errorMsg", "つぶやきが入力されていません");
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
//フォワード
|
214
|
+
|
215
|
+
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/main.jsp");
|
216
|
+
|
217
|
+
dispatcher.forward(request, response);
|
218
|
+
|
219
|
+
|
220
|
+
|
115
221
|
}
|
116
222
|
|
117
|
-
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
|
228
|
+
|
118
|
-
|
229
|
+
⚫️Account.DAO
|
230
|
+
|
119
|
-
|
231
|
+
package dao;
|
232
|
+
|
233
|
+
|
234
|
+
|
120
|
-
|
235
|
+
import java.sql.Connection;
|
236
|
+
|
237
|
+
import java.sql.DriverManager;
|
238
|
+
|
239
|
+
import java.sql.PreparedStatement;
|
240
|
+
|
241
|
+
import java.sql.ResultSet;
|
242
|
+
|
243
|
+
import java.sql.SQLException;
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
import model.Account;
|
248
|
+
|
249
|
+
import model.Login;
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
public class AccountDAO {
|
254
|
+
|
255
|
+
@SuppressWarnings("finally")
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
//findByLoginメソッド
|
260
|
+
|
261
|
+
public Account findByLogin(Login login){
|
262
|
+
|
263
|
+
Connection conn=null;
|
264
|
+
|
265
|
+
Account account=null;
|
266
|
+
|
267
|
+
try{
|
268
|
+
|
121
|
-
|
269
|
+
//JDBCドライバを読み込み
|
122
|
-
|
270
|
+
|
123
|
-
|
271
|
+
Class.forName("org.h2.Driver");
|
124
|
-
|
125
|
-
米 String text =request.getParameter("text");
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
//入力値チェック
|
130
|
-
|
131
|
-
if(text !=null && text.length()!=0){
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
//セッションスコープに保存されたUser情報を取得
|
136
|
-
|
137
|
-
米 HttpSession session=request.getSession();
|
138
|
-
|
139
|
-
Account account=(Account)session.getAttribute("userId");
|
140
272
|
|
141
273
|
|
142
274
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
i
|
275
|
+
//データベースに接続
|
276
|
+
|
277
|
+
conn=DriverManager.getConnection("jdbc:h2:file:/Users/taichi/Documents/networkspace/newsns/h2data.dat","sa","");
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
//SELECT文を準備
|
282
|
+
|
283
|
+
String sql="SELECT USER_ID,,TEXT FROM ACCOUNT WHERE USER_ID=? ";
|
284
|
+
|
285
|
+
//自動配信のIDだからかぶることはない、だから、IDだけで大丈夫
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
//ちょっと複雑
|
290
|
+
|
291
|
+
PreparedStatement pStmt=conn.prepareStatement(sql);
|
292
|
+
|
293
|
+
pStmt.setString(1,login.getUserId());
|
294
|
+
|
295
|
+
//SELECT文を実行し、結果表を取得
|
296
|
+
|
297
|
+
ResultSet rs=pStmt.executeQuery();
|
298
|
+
|
299
|
+
//ここで止まってる。
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
//一致したユーザーが存在した場合
|
304
|
+
|
305
|
+
//そのユーザーを表すAccountインスタンスを生成
|
306
|
+
|
307
|
+
if(rs.next()){
|
308
|
+
|
309
|
+
//結果表からデータを取得
|
310
|
+
|
311
|
+
String userId=rs.getString("USER_ID");
|
312
|
+
|
313
|
+
String text=rs.getString("TEXT");
|
314
|
+
|
315
|
+
//passとnameは入れない(selectにも入れてない)
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
//これで作る、これはメソッドAccountでaccountを作る
|
322
|
+
|
323
|
+
account=new Account(userId,text);
|
324
|
+
|
325
|
+
//作ってexecuteで使用している。
|
326
|
+
|
327
|
+
}
|
328
|
+
|
329
|
+
}catch(SQLException e){
|
330
|
+
|
331
|
+
e.printStackTrace();
|
332
|
+
|
333
|
+
}catch(ClassNotFoundException e){
|
334
|
+
|
335
|
+
e.printStackTrace();
|
336
|
+
|
337
|
+
return null;
|
338
|
+
|
339
|
+
}finally{
|
340
|
+
|
341
|
+
//データベースを切断
|
342
|
+
|
343
|
+
if(conn!=null){
|
344
|
+
|
345
|
+
try{
|
346
|
+
|
347
|
+
conn.close();
|
348
|
+
|
349
|
+
}catch(SQLException e){
|
350
|
+
|
351
|
+
e.printStackTrace();
|
352
|
+
|
353
|
+
return null;
|
354
|
+
|
355
|
+
}
|
356
|
+
|
357
|
+
}
|
358
|
+
|
359
|
+
//見つかったユーザーまたは、nullを返す。
|
360
|
+
|
361
|
+
return account;
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
//creteメソッド
|
374
|
+
|
375
|
+
public boolean create(Account account){
|
376
|
+
|
377
|
+
Connection conn=null;
|
378
|
+
|
379
|
+
try{
|
380
|
+
|
381
|
+
//データベースへ接続
|
382
|
+
|
383
|
+
conn=DriverManager.getConnection("jdbc:h2:file:/Users/taichi/Documents/networkspace/newsns/h2data.dat","sa","");
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
//insert文の準備(idは自動連番なので、指定しなくてもいい)
|
388
|
+
|
389
|
+
String sql="INSERT INTO ACCOUNT (USER_ID,TEXT)VALUES(?,?,?)";
|
390
|
+
|
391
|
+
PreparedStatement pStmt=conn.prepareStatement(sql);
|
392
|
+
|
393
|
+
//insert文中の「?」に使用する値を指定し、SQL文を完成
|
394
|
+
|
395
|
+
//さっき作ったaccountをここで、使うinsertのときにgetUserIdとかしたやつを使う
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
pStmt.setString(1, account.getUserId());
|
400
|
+
|
401
|
+
pStmt.setString(2, account.getText());
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
//insert文を実行
|
408
|
+
|
409
|
+
int result=pStmt.executeUpdate();
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
if(result !=1){
|
414
|
+
|
415
|
+
return false;
|
416
|
+
|
417
|
+
}
|
418
|
+
|
419
|
+
}catch(SQLException e){
|
420
|
+
|
421
|
+
e.printStackTrace();
|
422
|
+
|
423
|
+
return false;
|
424
|
+
|
425
|
+
}finally{
|
426
|
+
|
427
|
+
//データベースを切断
|
428
|
+
|
429
|
+
if(conn!=null){
|
430
|
+
|
431
|
+
try{
|
432
|
+
|
433
|
+
conn.close();
|
434
|
+
|
435
|
+
}catch(SQLException e){
|
436
|
+
|
437
|
+
e.printStackTrace();
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
}
|
442
|
+
|
443
|
+
}
|
444
|
+
|
445
|
+
return true;
|
446
|
+
|
447
|
+
}
|
448
|
+
|
449
|
+
}
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
⚫️Account.java
|
454
|
+
|
455
|
+
package model;
|
456
|
+
|
457
|
+
//Accountは3つuserId,pass,mail
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
public class Account {
|
462
|
+
|
463
|
+
//フィールド
|
464
|
+
|
465
|
+
private String userId;
|
466
|
+
|
467
|
+
private String text;
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
//Accountメソッド引数全て、
|
474
|
+
|
475
|
+
public Account(String userId,String text){
|
476
|
+
|
477
|
+
this.userId=userId;
|
478
|
+
|
479
|
+
this.text=text;
|
480
|
+
|
481
|
+
|
482
|
+
|
483
|
+
}
|
484
|
+
|
485
|
+
//UserId.Passとか
|
486
|
+
|
487
|
+
public String getUserId(){return userId;}
|
488
|
+
|
489
|
+
public String getText(){return text;}
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
//getIdとtextだけにした。すっきりしたけどいいのかな?しかも、これって、Mutterと丸かぶりだよね?これでもいいのかな?MutterDAOを見てみよう
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
|
500
|
+
|
501
|
+
}
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
⚫️Mutter.java
|
508
|
+
|
509
|
+
package model; //色のついた部分が変更箇所
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
import java.io.Serializable;
|
514
|
+
|
515
|
+
|
516
|
+
|
517
|
+
public class Mutter implements Serializable{
|
518
|
+
|
519
|
+
/**
|
520
|
+
|
521
|
+
*
|
522
|
+
|
523
|
+
*/
|
524
|
+
|
525
|
+
private static final long serialVersionUID = 1L;
|
526
|
+
|
527
|
+
private String userId; //user内容
|
528
|
+
|
529
|
+
private String text; //つぶやき内容
|
530
|
+
|
531
|
+
|
532
|
+
|
533
|
+
public Mutter(){}
|
534
|
+
|
535
|
+
public Mutter(String userId,String text){
|
536
|
+
|
537
|
+
this.userId=userId;
|
538
|
+
|
539
|
+
this.text=text;
|
540
|
+
|
541
|
+
}
|
542
|
+
|
543
|
+
|
544
|
+
|
545
|
+
|
546
|
+
|
547
|
+
public String getUserId(){return userId;}
|
548
|
+
|
549
|
+
public String getText(){return text;}
|
550
|
+
|
551
|
+
//Account.javaとMutter.java同じになった
|
552
|
+
|
553
|
+
|
554
|
+
|
555
|
+
}
|
556
|
+
|
557
|
+
|
558
|
+
|
559
|
+
|
560
|
+
|
561
|
+
⚫️newRegistration.Servlet.java
|
562
|
+
|
563
|
+
package servlet;
|
564
|
+
|
565
|
+
|
566
|
+
|
567
|
+
import java.io.IOException;
|
568
|
+
|
569
|
+
|
570
|
+
|
571
|
+
import javax.servlet.RequestDispatcher;
|
572
|
+
|
573
|
+
import javax.servlet.ServletException;
|
574
|
+
|
575
|
+
import javax.servlet.annotation.WebServlet;
|
576
|
+
|
577
|
+
import javax.servlet.http.HttpServlet;
|
578
|
+
|
579
|
+
import javax.servlet.http.HttpServletRequest;
|
580
|
+
|
581
|
+
import javax.servlet.http.HttpServletResponse;
|
582
|
+
|
583
|
+
import javax.servlet.http.HttpSession;
|
244
584
|
|
245
585
|
|
246
586
|
|
@@ -248,454 +588,114 @@
|
|
248
588
|
|
249
589
|
import model.Login;
|
250
590
|
|
251
|
-
|
591
|
+
import model.User;
|
592
|
+
|
593
|
+
|
594
|
+
|
252
|
-
|
595
|
+
@WebServlet("/newRegistrationServlet")
|
596
|
+
|
597
|
+
public class newRegistrationServlet extends HttpServlet {
|
598
|
+
|
599
|
+
private static final long serialVersionUID = 1L;
|
600
|
+
|
601
|
+
|
602
|
+
|
603
|
+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
604
|
+
|
605
|
+
//フォワード
|
606
|
+
|
607
|
+
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/newRegistration.jsp");
|
608
|
+
|
609
|
+
dispatcher.forward(request, response);
|
610
|
+
|
611
|
+
}
|
612
|
+
|
613
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
614
|
+
|
615
|
+
|
616
|
+
|
617
|
+
//リクエストパラメータの取得
|
618
|
+
|
619
|
+
request.setCharacterEncoding("UTF-8");
|
620
|
+
|
621
|
+
String userId=request.getParameter("userId");
|
622
|
+
|
623
|
+
String pass=request.getParameter("pass");
|
624
|
+
|
625
|
+
|
626
|
+
|
627
|
+
//Userインスタンス(ユーザー情報)の生成
|
628
|
+
|
629
|
+
Login login=new Login(userId,pass);
|
630
|
+
|
631
|
+
//新しいメソッドを作る
|
632
|
+
|
633
|
+
//アカウントは、乱数で強制的にこっちからしてして、パスワードだけの登録にさせる。乱数を与えるときに保存して、パスワードとセットでデータベースに保存しておく
|
634
|
+
|
635
|
+
//ユーザー情報をセッションスコープに保存
|
636
|
+
|
637
|
+
HttpSession session=request.getSession();
|
638
|
+
|
639
|
+
//sessionスコープに保存して、今後のようこそ〜さんという時に使う
|
640
|
+
|
641
|
+
session.setAttribute("userId",userId);
|
642
|
+
|
643
|
+
|
644
|
+
|
645
|
+
//ログイン結果画面にフォワード
|
646
|
+
|
647
|
+
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/login.jsp");
|
648
|
+
|
649
|
+
dispatcher.forward(request, response);
|
650
|
+
|
651
|
+
}
|
652
|
+
|
653
|
+
}
|
654
|
+
|
655
|
+
|
656
|
+
|
657
|
+
|
658
|
+
|
659
|
+
⚫️LoginLogic
|
660
|
+
|
661
|
+
|
662
|
+
|
663
|
+
package model;
|
664
|
+
|
665
|
+
|
666
|
+
|
667
|
+
|
668
|
+
|
669
|
+
import dao.AccountDAO;
|
670
|
+
|
671
|
+
|
672
|
+
|
673
|
+
|
674
|
+
|
253
|
-
public class
|
675
|
+
public class LoginLogic{
|
254
|
-
|
255
|
-
|
676
|
+
|
256
|
-
|
257
|
-
|
258
|
-
|
677
|
+
|
678
|
+
|
259
|
-
//
|
679
|
+
//executメソッド
|
260
|
-
|
680
|
+
|
261
|
-
public
|
681
|
+
public boolean execute(Login login){
|
262
|
-
|
682
|
+
|
263
|
-
|
683
|
+
AccountDAO dao=new AccountDAO();
|
684
|
+
|
264
|
-
|
685
|
+
Account account=dao.findByLogin(login);
|
686
|
+
|
687
|
+
//public Account findByLogin(Login login){となっているが引数は一つでいいの?これって、引数を一つにするやつだっけ?
|
688
|
+
|
265
|
-
|
689
|
+
return account !=null;
|
266
|
-
|
267
|
-
|
690
|
+
|
268
|
-
|
269
|
-
//JDBCドライバを読み込み
|
270
|
-
|
271
|
-
|
691
|
+
//目的は、taichiのGetMutterListLogicと同じ、つまり、insertするために、account=get.UserIdとかとするためであって、それがGetMutterListLogicでは、mutterListに追加するだけである。
|
272
692
|
|
273
693
|
|
274
694
|
|
275
|
-
//データベースに接続
|
276
|
-
|
277
|
-
conn=DriverManager.getConnection("jdbc:h2:file:/Users/taichi/Documents/networkspace/newsns/h2data.dat","sa","");
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
//SELECT文を準備
|
282
|
-
|
283
|
-
String sql="SELECT USER_ID,,TEXT FROM ACCOUNT WHERE USER_ID=? ";
|
284
|
-
|
285
|
-
//自動配信のIDだからかぶることはない、だから、IDだけで大丈夫
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
//ちょっと複雑
|
290
|
-
|
291
|
-
PreparedStatement pStmt=conn.prepareStatement(sql);
|
292
|
-
|
293
|
-
pStmt.setString(1,login.getUserId());
|
294
|
-
|
295
|
-
//SELECT文を実行し、結果表を取得
|
296
|
-
|
297
|
-
ResultSet rs=pStmt.executeQuery();
|
298
|
-
|
299
|
-
//ここで止まってる。
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
//一致したユーザーが存在した場合
|
304
|
-
|
305
|
-
//そのユーザーを表すAccountインスタンスを生成
|
306
|
-
|
307
|
-
if(rs.next()){
|
308
|
-
|
309
|
-
//結果表からデータを取得
|
310
|
-
|
311
|
-
String userId=rs.getString("USER_ID");
|
312
|
-
|
313
|
-
String text=rs.getString("TEXT");
|
314
|
-
|
315
|
-
//passとnameは入れない(selectにも入れてない)
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
//これで作る、これはメソッドAccountでaccountを作る
|
322
|
-
|
323
|
-
account=new Account(userId,text);
|
324
|
-
|
325
|
-
//作ってexecuteで使用している。
|
326
|
-
|
327
|
-
}
|
328
|
-
|
329
|
-
}catch(SQLException e){
|
330
|
-
|
331
|
-
e.printStackTrace();
|
332
|
-
|
333
|
-
}catch(ClassNotFoundException e){
|
334
|
-
|
335
|
-
e.printStackTrace();
|
336
|
-
|
337
|
-
return null;
|
338
|
-
|
339
|
-
}finally{
|
340
|
-
|
341
|
-
//データベースを切断
|
342
|
-
|
343
|
-
if(conn!=null){
|
344
|
-
|
345
|
-
try{
|
346
|
-
|
347
|
-
conn.close();
|
348
|
-
|
349
|
-
}catch(SQLException e){
|
350
|
-
|
351
|
-
e.printStackTrace();
|
352
|
-
|
353
|
-
return null;
|
354
|
-
|
355
|
-
}
|
356
|
-
|
357
|
-
}
|
358
|
-
|
359
|
-
//見つかったユーザーまたは、nullを返す。
|
360
|
-
|
361
|
-
return account;
|
362
|
-
|
363
695
|
}
|
364
696
|
|
365
697
|
}
|
366
698
|
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
//creteメソッド
|
374
|
-
|
375
|
-
public boolean create(Account account){
|
376
|
-
|
377
|
-
Connection conn=null;
|
378
|
-
|
379
|
-
try{
|
380
|
-
|
381
|
-
//データベースへ接続
|
382
|
-
|
383
|
-
conn=DriverManager.getConnection("jdbc:h2:file:/Users/taichi/Documents/networkspace/newsns/h2data.dat","sa","");
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
//insert文の準備(idは自動連番なので、指定しなくてもいい)
|
388
|
-
|
389
|
-
String sql="INSERT INTO ACCOUNT (USER_ID,TEXT)VALUES(?,?,?)";
|
390
|
-
|
391
|
-
PreparedStatement pStmt=conn.prepareStatement(sql);
|
392
|
-
|
393
|
-
//insert文中の「?」に使用する値を指定し、SQL文を完成
|
394
|
-
|
395
|
-
//さっき作ったaccountをここで、使うinsertのときにgetUserIdとかしたやつを使う
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
pStmt.setString(1, account.getUserId());
|
400
|
-
|
401
|
-
pStmt.setString(2, account.getText());
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
//insert文を実行
|
408
|
-
|
409
|
-
int result=pStmt.executeUpdate();
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
if(result !=1){
|
414
|
-
|
415
|
-
return false;
|
416
|
-
|
417
|
-
}
|
418
|
-
|
419
|
-
}catch(SQLException e){
|
420
|
-
|
421
|
-
e.printStackTrace();
|
422
|
-
|
423
|
-
return false;
|
424
|
-
|
425
|
-
}finally{
|
426
|
-
|
427
|
-
//データベースを切断
|
428
|
-
|
429
|
-
if(conn!=null){
|
430
|
-
|
431
|
-
try{
|
432
|
-
|
433
|
-
conn.close();
|
434
|
-
|
435
|
-
}catch(SQLException e){
|
436
|
-
|
437
|
-
e.printStackTrace();
|
438
|
-
|
439
|
-
}
|
440
|
-
|
441
|
-
}
|
442
|
-
|
443
|
-
}
|
444
|
-
|
445
|
-
return true;
|
446
|
-
|
447
|
-
}
|
448
|
-
|
449
|
-
}
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
⚫️Account.java
|
454
|
-
|
455
|
-
package model;
|
456
|
-
|
457
|
-
//Accountは3つuserId,pass,mail
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
public class Account {
|
462
|
-
|
463
|
-
//フィールド
|
464
|
-
|
465
|
-
private String userId;
|
466
|
-
|
467
|
-
private String text;
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
//Accountメソッド引数全て、
|
474
|
-
|
475
|
-
public Account(String userId,String text){
|
476
|
-
|
477
|
-
this.userId=userId;
|
478
|
-
|
479
|
-
this.text=text;
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
}
|
484
|
-
|
485
|
-
//UserId.Passとか
|
486
|
-
|
487
|
-
public String getUserId(){return userId;}
|
488
|
-
|
489
|
-
public String getText(){return text;}
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
//getIdとtextだけにした。すっきりしたけどいいのかな?しかも、これって、Mutterと丸かぶりだよね?これでもいいのかな?MutterDAOを見てみよう
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
}
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
⚫️Mutter.java
|
508
|
-
|
509
|
-
package model; //色のついた部分が変更箇所
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
import java.io.Serializable;
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
public class Mutter implements Serializable{
|
518
|
-
|
519
|
-
/**
|
520
|
-
|
521
|
-
*
|
522
|
-
|
523
|
-
*/
|
524
|
-
|
525
|
-
private static final long serialVersionUID = 1L;
|
526
|
-
|
527
|
-
private String userId; //user内容
|
528
|
-
|
529
|
-
private String text; //つぶやき内容
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
public Mutter(){}
|
534
|
-
|
535
|
-
public Mutter(String userId,String text){
|
536
|
-
|
537
|
-
this.userId=userId;
|
538
|
-
|
539
|
-
this.text=text;
|
540
|
-
|
541
|
-
}
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
public String getUserId(){return userId;}
|
548
|
-
|
549
|
-
public String getText(){return text;}
|
550
|
-
|
551
|
-
//Account.javaとMutter.java同じになった
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
}
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
⚫️newRegistration.Servlet.java
|
562
|
-
|
563
|
-
package servlet;
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
import java.io.IOException;
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
import javax.servlet.RequestDispatcher;
|
572
|
-
|
573
|
-
import javax.servlet.ServletException;
|
574
|
-
|
575
|
-
import javax.servlet.annotation.WebServlet;
|
576
|
-
|
577
|
-
import javax.servlet.http.HttpServlet;
|
578
|
-
|
579
|
-
import javax.servlet.http.HttpServletRequest;
|
580
|
-
|
581
|
-
import javax.servlet.http.HttpServletResponse;
|
582
|
-
|
583
|
-
import javax.servlet.http.HttpSession;
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
import model.Account;
|
588
|
-
|
589
|
-
import model.Login;
|
590
|
-
|
591
|
-
import model.User;
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
@WebServlet("/newRegistrationServlet")
|
596
|
-
|
597
|
-
public class newRegistrationServlet extends HttpServlet {
|
598
|
-
|
599
|
-
private static final long serialVersionUID = 1L;
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
604
|
-
|
605
|
-
//フォワード
|
606
|
-
|
607
|
-
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/newRegistration.jsp");
|
608
|
-
|
609
|
-
dispatcher.forward(request, response);
|
610
|
-
|
611
|
-
}
|
612
|
-
|
613
|
-
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
//リクエストパラメータの取得
|
618
|
-
|
619
|
-
request.setCharacterEncoding("UTF-8");
|
620
|
-
|
621
|
-
String userId=request.getParameter("userId");
|
622
|
-
|
623
|
-
String pass=request.getParameter("pass");
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
//Userインスタンス(ユーザー情報)の生成
|
628
|
-
|
629
|
-
Login login=new Login(userId,pass);
|
630
|
-
|
631
|
-
//新しいメソッドを作る
|
632
|
-
|
633
|
-
//アカウントは、乱数で強制的にこっちからしてして、パスワードだけの登録にさせる。乱数を与えるときに保存して、パスワードとセットでデータベースに保存しておく
|
634
|
-
|
635
|
-
//ユーザー情報をセッションスコープに保存
|
636
|
-
|
637
|
-
HttpSession session=request.getSession();
|
638
|
-
|
639
|
-
//sessionスコープに保存して、今後のようこそ〜さんという時に使う
|
640
|
-
|
641
|
-
session.setAttribute("userId",userId);
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
//ログイン結果画面にフォワード
|
646
|
-
|
647
|
-
RequestDispatcher dispatcher=request.getRequestDispatcher("/WEB-INF/jsp/login.jsp");
|
648
|
-
|
649
|
-
dispatcher.forward(request, response);
|
650
|
-
|
651
|
-
}
|
652
|
-
|
653
|
-
}
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
⚫️LoginLogic
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
package model;
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
import dao.AccountDAO;
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
public class LoginLogic{
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
//executメソッド
|
680
|
-
|
681
|
-
public boolean execute(Login login){
|
682
|
-
|
683
|
-
AccountDAO dao=new AccountDAO();
|
684
|
-
|
685
|
-
Account account=dao.findByLogin(login);
|
686
|
-
|
687
|
-
//public Account findByLogin(Login login){となっているが引数は一つでいいの?これって、引数を一つにするやつだっけ?
|
688
|
-
|
689
|
-
return account !=null;
|
690
|
-
|
691
|
-
//目的は、taichiのGetMutterListLogicと同じ、つまり、insertするために、account=get.UserIdとかとするためであって、それがGetMutterListLogicでは、mutterListに追加するだけである。
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
}
|
696
|
-
|
697
|
-
}
|
698
|
-
|
699
699
|
|
700
700
|
|
701
701
|
関連するコードをのせました。量が膨大でみずらく申し訳ありません。
|
1
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,7 +20,7 @@
|
|
20
20
|
|
21
21
|
⚫️Main.java
|
22
22
|
|
23
|
-
package servlet;
|
23
|
+
package servlet;
|
24
24
|
|
25
25
|
|
26
26
|
|