質問編集履歴
4
意図的な内容抹消の取り消し
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,145 @@
|
|
1
1
|
サーブレットでDAOを呼び出す書式を教えてください
|
2
2
|
|
3
|
-
IDとパスワードを打ち込む掲示板においてデータベースにあるIDとパスワードと間違って
|
3
|
+
IDとパスワードを打ち込む掲示板においてデータベースにあるIDとパスワードと間違っている場合はログイン出来ずエラーメッセージを表示させたいです
|
4
|
+
|
5
|
+
DAO
|
6
|
+
|
7
|
+
import java.sql.Connection;
|
8
|
+
|
9
|
+
import java.sql.PreparedStatement;
|
10
|
+
|
11
|
+
import java.sql.ResultSet;
|
12
|
+
|
13
|
+
import java.sql.SQLException;
|
14
|
+
|
15
|
+
import java.util.ArrayList;
|
16
|
+
|
17
|
+
import java.util.List;
|
18
|
+
|
19
|
+
import javax.naming.InitialContext;
|
20
|
+
|
21
|
+
import javax.naming.NamingException;
|
22
|
+
|
23
|
+
import javax.sql.DataSource;
|
24
|
+
|
25
|
+
public class indexDAO {
|
26
|
+
|
27
|
+
private DataSource source;
|
28
|
+
|
29
|
+
private static final String SELECT = "select * from account where user_id = ? user_pass = ? ";
|
30
|
+
|
31
|
+
public indexDAO() throws NamingException {
|
32
|
+
|
33
|
+
InitialContext context = new InitialContext();
|
34
|
+
|
35
|
+
source = (DataSource) context.lookup("");
|
36
|
+
|
37
|
+
}
|
38
|
+
|
39
|
+
public List<AccountBean> getIndexList() throws SQLException {
|
40
|
+
|
41
|
+
List<AccountBean> indexList = new ArrayList<AccountBean>();
|
42
|
+
|
43
|
+
Connection connection = source.getConnection();
|
44
|
+
|
45
|
+
try {
|
46
|
+
|
47
|
+
PreparedStatement statement = connection.prepareStatement(SELECT);
|
48
|
+
|
49
|
+
statement.setString(1,"id");
|
50
|
+
|
51
|
+
statement.setString(2,"password");
|
52
|
+
|
53
|
+
ResultSet result = statement.executeQuery();
|
54
|
+
|
55
|
+
while (result.next()) {
|
56
|
+
|
57
|
+
AccountBean index = new AccountBean();
|
58
|
+
|
59
|
+
index.setId(result.getString("id"));
|
60
|
+
|
61
|
+
index.setPassword(result.getString("password"));
|
62
|
+
|
63
|
+
indexList.add(index);
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
statement.close();
|
68
|
+
|
69
|
+
} catch (SQLException e) {
|
70
|
+
|
71
|
+
e.printStackTrace();
|
72
|
+
|
73
|
+
} finally {
|
74
|
+
|
75
|
+
if (connection != null) {
|
76
|
+
|
77
|
+
connection.close();
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
return indexList;
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
サーブレットのdopostメソッド
|
90
|
+
|
91
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
92
|
+
|
93
|
+
throws ServletException, IOException {
|
94
|
+
|
95
|
+
request.setCharacterEncoding("UTF-8");
|
96
|
+
|
97
|
+
String id = request.getParameter("userID");
|
98
|
+
|
99
|
+
String password = request.getParameter("password");
|
100
|
+
|
101
|
+
ArrayList<String> errorList = new ArrayList<String>();
|
102
|
+
|
103
|
+
AccountBean account = new AccountBean();
|
104
|
+
|
105
|
+
DAO hoge = new DAO();
|
106
|
+
|
107
|
+
ArrayList<AccountBean> huga = hoge.select(AccountBean);
|
108
|
+
|
109
|
+
//エラーチェック処理
|
110
|
+
|
111
|
+
if (!Check.checkBlank(id) || !Check.checkBlank(password)) {
|
112
|
+
|
113
|
+
errorList.add("IDとパスワードは必須項目です。");
|
114
|
+
|
115
|
+
request.setAttribute("errorList", errorList);
|
116
|
+
|
117
|
+
String url = PropertyLoader.getProperty("url.jsp.index");
|
118
|
+
|
119
|
+
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
|
120
|
+
|
121
|
+
dispatcher.forward(request, response);
|
122
|
+
|
123
|
+
} else if (account != account) {
|
124
|
+
|
125
|
+
errorList.add("IDとパスワードが間違っています。");
|
126
|
+
|
127
|
+
request.setAttribute("errorList", errorList);
|
128
|
+
|
129
|
+
String url = PropertyLoader.getProperty("url.jsp.index");
|
130
|
+
|
131
|
+
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
|
132
|
+
|
133
|
+
dispatcher.forward(request, response);
|
134
|
+
|
135
|
+
} else {
|
136
|
+
|
137
|
+
String url = PropertyLoader.getProperty("url.sb.input");
|
138
|
+
|
139
|
+
response.sendRedirect(url);
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
}
|
3
変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,179 +1,3 @@
|
|
1
1
|
サーブレットでDAOを呼び出す書式を教えてください
|
2
2
|
|
3
|
-
IDとパスワードを打ち込む掲示板においてデータベースにあるIDとパスワードと間違って
|
3
|
+
IDとパスワードを打ち込む掲示板においてデータベースにあるIDとパスワードと間違って
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
DAO
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
import java.sql.Connection;
|
14
|
-
|
15
|
-
import java.sql.PreparedStatement;
|
16
|
-
|
17
|
-
import java.sql.ResultSet;
|
18
|
-
|
19
|
-
import java.sql.SQLException;
|
20
|
-
|
21
|
-
import java.util.ArrayList;
|
22
|
-
|
23
|
-
import java.util.List;
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
import javax.naming.InitialContext;
|
28
|
-
|
29
|
-
import javax.naming.NamingException;
|
30
|
-
|
31
|
-
import javax.sql.DataSource;
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
public class indexDAO {
|
40
|
-
|
41
|
-
private DataSource source;
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
private static final String SELECT = "select * from account where user_id = ? user_pass = ? ";
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
public indexDAO() throws NamingException {
|
50
|
-
|
51
|
-
InitialContext context = new InitialContext();
|
52
|
-
|
53
|
-
source = (DataSource) context.lookup("");
|
54
|
-
|
55
|
-
}
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
public List<AccountBean> getIndexList() throws SQLException {
|
60
|
-
|
61
|
-
List<AccountBean> indexList = new ArrayList<AccountBean>();
|
62
|
-
|
63
|
-
Connection connection = source.getConnection();
|
64
|
-
|
65
|
-
try {
|
66
|
-
|
67
|
-
PreparedStatement statement = connection.prepareStatement(SELECT);
|
68
|
-
|
69
|
-
statement.setString(1,"id");
|
70
|
-
|
71
|
-
statement.setString(2,"password");
|
72
|
-
|
73
|
-
ResultSet result = statement.executeQuery();
|
74
|
-
|
75
|
-
while (result.next()) {
|
76
|
-
|
77
|
-
AccountBean index = new AccountBean();
|
78
|
-
|
79
|
-
index.setId(result.getString("id"));
|
80
|
-
|
81
|
-
index.setPassword(result.getString("password"));
|
82
|
-
|
83
|
-
indexList.add(index);
|
84
|
-
|
85
|
-
}
|
86
|
-
|
87
|
-
statement.close();
|
88
|
-
|
89
|
-
} catch (SQLException e) {
|
90
|
-
|
91
|
-
e.printStackTrace();
|
92
|
-
|
93
|
-
} finally {
|
94
|
-
|
95
|
-
if (connection != null) {
|
96
|
-
|
97
|
-
connection.close();
|
98
|
-
|
99
|
-
}
|
100
|
-
|
101
|
-
}
|
102
|
-
|
103
|
-
return indexList;
|
104
|
-
|
105
|
-
}
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
}
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
サーブレットのdopostメソッド
|
114
|
-
|
115
|
-
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
116
|
-
|
117
|
-
throws ServletException, IOException {
|
118
|
-
|
119
|
-
request.setCharacterEncoding("UTF-8");
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
String id = request.getParameter("userID");
|
124
|
-
|
125
|
-
String password = request.getParameter("password");
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
ArrayList<String> errorList = new ArrayList<String>();
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
AccountBean account = new AccountBean();
|
134
|
-
|
135
|
-
DAO hoge = new DAO();
|
136
|
-
|
137
|
-
ArrayList<AccountBean> huga = hoge.select(AccountBean);
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
//エラーチェック処理
|
142
|
-
|
143
|
-
if (!Check.checkBlank(id) || !Check.checkBlank(password)) {
|
144
|
-
|
145
|
-
errorList.add("IDとパスワードは必須項目です。");
|
146
|
-
|
147
|
-
request.setAttribute("errorList", errorList);
|
148
|
-
|
149
|
-
String url = PropertyLoader.getProperty("url.jsp.index");
|
150
|
-
|
151
|
-
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
|
152
|
-
|
153
|
-
dispatcher.forward(request, response);
|
154
|
-
|
155
|
-
} else if (account != account) {
|
156
|
-
|
157
|
-
errorList.add("IDとパスワードが間違っています。");
|
158
|
-
|
159
|
-
request.setAttribute("errorList", errorList);
|
160
|
-
|
161
|
-
String url = PropertyLoader.getProperty("url.jsp.index");
|
162
|
-
|
163
|
-
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
|
164
|
-
|
165
|
-
dispatcher.forward(request, response);
|
166
|
-
|
167
|
-
} else {
|
168
|
-
|
169
|
-
String url = PropertyLoader.getProperty("url.sb.input");
|
170
|
-
|
171
|
-
response.sendRedirect(url);
|
172
|
-
|
173
|
-
}
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
}
|
178
|
-
|
179
|
-
}
|
2
変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
DAO
|
8
8
|
|
9
|
-
|
9
|
+
|
10
10
|
|
11
11
|
|
12
12
|
|
@@ -32,7 +32,7 @@
|
|
32
32
|
|
33
33
|
|
34
34
|
|
35
|
-
|
35
|
+
|
36
36
|
|
37
37
|
|
38
38
|
|
1
追記しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,11 +1,179 @@
|
|
1
1
|
サーブレットでDAOを呼び出す書式を教えてください
|
2
|
+
|
3
|
+
IDとパスワードを打ち込む掲示板においてデータベースにあるIDとパスワードと間違っている場合はログイン出来ずエラーメッセージを表示させたいです
|
2
4
|
|
3
5
|
|
4
6
|
|
7
|
+
DAO
|
8
|
+
|
9
|
+
package jp.sljacademy.javaweb.dao;
|
5
10
|
|
6
11
|
|
12
|
+
|
13
|
+
import java.sql.Connection;
|
14
|
+
|
15
|
+
import java.sql.PreparedStatement;
|
16
|
+
|
17
|
+
import java.sql.ResultSet;
|
18
|
+
|
19
|
+
import java.sql.SQLException;
|
20
|
+
|
21
|
+
import java.util.ArrayList;
|
22
|
+
|
23
|
+
import java.util.List;
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
import javax.naming.InitialContext;
|
28
|
+
|
29
|
+
import javax.naming.NamingException;
|
30
|
+
|
31
|
+
import javax.sql.DataSource;
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
import jp.sljacademy.javaweb.bean.AccountBean;
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
public class indexDAO {
|
40
|
+
|
41
|
+
private DataSource source;
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
private static final String SELECT = "select * from account where user_id = ? user_pass = ? ";
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
public indexDAO() throws NamingException {
|
50
|
+
|
51
|
+
InitialContext context = new InitialContext();
|
52
|
+
|
53
|
+
source = (DataSource) context.lookup("");
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
public List<AccountBean> getIndexList() throws SQLException {
|
60
|
+
|
61
|
+
List<AccountBean> indexList = new ArrayList<AccountBean>();
|
62
|
+
|
63
|
+
Connection connection = source.getConnection();
|
64
|
+
|
65
|
+
try {
|
66
|
+
|
67
|
+
PreparedStatement statement = connection.prepareStatement(SELECT);
|
68
|
+
|
69
|
+
statement.setString(1,"id");
|
70
|
+
|
71
|
+
statement.setString(2,"password");
|
72
|
+
|
73
|
+
ResultSet result = statement.executeQuery();
|
74
|
+
|
75
|
+
while (result.next()) {
|
76
|
+
|
77
|
+
AccountBean index = new AccountBean();
|
78
|
+
|
79
|
+
index.setId(result.getString("id"));
|
80
|
+
|
81
|
+
index.setPassword(result.getString("password"));
|
82
|
+
|
83
|
+
indexList.add(index);
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
statement.close();
|
88
|
+
|
89
|
+
} catch (SQLException e) {
|
90
|
+
|
91
|
+
e.printStackTrace();
|
92
|
+
|
93
|
+
} finally {
|
94
|
+
|
95
|
+
if (connection != null) {
|
96
|
+
|
97
|
+
connection.close();
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
return indexList;
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
サーブレットのdopostメソッド
|
114
|
+
|
115
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
116
|
+
|
117
|
+
throws ServletException, IOException {
|
118
|
+
|
119
|
+
request.setCharacterEncoding("UTF-8");
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
String id = request.getParameter("userID");
|
124
|
+
|
125
|
+
String password = request.getParameter("password");
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
ArrayList<String> errorList = new ArrayList<String>();
|
130
|
+
|
131
|
+
|
132
|
+
|
7
|
-
AccountBean account = new AccountBean();
|
133
|
+
AccountBean account = new AccountBean();
|
8
134
|
|
9
135
|
DAO hoge = new DAO();
|
10
136
|
|
11
137
|
ArrayList<AccountBean> huga = hoge.select(AccountBean);
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
//エラーチェック処理
|
142
|
+
|
143
|
+
if (!Check.checkBlank(id) || !Check.checkBlank(password)) {
|
144
|
+
|
145
|
+
errorList.add("IDとパスワードは必須項目です。");
|
146
|
+
|
147
|
+
request.setAttribute("errorList", errorList);
|
148
|
+
|
149
|
+
String url = PropertyLoader.getProperty("url.jsp.index");
|
150
|
+
|
151
|
+
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
|
152
|
+
|
153
|
+
dispatcher.forward(request, response);
|
154
|
+
|
155
|
+
} else if (account != account) {
|
156
|
+
|
157
|
+
errorList.add("IDとパスワードが間違っています。");
|
158
|
+
|
159
|
+
request.setAttribute("errorList", errorList);
|
160
|
+
|
161
|
+
String url = PropertyLoader.getProperty("url.jsp.index");
|
162
|
+
|
163
|
+
RequestDispatcher dispatcher = request.getRequestDispatcher(url);
|
164
|
+
|
165
|
+
dispatcher.forward(request, response);
|
166
|
+
|
167
|
+
} else {
|
168
|
+
|
169
|
+
String url = PropertyLoader.getProperty("url.sb.input");
|
170
|
+
|
171
|
+
response.sendRedirect(url);
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|