teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

訂正

2019/05/09 06:00

投稿

kokok
kokok

スコア145

title CHANGED
File without changes
body CHANGED
@@ -3,8 +3,9 @@
3
3
 
4
4
 
5
5
 
6
- 'package sample1;
7
6
 
7
+ package sample1;
8
+
8
9
  import java.io.IOException;
9
10
  import java.io.PrintWriter;
10
11
  import java.sql.Connection;
@@ -114,6 +115,11 @@
114
115
  throws ServletException, IOException {
115
116
  doGet(request, response);
116
117
  }
117
- }'
118
+
119
+
118
120
  コード
119
- ```
121
+ ```
122
+
123
+
124
+ --------------------------------------------------------------------------
125
+ doGet メソッドのrequestとresponse の引数は何を受けっと手いるのでしょうか?

1

マークダウン訂正

2019/05/09 06:00

投稿

kokok
kokok

スコア145

title CHANGED
File without changes
body CHANGED
@@ -1,19 +1,119 @@
1
1
  ```java
2
2
 
3
+
4
+
5
+
6
+ 'package sample1;
7
+
8
+ import java.io.IOException;
9
+ import java.io.PrintWriter;
10
+ import java.sql.Connection;
11
+ import java.sql.DriverManager;
12
+ import java.sql.ResultSet;
13
+ import java.sql.SQLException;
14
+ import java.sql.Statement;
15
+
16
+ import javax.servlet.ServletException;
17
+ import javax.servlet.annotation.WebServlet;
18
+ import javax.servlet.http.HttpServlet;
19
+ import javax.servlet.http.HttpServletRequest;
20
+ import javax.servlet.http.HttpServletResponse;
21
+
22
+ /**
23
+ *
24
+ * @author mori
25
+ *
3
- コード
26
+ */
27
+ @WebServlet("/Hello")
28
+ public class Hello extends HttpServlet {
29
+ private static final long serialVersionUID = 1L;
30
+
31
+
4
- ```
32
+ /**
5
33
  * doGetメソッド
6
34
  * SELECT文を発行・画面に表示する
7
35
  * @param request
8
36
  * @param response
9
- * @throw ServletException
37
+ * @throw ServletException 処理中に問題が起こったときに、Servlet がスローする可能性のある一般的な例外を定義しています。
10
- * @throw IOException
38
+ * @throw IOException 入出力処理の失敗、または割り込みの発生によって生成される例外
11
39
  *
12
40
  */
13
41
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
14
42
  throws ServletException, IOException {
15
43
  response.setContentType("text/html; charset=utf-8");
44
+ PrintWriter out = response.getWriter();
16
45
 
17
- ---------------------------------------------------------------------------------
46
+ out.println("<html>");
18
47
 
48
+ out.println("<body>");
49
+
50
+ Connection conn = null;
51
+ String url = "jdbc:mysql://localhost/training";
52
+ String user = "root";
53
+ String password = "ryota";
54
+
55
+ try {
56
+ Class.forName("com.mysql.jdbc.Driver");
57
+ conn = DriverManager.getConnection(url, user, password);
58
+
59
+ Statement stmt = conn.createStatement();
60
+ String sql = "SELECT * FROM user";
61
+ ResultSet rs = stmt.executeQuery(sql);
62
+
63
+ out.println("<table = border=1>");
64
+ out.println("<tr>");
65
+ out.println("<th>");
66
+ out.println("</th>");
67
+ out.println("<th>");
68
+ out.println("name");
69
+ out.println("</th>");
70
+ out.println("<th>");
71
+ out.println("age");
72
+ out.println("</th>");
73
+
74
+ while (rs.next()) {
75
+ int id = rs.getInt("id");
76
+ String name = rs.getString("name");
77
+ int age = rs.getInt("age");
78
+
79
+ out.println("<tr>" + "<td>" + id + "</td>" + "<td>" + name + "</td>" + "<td>" + age + "</td>" + "<tr>");
80
+ out.println("<br>");
81
+
82
+ }
83
+ out.println("</table>");
84
+ rs.close();
85
+ stmt.close();
86
+ } catch (ClassNotFoundException e) {
87
+ out.println("lassNotFoundException:" + e.getMessage());
88
+ } catch (SQLException e) {
89
+ out.println("SQLException:" + e.getMessage());
90
+ } catch (Exception e) {
91
+ out.println("Exception:" + e.getMessage());
92
+ } finally {
93
+
94
+ try {
95
+ if (conn != null) {
96
+ conn.close();
97
+ }
98
+ } catch (SQLException e) {
99
+ out.println("SQLException:" + e.getMessage());
100
+ }
101
+
102
+ }
103
+
104
+ out.println("</body>");
105
+ out.println("</html>");
106
+ }
107
+
108
+ /**
109
+ * doPostメソッド
110
+ *
111
+ */
112
+
113
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
114
+ throws ServletException, IOException {
19
- doget メソッドの request response の引数はそれぞれ何を受け取っているのでしょうか?
115
+ doGet(request, response);
116
+ }
117
+ }'
118
+ コード
119
+ ```