質問編集履歴
2
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,7 +8,9 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
+
|
12
|
+
|
11
|
-
|
13
|
+
package sample1;
|
12
14
|
|
13
15
|
|
14
16
|
|
@@ -230,8 +232,18 @@
|
|
230
232
|
|
231
233
|
}
|
232
234
|
|
233
|
-
|
235
|
+
|
236
|
+
|
237
|
+
|
234
238
|
|
235
239
|
コード
|
236
240
|
|
237
241
|
```
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
--------------------------------------------------------------------------
|
248
|
+
|
249
|
+
doGet メソッドのrequestとresponse の引数は何を受けっと手いるのでしょうか?
|
1
マークダウン訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,36 +2,236 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
'package sample1;
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
import java.io.IOException;
|
16
|
+
|
17
|
+
import java.io.PrintWriter;
|
18
|
+
|
19
|
+
import java.sql.Connection;
|
20
|
+
|
21
|
+
import java.sql.DriverManager;
|
22
|
+
|
23
|
+
import java.sql.ResultSet;
|
24
|
+
|
25
|
+
import java.sql.SQLException;
|
26
|
+
|
27
|
+
import java.sql.Statement;
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
import javax.servlet.ServletException;
|
32
|
+
|
33
|
+
import javax.servlet.annotation.WebServlet;
|
34
|
+
|
35
|
+
import javax.servlet.http.HttpServlet;
|
36
|
+
|
37
|
+
import javax.servlet.http.HttpServletRequest;
|
38
|
+
|
39
|
+
import javax.servlet.http.HttpServletResponse;
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
/**
|
44
|
+
|
45
|
+
*
|
46
|
+
|
47
|
+
* @author mori
|
48
|
+
|
49
|
+
*
|
50
|
+
|
51
|
+
*/
|
52
|
+
|
53
|
+
@WebServlet("/Hello")
|
54
|
+
|
55
|
+
public class Hello extends HttpServlet {
|
56
|
+
|
57
|
+
private static final long serialVersionUID = 1L;
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
/**
|
64
|
+
|
65
|
+
* doGetメソッド
|
66
|
+
|
67
|
+
* SELECT文を発行・画面に表示する
|
68
|
+
|
69
|
+
* @param request
|
70
|
+
|
71
|
+
* @param response
|
72
|
+
|
73
|
+
* @throw ServletException 処理中に問題が起こったときに、Servlet がスローする可能性のある一般的な例外を定義しています。
|
74
|
+
|
75
|
+
* @throw IOException 入出力処理の失敗、または割り込みの発生によって生成される例外
|
76
|
+
|
77
|
+
*
|
78
|
+
|
79
|
+
*/
|
80
|
+
|
81
|
+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
82
|
+
|
83
|
+
throws ServletException, IOException {
|
84
|
+
|
85
|
+
response.setContentType("text/html; charset=utf-8");
|
86
|
+
|
87
|
+
PrintWriter out = response.getWriter();
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
out.println("<html>");
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
out.println("<body>");
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
Connection conn = null;
|
100
|
+
|
101
|
+
String url = "jdbc:mysql://localhost/training";
|
102
|
+
|
103
|
+
String user = "root";
|
104
|
+
|
105
|
+
String password = "ryota";
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
try {
|
110
|
+
|
111
|
+
Class.forName("com.mysql.jdbc.Driver");
|
112
|
+
|
113
|
+
conn = DriverManager.getConnection(url, user, password);
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
Statement stmt = conn.createStatement();
|
118
|
+
|
119
|
+
String sql = "SELECT * FROM user";
|
120
|
+
|
121
|
+
ResultSet rs = stmt.executeQuery(sql);
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
out.println("<table = border=1>");
|
126
|
+
|
127
|
+
out.println("<tr>");
|
128
|
+
|
129
|
+
out.println("<th>");
|
130
|
+
|
131
|
+
out.println("</th>");
|
132
|
+
|
133
|
+
out.println("<th>");
|
134
|
+
|
135
|
+
out.println("name");
|
136
|
+
|
137
|
+
out.println("</th>");
|
138
|
+
|
139
|
+
out.println("<th>");
|
140
|
+
|
141
|
+
out.println("age");
|
142
|
+
|
143
|
+
out.println("</th>");
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
while (rs.next()) {
|
148
|
+
|
149
|
+
int id = rs.getInt("id");
|
150
|
+
|
151
|
+
String name = rs.getString("name");
|
152
|
+
|
153
|
+
int age = rs.getInt("age");
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
out.println("<tr>" + "<td>" + id + "</td>" + "<td>" + name + "</td>" + "<td>" + age + "</td>" + "<tr>");
|
158
|
+
|
159
|
+
out.println("<br>");
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
out.println("</table>");
|
166
|
+
|
167
|
+
rs.close();
|
168
|
+
|
169
|
+
stmt.close();
|
170
|
+
|
171
|
+
} catch (ClassNotFoundException e) {
|
172
|
+
|
173
|
+
out.println("lassNotFoundException:" + e.getMessage());
|
174
|
+
|
175
|
+
} catch (SQLException e) {
|
176
|
+
|
177
|
+
out.println("SQLException:" + e.getMessage());
|
178
|
+
|
179
|
+
} catch (Exception e) {
|
180
|
+
|
181
|
+
out.println("Exception:" + e.getMessage());
|
182
|
+
|
183
|
+
} finally {
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
try {
|
188
|
+
|
189
|
+
if (conn != null) {
|
190
|
+
|
191
|
+
conn.close();
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
} catch (SQLException e) {
|
196
|
+
|
197
|
+
out.println("SQLException:" + e.getMessage());
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
out.println("</body>");
|
208
|
+
|
209
|
+
out.println("</html>");
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
/**
|
216
|
+
|
217
|
+
* doPostメソッド
|
218
|
+
|
219
|
+
*
|
220
|
+
|
221
|
+
*/
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
226
|
+
|
227
|
+
throws ServletException, IOException {
|
228
|
+
|
229
|
+
doGet(request, response);
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
}'
|
234
|
+
|
5
235
|
コード
|
6
236
|
|
7
237
|
```
|
8
|
-
|
9
|
-
* doGetメソッド
|
10
|
-
|
11
|
-
* SELECT文を発行・画面に表示する
|
12
|
-
|
13
|
-
* @param request
|
14
|
-
|
15
|
-
* @param response
|
16
|
-
|
17
|
-
* @throw ServletException
|
18
|
-
|
19
|
-
* @throw IOException
|
20
|
-
|
21
|
-
*
|
22
|
-
|
23
|
-
*/
|
24
|
-
|
25
|
-
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
26
|
-
|
27
|
-
throws ServletException, IOException {
|
28
|
-
|
29
|
-
response.setContentType("text/html; charset=utf-8");
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
---------------------------------------------------------------------------------
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
doget メソッドの request とresponse の引数はそれぞれ何を受け取っているのでしょうか?
|