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

質問編集履歴

3

login\.jspを削除、list\.jspを追加。

2016/09/19 06:28

投稿

yuichikubo
yuichikubo

スコア12

title CHANGED
File without changes
body CHANGED
@@ -36,29 +36,17 @@
36
36
  import javax.servlet.http.HttpServlet;
37
37
  import javax.servlet.http.HttpServletRequest;
38
38
  import javax.servlet.http.HttpServletResponse;
39
- //Servletとして必要なインターフェース・クラスをインポート//
40
39
 
41
40
  @WebServlet(name = "MemoServlet", urlPatterns = { "/MemoServlet" })
42
41
  public class MemoServlet extends HttpServlet {
43
-
44
-
42
+
45
43
  private static final long serialVersionUID = 1L;
46
44
 
47
- /**
48
- * JSPのベースディレクトリ。
49
- */
50
- private static final String JSP_BASE = "/WEB-INF/jsp/";
45
+ private static final String JSP_BASE = "/WEB-INF/jsp/";
51
46
 
52
- /**
53
- * データベースのコネクションを保持します。
54
- */
55
- private Connection _pooledConnection;
47
+ private Connection _pooledConnection;
56
48
 
57
-
58
- /**
59
- * 構築します。
60
- */
61
- public MemoServlet() {
49
+ public MemoServlet() {
62
50
  _pooledConnection = null;
63
51
  }
64
52
 
@@ -79,46 +67,42 @@
79
67
  @Override
80
68
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
81
69
  throws ServletException, IOException {
82
- // 要求からactionパラメータを取得
70
+
83
71
  String action = req.getParameter("action");
84
72
 
85
73
  String forward = null;
86
74
  if("login".equals(action)) {
87
- // ログイン画面の処理
75
+
88
- // login.jspへフォワードする
89
76
  forward = JSP_BASE + "login.jsp";
90
77
  }else{
91
- // 不正なアクションの場合
78
+
92
79
  throw new ServletException("不正なリクエストです");
93
80
  }
94
81
 
95
- // JSPへのフォワード
96
82
  RequestDispatcher dispatcher = req.getRequestDispatcher(forward);
97
83
  dispatcher.forward(req, resp);
98
84
  }
99
85
  @Override
100
86
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
101
87
  throws ServletException, IOException {
102
- // 要求からactionパラメータを取得
88
+
103
89
  String action = req.getParameter("action");
104
90
 
105
91
  String forward = null;
106
92
  if ("login_action".equals(action)) {
107
- // ログイン画面からの入力受付
93
+
108
94
  forward = doLoginAction(req, resp);
109
95
  } else {
110
- // 不正なアクションの場合
96
+
111
97
  forward = doError(req, resp, "不正なリクエストです");
112
98
  }
113
99
 
114
- // JSPへのフォワード
100
+
115
101
  RequestDispatcher dispatcher = req.getRequestDispatcher(forward);
116
102
  dispatcher.forward(req, resp);
117
103
  }
118
104
 
119
- /**
105
+
120
- * ログイン処理をおこないます。
121
- */
122
106
  private String doLoginAction(HttpServletRequest req,
123
107
  HttpServletResponse resp) throws ServletException, IOException {
124
108
  String userID = req.getParameter("user_id");
@@ -128,42 +112,34 @@
128
112
  }
129
113
 
130
114
  try{
131
- // ユーザを取得する
115
+
132
116
  User user = getUser(userID, password);
133
117
  if(user == null) {
134
118
  return doError(req, resp, "不正なユーザIDもしくはパラメータです。");
135
119
  }
136
120
 
137
- // メモを取得する
121
+
138
122
  Memo[] memos = getMemos();
139
123
 
140
- // アイテムを要求オブジェクトに格納する
124
+
141
125
  req.setAttribute("memos", memos);
142
126
 
143
- // 一覧を表示する
144
127
  return JSP_BASE + "list.jsp";
145
128
  }catch(SQLException e) {
146
129
  return doError(req, resp, e.getMessage());
147
130
  }
148
131
  }
149
132
 
150
- /**
151
- * エラーを表示します。
152
- */
153
133
 
154
134
  private String doError(HttpServletRequest req, HttpServletResponse resp,
155
135
  String message) throws ServletException, IOException {
156
136
  req.setAttribute("message", message);
157
137
 
158
- // エラーを表示する
159
138
  return JSP_BASE + "error.jsp";
160
139
  }
161
- /**
162
- * 接続オブジェクトを生成します。
163
- */
164
- private Connection getConnection()
140
+ private Connection getConnection()
165
141
  throws SQLException {
166
- // Connectionの準備
142
+
167
143
  if(_pooledConnection != null) {
168
144
  return _pooledConnection;
169
145
  }
@@ -184,14 +160,11 @@
184
160
  }
185
161
  }
186
162
 
