質問編集履歴
2
サーブレットのビューの変更とスクリーンショットからコード挿入への変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -178,6 +178,8 @@
|
|
178
178
|
|
179
179
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
|
180
180
|
|
181
|
+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
182
|
+
|
181
183
|
<!DOCTYPE html>
|
182
184
|
|
183
185
|
<html lang="ja">
|
@@ -186,34 +188,42 @@
|
|
186
188
|
|
187
189
|
<meta charset="UTF-8">
|
188
190
|
|
189
|
-
<title>
|
191
|
+
<title>おみくじの結果</title>
|
190
192
|
|
191
193
|
</head>
|
192
194
|
|
193
195
|
<body>
|
194
196
|
|
197
|
+
<c:choose>
|
198
|
+
|
199
|
+
<c:when test="${errors != null}">
|
200
|
+
|
201
|
+
<h1>入力にエラーがあります。</h1>
|
202
|
+
|
203
|
+
<ul>
|
204
|
+
|
205
|
+
<c:forEach var = "error" items="${errors}">
|
206
|
+
|
207
|
+
<li><c:out value="${error}"/></li>
|
208
|
+
|
209
|
+
</c:forEach>
|
210
|
+
|
211
|
+
</ul>
|
212
|
+
|
213
|
+
</c:when>
|
214
|
+
|
215
|
+
<c:otherwise>
|
216
|
+
|
195
|
-
<h1>
|
217
|
+
<h1> おみくじの結果</h1>
|
196
|
-
|
218
|
+
|
197
|
-
<
|
219
|
+
<p><c:out value="${username}" />さんの今日の運勢は <c:out value="${omikuji_result}" /> です。</p>
|
198
|
-
|
199
|
-
|
220
|
+
|
200
|
-
|
201
|
-
<
|
221
|
+
</c:otherwise>
|
202
|
-
|
203
|
-
|
222
|
+
|
204
|
-
|
205
|
-
<br>
|
206
|
-
|
207
|
-
<br>
|
208
|
-
|
209
|
-
<button type="submit">占う!</button>
|
210
|
-
|
211
|
-
</
|
223
|
+
</c:choose>
|
212
224
|
|
213
225
|
</body>
|
214
226
|
|
215
227
|
</html>
|
216
228
|
|
217
|
-
|
218
|
-
|
219
229
|
```
|
1
画像のスクリーンショットでなく、<code>ボタンからコード入力
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,18 +16,204 @@
|
|
16
16
|
|
17
17
|
よろしくお願いします。
|
18
18
|
|
19
|
-
入力フォーム
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
19
|
+
入力フォーム(omikuji.jsp)
|
20
|
+
|
21
|
+
```java
|
22
|
+
|
23
|
+
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
|
24
|
+
|
25
|
+
<!DOCTYPE html>
|
26
|
+
|
27
|
+
<html lang="ja">
|
28
|
+
|
29
|
+
<head>
|
30
|
+
|
31
|
+
<meta charset="UTF-8">
|
32
|
+
|
33
|
+
<title>今日のおみくじ</title>
|
34
|
+
|
35
|
+
</head>
|
36
|
+
|
37
|
+
<body>
|
38
|
+
|
39
|
+
<h1>今日のおみくじ</h1>
|
40
|
+
|
41
|
+
<form method="POST" action="/first_webapp/OmikujiServlet">
|
42
|
+
|
43
|
+
<label for="username">氏名</label>
|
44
|
+
|
45
|
+
<br>
|
46
|
+
|
47
|
+
<input type="text" name="username">
|
48
|
+
|
49
|
+
<br>
|
50
|
+
|
51
|
+
<br>
|
52
|
+
|
53
|
+
<button type="submit">占う!</button>
|
54
|
+
|
55
|
+
</form>
|
56
|
+
|
57
|
+
</body>
|
58
|
+
|
59
|
+
</html>
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
サーブレット(OmikujiServlet.java)
|
68
|
+
|
69
|
+
```java
|
70
|
+
|
71
|
+
pakege test
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
import java.io.IOException;
|
76
|
+
|
77
|
+
import java.util.ArrayList;
|
78
|
+
|
79
|
+
import java.util.List;
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
import javax.servlet.RequestDispatcher;
|
84
|
+
|
85
|
+
import javax.servlet.ServletException;
|
86
|
+
|
87
|
+
import javax.servlet.annotation.WebServlet;
|
88
|
+
|
89
|
+
import javax.servlet.http.HttpServlet;
|
90
|
+
|
91
|
+
import javax.servlet.http.HttpServletRequest;
|
92
|
+
|
93
|
+
import javax.servlet.http.HttpServletResponse;
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
/**
|
98
|
+
|
99
|
+
* Servlet implementation class OmikujiServlet
|
100
|
+
|
101
|
+
*/
|
102
|
+
|
103
|
+
public class OmikujiServlet extends HttpServlet {
|
104
|
+
|
105
|
+
private static final long serialVersionUID = 1L;
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
public OmikujiServlet() {
|
110
|
+
|
111
|
+
super();
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
118
|
+
|
119
|
+
String username = request.getParameter("username");
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
String[] omikuji_results = {"大吉", "吉", "中吉", "小吉”, 末吉", "凶"};
|
124
|
+
|
125
|
+
String omikuji_result = omikuji_results[(int)(Math.random() * 6)];
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
//入力チェック
|
130
|
+
|
131
|
+
List<String> errors = new ArrayList<String>();
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
if (username == null || username.equals("")) {
|
136
|
+
|
137
|
+
errors.add("名前を入力してください。");
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
if (errors.size() > 0) {
|
144
|
+
|
145
|
+
request.setAttribute("errors", errors);
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
} else {
|
150
|
+
|
151
|
+
request.setAttribute("username", username);
|
152
|
+
|
153
|
+
request.setAttribute("omikuji_result", omikuji_result);
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
// ビューとなるJSPを指定して表示する
|
160
|
+
|
161
|
+
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/omikuji_result.jsp");
|
162
|
+
|
163
|
+
rd.forward(request, response);
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
```
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
サーブレットのビューとなるJSPファイル(omikuji_result.jsp)
|
176
|
+
|
177
|
+
```java
|
178
|
+
|
179
|
+
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
|
180
|
+
|
181
|
+
<!DOCTYPE html>
|
182
|
+
|
183
|
+
<html lang="ja">
|
184
|
+
|
185
|
+
<head>
|
186
|
+
|
187
|
+
<meta charset="UTF-8">
|
188
|
+
|
189
|
+
<title>今日のおみくじ</title>
|
190
|
+
|
191
|
+
</head>
|
192
|
+
|
193
|
+
<body>
|
194
|
+
|
195
|
+
<h1>今日のおみくじ</h1>
|
196
|
+
|
197
|
+
<form method="POST" action="/first_webapp/OmikujiServlet">
|
198
|
+
|
199
|
+
<label for="username">氏名</label>
|
200
|
+
|
201
|
+
<br>
|
202
|
+
|
203
|
+
<input type="text" name="username">
|
204
|
+
|
205
|
+
<br>
|
206
|
+
|
207
|
+
<br>
|
208
|
+
|
209
|
+
<button type="submit">占う!</button>
|
210
|
+
|
211
|
+
</form>
|
212
|
+
|
213
|
+
</body>
|
214
|
+
|
215
|
+
</html>
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
```
|