質問編集履歴
4
エラー文を追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -245,3 +245,41 @@
|
|
245
245
|
|
246
246
|
|
247
247
|
```
|
248
|
+
|
249
|
+
エラー
|
250
|
+
|
251
|
+
```ここに言語を入力
|
252
|
+
|
253
|
+
HTTPステータス 500 -
|
254
|
+
|
255
|
+
type 例外レポート
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
メッセージ
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
説明 The server encountered an internal error that prevented it from fulfilling this request.
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
例外
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
java.lang.NullPointerException
|
272
|
+
|
273
|
+
controller.Controller.doPost(Controller.java:43)
|
274
|
+
|
275
|
+
controller.Controller.doGet(Controller.java:26)
|
276
|
+
|
277
|
+
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
|
278
|
+
|
279
|
+
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
|
280
|
+
|
281
|
+
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
|
282
|
+
|
283
|
+
```
|
284
|
+
|
285
|
+
43行目はswitchのbuttonIdです。
|
3
actionクラスの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -191,3 +191,57 @@
|
|
191
191
|
}
|
192
192
|
|
193
193
|
```
|
194
|
+
|
195
|
+
```Action
|
196
|
+
|
197
|
+
public class GoMainAction {
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
public String execute(HttpServletRequest req) {
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
//遷移先
|
206
|
+
|
207
|
+
String page = "/WEB-INF/jsp/main.jsp";
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
//セッション取得
|
212
|
+
|
213
|
+
HttpSession session = req.getSession(false);
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
//ログインチェック。セッションなければnullなのでエラー返す
|
218
|
+
|
219
|
+
if(session==null){
|
220
|
+
|
221
|
+
page = "/dokotsubu/top.jsp";
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
//セッションから過去のツイートを取り出し
|
228
|
+
|
229
|
+
ArrayList<String> tweetList = (ArrayList<String>)session.getAttribute("tweet");
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
//過去のツイートある場合
|
234
|
+
|
235
|
+
session.setAttribute("tweetList", tweetList);
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
return page;
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
```
|
2
サーブレットを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,19 +6,19 @@
|
|
6
6
|
|
7
7
|
隠しパラメータを使用してボタンと同じように実装してみた以下のコードでは
|
8
8
|
|
9
|
-
buttonIdがnullになっているようで、
|
9
|
+
buttonIdがnullになっているようで、リンクをクリックすると
|
10
10
|
|
11
11
|
500エラーでNullPointerExceptionが出ます。
|
12
12
|
|
13
13
|
|
14
14
|
|
15
|
-
どうすればサーブレットを呼び出すことができるか教えていただきたいです。
|
15
|
+
どうすればサーブレットを正しく呼び出すことができるか教えていただきたいです。
|
16
16
|
|
17
17
|
よろしくお願いします。
|
18
18
|
|
19
19
|
|
20
20
|
|
21
|
-
```
|
21
|
+
```JSP
|
22
22
|
|
23
23
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
24
24
|
|
@@ -64,13 +64,11 @@
|
|
64
64
|
|
65
65
|
<br>
|
66
66
|
|
67
|
-
|
67
|
+
<%--この部分 --%>
|
68
68
|
|
69
69
|
<input type="hidden" value="" name="buttonId">
|
70
70
|
|
71
71
|
<a href onclick="javascript:this.form.buttonId.value='UC0004'; this.form.submit();">つぶやきの投稿・閲覧へ</a>
|
72
|
-
|
73
|
-
//*この部分*******************************
|
74
72
|
|
75
73
|
</c:if>
|
76
74
|
|
@@ -81,3 +79,115 @@
|
|
81
79
|
</html>
|
82
80
|
|
83
81
|
```
|
82
|
+
|
83
|
+
```Servlet
|
84
|
+
|
85
|
+
@WebServlet("/doko")
|
86
|
+
|
87
|
+
public class Controller extends HttpServlet {
|
88
|
+
|
89
|
+
private static final long serialVersionUID = 1L;
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
94
|
+
|
95
|
+
throws ServletException, IOException {
|
96
|
+
|
97
|
+
doPost(request, response);
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
/**
|
104
|
+
|
105
|
+
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
106
|
+
|
107
|
+
*/
|
108
|
+
|
109
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
110
|
+
|
111
|
+
throws ServletException, IOException {
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
//受け取った値の文字コード設定
|
116
|
+
|
117
|
+
request.setCharacterEncoding("UTF-8");
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
String page = null;
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
String buttonId = request.getParameter("buttonId");
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
//ボタンIDで遷移先の振り分け
|
130
|
+
|
131
|
+
switch (buttonId) {
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
case "UC0001":
|
136
|
+
|
137
|
+
LoginAction loginAction = new LoginAction();
|
138
|
+
|
139
|
+
page = loginAction.execute(request);
|
140
|
+
|
141
|
+
break;
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
case "UC0002":
|
146
|
+
|
147
|
+
TweetAction tweetAction = new TweetAction();
|
148
|
+
|
149
|
+
page = tweetAction.execute(request);
|
150
|
+
|
151
|
+
break;
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
case "UC0003":
|
156
|
+
|
157
|
+
LogoutAction logoutAction = new LogoutAction();
|
158
|
+
|
159
|
+
page = logoutAction.execute(request);
|
160
|
+
|
161
|
+
break;
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
case "UC0004":
|
166
|
+
|
167
|
+
GoMainAction goMainAction = new GoMainAction();
|
168
|
+
|
169
|
+
page = goMainAction.execute(request);
|
170
|
+
|
171
|
+
break;
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
//フォワード
|
180
|
+
|
181
|
+
RequestDispatcher dispacher = request.getRequestDispatcher(page);
|
182
|
+
|
183
|
+
dispacher.forward(request, response);
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
```
|
1
コードをわかりやすく
test
CHANGED
File without changes
|
test
CHANGED
@@ -64,11 +64,13 @@
|
|
64
64
|
|
65
65
|
<br>
|
66
66
|
|
67
|
-
//この部分
|
67
|
+
//*この部分*******************************
|
68
68
|
|
69
69
|
<input type="hidden" value="" name="buttonId">
|
70
70
|
|
71
71
|
<a href onclick="javascript:this.form.buttonId.value='UC0004'; this.form.submit();">つぶやきの投稿・閲覧へ</a>
|
72
|
+
|
73
|
+
//*この部分*******************************
|
72
74
|
|
73
75
|
</c:if>
|
74
76
|
|