187
- /**
188
- * ユーザを取得します。
189
- */
190
- private User getUser(String userID, String password)
163
+ private User getUser(String userID, String password)
191
164
  throws SQLException {
192
165
  Statement statement = null;
193
166
  try {
194
- // SQL文を発行
167
+
195
168
  statement = getConnection().createStatement();
196
169
  ResultSet resultSet = statement
197
170
  .executeQuery("SELECT USER_ID,USER_NAME FROM USER WHERE USER_ID='"
@@ -221,54 +194,115 @@
221
194
  ```
222
195
 
223
196
  ```html
224
- login.jsp
197
+ list.jsp
225
198
 
226
199
  <%@ page language="java" contentType="text/html; charset=utf-8" %>
200
+ <%@ page import="pack.User" %>
201
+ <%@ page import="pack.Memo" %>
202
+ <%@ page import="java.util.Calendar" %>
203
+ <%
204
+ User currentUser = (User) request.getSession().getAttribute("currentUser");
205
+ %>
227
206
  <html>
228
207
  <head>
229
208
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
230
- <title>ログイン</title>
209
+ <title>検索一覧</title>
231
210
  <link rel="STYLESHEET" href="todo.css" type="text/css">
232
- <script type="text/javascript" src="login.js">
233
- </script>
234
211
  </head>
212
+ <body>
213
+ <!-- 作業登録・検索 -->
235
- <body onload="fieldChanged();">
214
+ <table border="0" width="90%" class="toolbar">
236
- <h1>ログイン</h1>
215
+ <tr>
237
-
238
- <div align="center">
239
- <table border="0">
240
216
  <form action="todo" method="post">
241
- <input type="hidden" name="action" value="login_action">
217
+ <input type="hidden" name="action" value="add">
242
- <tr>
218
+ <td>
243
- <th class="login_field">
244
- ID
245
- </th>
246
- <td class="login_field">
247
- <input type="text" name="user_id" value=""
219
+ <input type="submit" value="作業登録">
248
- size="24" id="user_id"
249
- onkeyup="fieldChanged();"
250
- onchange="fieldChanged();">
251
- </td>
220
+ </td>
252
- </tr>
253
- <tr>
254
- <th class="login_field">
255
- PW
256
- </th>
257
- <td class="login_field">
258
- <input type="password" name="password" value=""
259
- size="24" id="password"
260
- onkeyup="fieldChanged();"
261
- onchange="fieldChanged();">
262
- </td>
263
- </tr>
264
- <tr>
265
- <td colspan="2" class="login_button">
266
- <input type="submit" value="ログイン" id="login">
267
- </td>
268
- </tr>
269
221
  </form>
222
+ <td align="left">
223
+ <table border="0">
224
+ <tr>
225
+ <td>
226
+ タイトル
227
+ </td>
228
+ <form action="todo" method="post">
229
+ <input type="hidden" name="action" value="search">
230
+ <td>
231
+ <input type="text" name="title" value="" size="24">
232
+ </td>
233
+ </form>
234
+ </tr>
235
+ <tr>
236
+ <td>
237
+ 更新日
238
+ </td>
239
+ <form action="todo" method="post">
240
+ <input type="hidden" name="action" value="search">
241
+ <td>
242
+ <input type="text" name="update_datetime" value="" size="24">
243
+ </td>
244
+ </form>
245
+ </tr>
246
+ <tr>
247
+ <td>
248
+ <input type="submit" value="検索">
249
+ </td>
250
+ </tr>
270
- </table>
251
+ </table>
271
- </div>
252
+ </td>
253
+ </tr>
254
+ </table>
255
+ <%
256
+ Memo[] memos = (Memo[]) request.getAttribute("items");
257
+ if(memos.length == 0) {
258
+ // アイテムが存在しない場合
259
+ %>
260
+ <div align="center">検索項目はありません。</div>
261
+ <%
262
+ }else{
263
+ %>
264
+ <table border="0" width="90%" class="list">
265
+ <tr>
266
+ <th>
267
+ ID
268
+ </th>
269
+ <th>
270
+ タイトル
271
+ </th>
272
+ <th>
273
+ 登録日
274
+ </th>
275
+ <th>
276
+ 更新日
277
+ </th>
278
+ </tr>
279
+ 一部省略
280
+ // メモを出力
281
+ for(int i = 0; i < memos.length; i ++) {
282
+ Memo memo = memos[i];
283
+ String styleAttr = "";
284
+ %>
285
+ <tr>
286
+ <td <%= styleAttr %>>
287
+ <%= memo.getId() %>
288
+ </td>
289
+ <td <%= styleAttr %>>
290
+ <%= memo.getTitle() %>
291
+ </td>
292
+ <td <%= styleAttr %>>
293
+ <%= memo.getInsertDate() %>
294
+ </td>
295
+ <td <%= styleAttr %>>
296
+ <%= memo.getUpdateDate() %>
297
+ </td>
298
+ </tr>
299
+ <%
300
+ }
301
+ %>
302
+ </table>
303
+ <%
304
+ }
305
+ %>
272
306
  </body>
273
307
  </html>
274
308
  ```

2

閉じタグを追加。

2016/09/19 06:28

投稿

yuichikubo
yuichikubo

スコア12

title CHANGED
File without changes
body CHANGED
@@ -17,7 +17,7 @@
17
17
  javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
18
18
  org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
19
19
 
20
- ```Java
20
+ ```java
21
21
  ★MemoServlet.java
22
22
 
23
23
  package pack;
@@ -218,8 +218,9 @@
218
218
 
219
219
  一部省略
220
220
  }
221
+ ```
221
222
 
222
-
223
+ ```html
223
224
  ★login.jsp
224
225
 
225
226
  <%@ page language="java" contentType="text/html; charset=utf-8" %>
@@ -269,4 +270,5 @@
269
270
  </table>
270
271
  </div>
271
272
  </body>
272
- </html>
273
+ </html>
274
+ ```

1

「```ここに言語を入力 」を「```Java」に修正。

2016/09/19 03:10

投稿

yuichikubo
yuichikubo

スコア12

title CHANGED
File without changes
body CHANGED
@@ -17,7 +17,7 @@
17
17
  javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
18
18
  org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
19
19
 
20
- ```ここに言語を入力
20
+ ```Java
21
21
  ★MemoServlet.java
22
22
 
23
23
  package pack;