회원탈퇴

1.View

1) WebContent - deleteMember.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
   <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
                                                <head>
                <script type="text/javascript">if (!window.T) { window.T = {} }
window.T.config = {"TOP_SSL_URL":"https://www.tistory.com","PREVIEW":false,"ROLE":"guest","PREV_PAGE":"","NEXT_PAGE":"","BLOG":{"id":3189920,"name":"dev-juniors","title":"개발자로가는길","isDormancy":true,"nickName":"서나브로","status":"open","profileStatus":"normal"},"NEED_COMMENT_LOGIN":false,"COMMENT_LOGIN_CONFIRM_MESSAGE":"","LOGIN_URL":"https://www.tistory.com/auth/login/?redirectUrl=https://dev-juniors.tistory.com/category/1.JAVA%2520Web%2520Programming/%255B3%25EC%25A3%25BC%25EC%25B0%25A8%255DJSP%252Bmodel2","DEFAULT_URL":"https://dev-juniors.tistory.com","USER":{"name":null,"homepage":null,"id":0,"profileImage":null},"SUBSCRIPTION":{"status":"none","isConnected":false,"isPending":false,"isWait":false,"isProcessing":false,"isNone":true},"IS_LOGIN":false,"HAS_BLOG":false,"IS_SUPPORT":false,"IS_SCRAPABLE":false,"TOP_URL":"http://www.tistory.com","JOIN_URL":"https://www.tistory.com/member/join","PHASE":"prod","ROLE_GROUP":"visitor"};
window.T.entryInfo = null;
window.appInfo = {"domain":"tistory.com","topUrl":"https://www.tistory.com","loginUrl":"https://www.tistory.com/auth/login","logoutUrl":"https://www.tistory.com/auth/logout"};
window.initData = {};
 
window.TistoryBlog = {
    basePath: "",
    tistoryUrl: "https://dev-juniors.tistory.com",
    token: "PtQf6jC7tQaydn/vGcnsyfI4our2U/p9Vm8jKUYjk47vrwyqgRw4Id3waqwksSql"
};
var servicePath = "";
var blogURL = "";</script>
 
                 
                 
                 
<meta charset="UTF-8">
<title>회원 탈퇴</title>
 
                <style type="text/css">.another_category {
    border: 1px solid #E5E5E5;
    padding: 10px 10px 5px;
    margin: 10px 0;
    clear: both;
}
 
.another_category h4 {
    font-size: 12px !important;
    margin: 0 !important;
    border-bottom: 1px solid #E5E5E5 !important;
    padding: 2px 0 6px !important;
}
 
.another_category h4 a {
    font-weight: bold !important;
}
 
.another_category table {
    table-layout: fixed;
    border-collapse: collapse;
    width: 100% !important;
    margin-top: 10px !important;
}
 
* html .another_category table {
    width: auto !important;
}
 
*:first-child + html .another_category table {
    width: auto !important;
}
 
.another_category th, .another_category td {
    padding: 0 0 4px !important;
}
 
.another_category th {
    text-align: left;
    font-size: 12px !important;
    font-weight: normal;
    word-break: break-all;
    overflow: hidden;
    line-height: 1.5;
}
 
.another_category td {
    text-align: right;
    width: 80px;
    font-size: 11px;
}
 
.another_category th a {
    font-weight: normal;
    text-decoration: none;
    border: none !important;
}
 
.another_category th a.current {
    font-weight: bold;
    text-decoration: none !important;
    border-bottom: 1px solid !important;
}
 
.another_category th span {
    font-weight: normal;
    text-decoration: none;
    font: 10px Tahoma, Sans-serif;
    border: none !important;
}
 
.another_category_color_gray, .another_category_color_gray h4 {
    border-color: #E5E5E5 !important;
}
 
.another_category_color_gray * {
    color: #909090 !important;
}
 
.another_category_color_gray th a.current {
    border-color: #909090 !important;
}
 
.another_category_color_gray h4, .another_category_color_gray h4 a {
    color: #737373 !important;
}
 
.another_category_color_red, .another_category_color_red h4 {
    border-color: #F6D4D3 !important;
}
 
.another_category_color_red * {
    color: #E86869 !important;
}
 
.another_category_color_red th a.current {
    border-color: #E86869 !important;
}
 
.another_category_color_red h4, .another_category_color_red h4 a {
    color: #ED0908 !important;
}
 
.another_category_color_green, .another_category_color_green h4 {
    border-color: #CCE7C8 !important;
}
 
.another_category_color_green * {
    color: #64C05B !important;
}
 
.another_category_color_green th a.current {
    border-color: #64C05B !important;
}
 
.another_category_color_green h4, .another_category_color_green h4 a {
    color: #3EA731 !important;
}
 
.another_category_color_blue, .another_category_color_blue h4 {
    border-color: #C8DAF2 !important;
}
 
.another_category_color_blue * {
    color: #477FD6 !important;
}
 
.another_category_color_blue th a.current {
    border-color: #477FD6 !important;
}
 
.another_category_color_blue h4, .another_category_color_blue h4 a {
    color: #1960CA !important;
}
 
.another_category_color_violet, .another_category_color_violet h4 {
    border-color: #E1CEEC !important;
}
 
.another_category_color_violet * {
    color: #9D64C5 !important;
}
 
.another_category_color_violet th a.current {
    border-color: #9D64C5 !important;
}
 
.another_category_color_violet h4, .another_category_color_violet h4 a {
    color: #7E2CB5 !important;
}
</style>
 
                 
<link rel="canonical" href="https://dev-juniors.tistory.com"/>
 
<!-- BEGIN STRUCTURED_DATA -->
<script type="application/ld+json">
    {"@context":"http://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":0,"item":{"@id":"https://dev-juniors.tistory.com/19","name":"Model2 Architecture - (6) 회원탈퇴"}},{"@type":"ListItem","position":1,"item":{"@id":"https://dev-juniors.tistory.com/18","name":"Model2 Architecture - (5) 회원수정"}},{"@type":"ListItem","position":2,"item":{"@id":"https://dev-juniors.tistory.com/13","name":"Model2 Architecture - (4) 로그아웃"}},{"@type":"ListItem","position":3,"item":{"@id":"https://dev-juniors.tistory.com/12","name":"Model2 Architecture - (3) 로그인"}},{"@type":"ListItem","position":4,"item":{"@id":"https://dev-juniors.tistory.com/11","name":"Model2 Architecture - (2) 회원조회"}},{"@type":"ListItem","position":5,"item":{"@id":"https://dev-juniors.tistory.com/10","name":"Model2 Architecture - (1) 회원가입"}},{"@type":"ListItem","position":6,"item":{"@id":"https://dev-juniors.tistory.com/9","name":"Model2 Architecture - table생성, 홈화면(index,dispatcher 등), error페이지"}}]}
</script>
<!-- END STRUCTURED_DATA -->
<link rel="stylesheet" type="text/css" href="//t1.daumcdn.net/tistory_admin/www/style/top/font.css"/>
 
                 
                </head>
<body>
    <form action ="/DispatcherServlet" method="post">
        <input type="hidden" name="command" value="delete">
    </form>
2) WebContent - delete_result.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
                                                <head>
                <script type="text/javascript">if (!window.T) { window.T = {} }
window.T.config = {"TOP_SSL_URL":"https://www.tistory.com","PREVIEW":false,"ROLE":"guest","PREV_PAGE":"","NEXT_PAGE":"","BLOG":{"id":3189920,"name":"dev-juniors","title":"개발자로가는길","isDormancy":true,"nickName":"서나브로","status":"open","profileStatus":"normal"},"NEED_COMMENT_LOGIN":false,"COMMENT_LOGIN_CONFIRM_MESSAGE":"","LOGIN_URL":"https://www.tistory.com/auth/login/?redirectUrl=https://dev-juniors.tistory.com/category/1.JAVA%2520Web%2520Programming/%255B3%25EC%25A3%25BC%25EC%25B0%25A8%255DJSP%252Bmodel2","DEFAULT_URL":"https://dev-juniors.tistory.com","USER":{"name":null,"homepage":null,"id":0,"profileImage":null},"SUBSCRIPTION":{"status":"none","isConnected":false,"isPending":false,"isWait":false,"isProcessing":false,"isNone":true},"IS_LOGIN":false,"HAS_BLOG":false,"IS_SUPPORT":false,"IS_SCRAPABLE":false,"TOP_URL":"http://www.tistory.com","JOIN_URL":"https://www.tistory.com/member/join","PHASE":"prod","ROLE_GROUP":"visitor"};
window.T.entryInfo = null;
window.appInfo = {"domain":"tistory.com","topUrl":"https://www.tistory.com","loginUrl":"https://www.tistory.com/auth/login","logoutUrl":"https://www.tistory.com/auth/logout"};
window.initData = {};
 
window.TistoryBlog = {
    basePath: "",
    tistoryUrl: "https://dev-juniors.tistory.com",
    token: "PtQf6jC7tQaydn/vGcnsyfI4our2U/p9Vm8jKUYjk47vrwyqgRw4Id3waqwksSql"
};
var servicePath = "";
var blogURL = "";</script>
 
                 
                 
                 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>회원 탈퇴</title>
 
                <style type="text/css">.another_category {
    border: 1px solid #E5E5E5;
    padding: 10px 10px 5px;
    margin: 10px 0;
    clear: both;
}
 
.another_category h4 {
    font-size: 12px !important;
    margin: 0 !important;
    border-bottom: 1px solid #E5E5E5 !important;
    padding: 2px 0 6px !important;
}
 
.another_category h4 a {
    font-weight: bold !important;
}
 
.another_category table {
    table-layout: fixed;
    border-collapse: collapse;
    width: 100% !important;
    margin-top: 10px !important;
}
 
* html .another_category table {
    width: auto !important;
}
 
*:first-child + html .another_category table {
    width: auto !important;
}
 
.another_category th, .another_category td {
    padding: 0 0 4px !important;
}
 
.another_category th {
    text-align: left;
    font-size: 12px !important;
    font-weight: normal;
    word-break: break-all;
    overflow: hidden;
    line-height: 1.5;
}
 
.another_category td {
    text-align: right;
    width: 80px;
    font-size: 11px;
}
 
.another_category th a {
    font-weight: normal;
    text-decoration: none;
    border: none !important;
}
 
.another_category th a.current {
    font-weight: bold;
    text-decoration: none !important;
    border-bottom: 1px solid !important;
}
 
.another_category th span {
    font-weight: normal;
    text-decoration: none;
    font: 10px Tahoma, Sans-serif;
    border: none !important;
}
 
.another_category_color_gray, .another_category_color_gray h4 {
    border-color: #E5E5E5 !important;
}
 
.another_category_color_gray * {
    color: #909090 !important;
}
 
.another_category_color_gray th a.current {
    border-color: #909090 !important;
}
 
.another_category_color_gray h4, .another_category_color_gray h4 a {
    color: #737373 !important;
}
 
.another_category_color_red, .another_category_color_red h4 {
    border-color: #F6D4D3 !important;
}
 
.another_category_color_red * {
    color: #E86869 !important;
}
 
.another_category_color_red th a.current {
    border-color: #E86869 !important;
}
 
.another_category_color_red h4, .another_category_color_red h4 a {
    color: #ED0908 !important;
}
 
.another_category_color_green, .another_category_color_green h4 {
    border-color: #CCE7C8 !important;
}
 
.another_category_color_green * {
    color: #64C05B !important;
}
 
.another_category_color_green th a.current {
    border-color: #64C05B !important;
}
 
.another_category_color_green h4, .another_category_color_green h4 a {
    color: #3EA731 !important;
}
 
.another_category_color_blue, .another_category_color_blue h4 {
    border-color: #C8DAF2 !important;
}
 
.another_category_color_blue * {
    color: #477FD6 !important;
}
 
.another_category_color_blue th a.current {
    border-color: #477FD6 !important;
}
 
.another_category_color_blue h4, .another_category_color_blue h4 a {
    color: #1960CA !important;
}
 
.another_category_color_violet, .another_category_color_violet h4 {
    border-color: #E1CEEC !important;
}
 
.another_category_color_violet * {
    color: #9D64C5 !important;
}
 
.another_category_color_violet th a.current {
    border-color: #9D64C5 !important;
}
 
.another_category_color_violet h4, .another_category_color_violet h4 a {
    color: #7E2CB5 !important;
}
</style>
 
                 
<link rel="canonical" href="https://dev-juniors.tistory.com"/>
 
<!-- BEGIN STRUCTURED_DATA -->
<script type="application/ld+json">
    {"@context":"http://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":0,"item":{"@id":"https://dev-juniors.tistory.com/19","name":"Model2 Architecture - (6) 회원탈퇴"}},{"@type":"ListItem","position":1,"item":{"@id":"https://dev-juniors.tistory.com/18","name":"Model2 Architecture - (5) 회원수정"}},{"@type":"ListItem","position":2,"item":{"@id":"https://dev-juniors.tistory.com/13","name":"Model2 Architecture - (4) 로그아웃"}},{"@type":"ListItem","position":3,"item":{"@id":"https://dev-juniors.tistory.com/12","name":"Model2 Architecture - (3) 로그인"}},{"@type":"ListItem","position":4,"item":{"@id":"https://dev-juniors.tistory.com/11","name":"Model2 Architecture - (2) 회원조회"}},{"@type":"ListItem","position":5,"item":{"@id":"https://dev-juniors.tistory.com/10","name":"Model2 Architecture - (1) 회원가입"}},{"@type":"ListItem","position":6,"item":{"@id":"https://dev-juniors.tistory.com/9","name":"Model2 Architecture - table생성, 홈화면(index,dispatcher 등), error페이지"}}]}
</script>
<!-- END STRUCTURED_DATA -->
<link rel="stylesheet" type="text/css" href="//t1.daumcdn.net/tistory_admin/www/style/top/font.css"/>
 
                 
                </head>
<body>
    <a href="index.jsp">HOME</a><br><br>
    <%
        String ret = (String)request.getAttribute("ret");
    %>
     
    <%=ret %>
2. Controller

1) src - controller.HandlerMapping.java에 delete 추가
1
2
3
if(command.equals("delete")) {
         c = new DeleteController();
     }
2) src-controller.DeleteController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    package controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import model.ClientDAO;
 
public class DeleteController implements Controller {
 
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("enter to DeleteController()...");
        String ret = ClientDAO.getInstance().deleteMember("minnn");
        System.out.println(ret);
        HttpSession session = request.getSession();
        session.invalidate();
        request.setAttribute("ret", ret);
        return "delete_result.jsp";
    }
 
}
3.model

1)src - model.ClientDAO.java에 Delete 메소드 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 회원 탈퇴
public String deleteMember(String id) throws SQLException {
    String ret = null;
    Connection con = null;
    PreparedStatement pstmt = null;
    int result = 0;
     
    try {
        con = getConnection();
        String sql = "delete from client where clientId = '"+id+"'";
        System.out.println(sql);
        pstmt = con.prepareStatement(sql);
        result = pstmt.executeUpdate();
        if(result>0)
            ret = "delete your id. Thank you for using my site. See you and Good luck :)";
        else
            ret = "Not deleted.";
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeAll(pstmt, con);
    }
    System.out.println(ret);
     
    return ret;
}
1. 회원수정 1)WebContent - update.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<%@ page import = "model.ClientDTO" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
                                                <head>
                <script type="text/javascript">if (!window.T) { window.T = {} }
window.T.config = {"TOP_SSL_URL":"https://www.tistory.com","PREVIEW":false,"ROLE":"guest","PREV_PAGE":"","NEXT_PAGE":"","BLOG":{"id":3189920,"name":"dev-juniors","title":"개발자로가는길","isDormancy":true,"nickName":"서나브로","status":"open","profileStatus":"normal"},"NEED_COMMENT_LOGIN":false,"COMMENT_LOGIN_CONFIRM_MESSAGE":"","LOGIN_URL":"https://www.tistory.com/auth/login/?redirectUrl=https://dev-juniors.tistory.com/category/1.JAVA%2520Web%2520Programming/%255B3%25EC%25A3%25BC%25EC%25B0%25A8%255DJSP%252Bmodel2","DEFAULT_URL":"https://dev-juniors.tistory.com","USER":{"name":null,"homepage":null,"id":0,"profileImage":null},"SUBSCRIPTION":{"status":"none","isConnected":false,"isPending":false,"isWait":false,"isProcessing":false,"isNone":true},"IS_LOGIN":false,"HAS_BLOG":false,"IS_SUPPORT":false,"IS_SCRAPABLE":false,"TOP_URL":"http://www.tistory.com","JOIN_URL":"https://www.tistory.com/member/join","PHASE":"prod","ROLE_GROUP":"visitor"};
window.T.entryInfo = null;
window.appInfo = {"domain":"tistory.com","topUrl":"https://www.tistory.com","loginUrl":"https://www.tistory.com/auth/login","logoutUrl":"https://www.tistory.com/auth/logout"};
window.initData = {};
 
window.TistoryBlog = {
    basePath: "",
    tistoryUrl: "https://dev-juniors.tistory.com",
    token: "PtQf6jC7tQaydn/vGcnsyfI4our2U/p9Vm8jKUYjk47vrwyqgRw4Id3waqwksSql"
};
var servicePath = "";
var blogURL = "";</script>
 
                 
                 
                 
<meta charset="UTF-8">
<title>회원정보수정</title>
 
                <style type="text/css">.another_category {
    border: 1px solid #E5E5E5;
    padding: 10px 10px 5px;
    margin: 10px 0;
    clear: both;
}
 
.another_category h4 {
    font-size: 12px !important;
    margin: 0 !important;
    border-bottom: 1px solid #E5E5E5 !important;
    padding: 2px 0 6px !important;
}
 
.another_category h4 a {
    font-weight: bold !important;
}
 
.another_category table {
    table-layout: fixed;
    border-collapse: collapse;
    width: 100% !important;
    margin-top: 10px !important;
}
 
* html .another_category table {
    width: auto !important;
}
 
*:first-child + html .another_category table {
    width: auto !important;
}
 
.another_category th, .another_category td {
    padding: 0 0 4px !important;
}
 
.another_category th {
    text-align: left;
    font-size: 12px !important;
    font-weight: normal;
    word-break: break-all;
    overflow: hidden;
    line-height: 1.5;
}
 
.another_category td {
    text-align: right;
    width: 80px;
    font-size: 11px;
}
 
.another_category th a {
    font-weight: normal;
    text-decoration: none;
    border: none !important;
}
 
.another_category th a.current {
    font-weight: bold;
    text-decoration: none !important;
    border-bottom: 1px solid !important;
}
 
.another_category th span {
    font-weight: normal;
    text-decoration: none;
    font: 10px Tahoma, Sans-serif;
    border: none !important;
}
 
.another_category_color_gray, .another_category_color_gray h4 {
    border-color: #E5E5E5 !important;
}
 
.another_category_color_gray * {
    color: #909090 !important;
}
 
.another_category_color_gray th a.current {
    border-color: #909090 !important;
}
 
.another_category_color_gray h4, .another_category_color_gray h4 a {
    color: #737373 !important;
}
 
.another_category_color_red, .another_category_color_red h4 {
    border-color: #F6D4D3 !important;
}
 
.another_category_color_red * {
    color: #E86869 !important;
}
 
.another_category_color_red th a.current {
    border-color: #E86869 !important;
}
 
.another_category_color_red h4, .another_category_color_red h4 a {
    color: #ED0908 !important;
}
 
.another_category_color_green, .another_category_color_green h4 {
    border-color: #CCE7C8 !important;
}
 
.another_category_color_green * {
    color: #64C05B !important;
}
 
.another_category_color_green th a.current {
    border-color: #64C05B !important;
}
 
.another_category_color_green h4, .another_category_color_green h4 a {
    color: #3EA731 !important;
}
 
.another_category_color_blue, .another_category_color_blue h4 {
    border-color: #C8DAF2 !important;
}
 
.another_category_color_blue * {
    color: #477FD6 !important;
}
 
.another_category_color_blue th a.current {
    border-color: #477FD6 !important;
}
 
.another_category_color_blue h4, .another_category_color_blue h4 a {
    color: #1960CA !important;
}
 
.another_category_color_violet, .another_category_color_violet h4 {
    border-color: #E1CEEC !important;
}
 
.another_category_color_violet * {
    color: #9D64C5 !important;
}
 
.another_category_color_violet th a.current {
    border-color: #9D64C5 !important;
}
 
.another_category_color_violet h4, .another_category_color_violet h4 a {
    color: #7E2CB5 !important;
}
</style>
 
                 
<link rel="canonical" href="https://dev-juniors.tistory.com"/>
 
<!-- BEGIN STRUCTURED_DATA -->
<script type="application/ld+json">
    {"@context":"http://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":0,"item":{"@id":"https://dev-juniors.tistory.com/19","name":"Model2 Architecture - (6) 회원탈퇴"}},{"@type":"ListItem","position":1,"item":{"@id":"https://dev-juniors.tistory.com/18","name":"Model2 Architecture - (5) 회원수정"}},{"@type":"ListItem","position":2,"item":{"@id":"https://dev-juniors.tistory.com/13","name":"Model2 Architecture - (4) 로그아웃"}},{"@type":"ListItem","position":3,"item":{"@id":"https://dev-juniors.tistory.com/12","name":"Model2 Architecture - (3) 로그인"}},{"@type":"ListItem","position":4,"item":{"@id":"https://dev-juniors.tistory.com/11","name":"Model2 Architecture - (2) 회원조회"}},{"@type":"ListItem","position":5,"item":{"@id":"https://dev-juniors.tistory.com/10","name":"Model2 Architecture - (1) 회원가입"}},{"@type":"ListItem","position":6,"item":{"@id":"https://dev-juniors.tistory.com/9","name":"Model2 Architecture - table생성, 홈화면(index,dispatcher 등), error페이지"}}]}
</script>
<!-- END STRUCTURED_DATA -->
<link rel="stylesheet" type="text/css" href="//t1.daumcdn.net/tistory_admin/www/style/top/font.css"/>
 
                 
                </head>
<body>
<%
    ClientDTO clientDTO = (ClientDTO)session.getAttribute("clientDTO");
%>
<form action="DispatcherServlet" method="post">
    <input type="hidden" name="command" value="update">
    <table>      
        <tr>
            <td>아이디</td>
            <td><input type="text" name="id" value="<%=clientDTO.getClientId() %>" readonly></td>
        </tr>
        <tr>
            <td>비밀번호</td>
            <td><input type="text" name="password" value="<%=clientDTO.getClientPassword() %>" ></td>
        </tr>
        <tr>
            <td>이름</td>
            <td><input type="text" name="name" value="<%=clientDTO.getClientName() %>" ></td>
        </tr>
        <tr>
            <td>이메일</td>
            <td><input type="text" name="email" value="<%=clientDTO.getClientEmail() %>" ></td>
        </tr>
        <tr>
            <td>전화번호</td>
            <td><input type="text" name="phone" value="<%=clientDTO.getClientPhone() %>" ></td>
        </tr>
        <tr>
            <td>주소</td>
            <td><input type="text" name="address" value="<%=clientDTO.getClientAddress() %>" ></td>
        </tr>
        <tr rowspan="2">
        <td><input type="submit" value="정보 수정"/></td>
        </tr>
    </table>
    </form>
2)WebContent - update_result.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    <%@ page import = "model.ClientDTO" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
                                                <head>
                <script type="text/javascript">if (!window.T) { window.T = {} }
window.T.config = {"TOP_SSL_URL":"https://www.tistory.com","PREVIEW":false,"ROLE":"guest","PREV_PAGE":"","NEXT_PAGE":"","BLOG":{"id":3189920,"name":"dev-juniors","title":"개발자로가는길","isDormancy":true,"nickName":"서나브로","status":"open","profileStatus":"normal"},"NEED_COMMENT_LOGIN":false,"COMMENT_LOGIN_CONFIRM_MESSAGE":"","LOGIN_URL":"https://www.tistory.com/auth/login/?redirectUrl=https://dev-juniors.tistory.com/category/1.JAVA%2520Web%2520Programming/%255B3%25EC%25A3%25BC%25EC%25B0%25A8%255DJSP%252Bmodel2","DEFAULT_URL":"https://dev-juniors.tistory.com","USER":{"name":null,"homepage":null,"id":0,"profileImage":null},"SUBSCRIPTION":{"status":"none","isConnected":false,"isPending":false,"isWait":false,"isProcessing":false,"isNone":true},"IS_LOGIN":false,"HAS_BLOG":false,"IS_SUPPORT":false,"IS_SCRAPABLE":false,"TOP_URL":"http://www.tistory.com","JOIN_URL":"https://www.tistory.com/member/join","PHASE":"prod","ROLE_GROUP":"visitor"};
window.T.entryInfo = null;
window.appInfo = {"domain":"tistory.com","topUrl":"https://www.tistory.com","loginUrl":"https://www.tistory.com/auth/login","logoutUrl":"https://www.tistory.com/auth/logout"};
window.initData = {};
 
window.TistoryBlog = {
    basePath: "",
    tistoryUrl: "https://dev-juniors.tistory.com",
    token: "PtQf6jC7tQaydn/vGcnsyfI4our2U/p9Vm8jKUYjk47vrwyqgRw4Id3waqwksSql"
};
var servicePath = "";
var blogURL = "";</script>
 
                 
                 
                 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
 
                <style type="text/css">.another_category {
    border: 1px solid #E5E5E5;
    padding: 10px 10px 5px;
    margin: 10px 0;
    clear: both;
}
 
.another_category h4 {
    font-size: 12px !important;
    margin: 0 !important;
    border-bottom: 1px solid #E5E5E5 !important;
    padding: 2px 0 6px !important;
}
 
.another_category h4 a {
    font-weight: bold !important;
}
 
.another_category table {
    table-layout: fixed;
    border-collapse: collapse;
    width: 100% !important;
    margin-top: 10px !important;
}
 
* html .another_category table {
    width: auto !important;
}
 
*:first-child + html .another_category table {
    width: auto !important;
}
 
.another_category th, .another_category td {
    padding: 0 0 4px !important;
}
 
.another_category th {
    text-align: left;
    font-size: 12px !important;
    font-weight: normal;
    word-break: break-all;
    overflow: hidden;
    line-height: 1.5;
}
 
.another_category td {
    text-align: right;
    width: 80px;
    font-size: 11px;
}
 
.another_category th a {
    font-weight: normal;
    text-decoration: none;
    border: none !important;
}
 
.another_category th a.current {
    font-weight: bold;
    text-decoration: none !important;
    border-bottom: 1px solid !important;
}
 
.another_category th span {
    font-weight: normal;
    text-decoration: none;
    font: 10px Tahoma, Sans-serif;
    border: none !important;
}
 
.another_category_color_gray, .another_category_color_gray h4 {
    border-color: #E5E5E5 !important;
}
 
.another_category_color_gray * {
    color: #909090 !important;
}
 
.another_category_color_gray th a.current {
    border-color: #909090 !important;
}
 
.another_category_color_gray h4, .another_category_color_gray h4 a {
    color: #737373 !important;
}
 
.another_category_color_red, .another_category_color_red h4 {
    border-color: #F6D4D3 !important;
}
 
.another_category_color_red * {
    color: #E86869 !important;
}
 
.another_category_color_red th a.current {
    border-color: #E86869 !important;
}
 
.another_category_color_red h4, .another_category_color_red h4 a {
    color: #ED0908 !important;
}
 
.another_category_color_green, .another_category_color_green h4 {
    border-color: #CCE7C8 !important;
}
 
.another_category_color_green * {
    color: #64C05B !important;
}
 
.another_category_color_green th a.current {
    border-color: #64C05B !important;
}
 
.another_category_color_green h4, .another_category_color_green h4 a {
    color: #3EA731 !important;
}
 
.another_category_color_blue, .another_category_color_blue h4 {
    border-color: #C8DAF2 !important;
}
 
.another_category_color_blue * {
    color: #477FD6 !important;
}
 
.another_category_color_blue th a.current {
    border-color: #477FD6 !important;
}
 
.another_category_color_blue h4, .another_category_color_blue h4 a {
    color: #1960CA !important;
}
 
.another_category_color_violet, .another_category_color_violet h4 {
    border-color: #E1CEEC !important;
}
 
.another_category_color_violet * {
    color: #9D64C5 !important;
}
 
.another_category_color_violet th a.current {
    border-color: #9D64C5 !important;
}
 
.another_category_color_violet h4, .another_category_color_violet h4 a {
    color: #7E2CB5 !important;
}
</style>
 
                 
<link rel="canonical" href="https://dev-juniors.tistory.com"/>
 
<!-- BEGIN STRUCTURED_DATA -->
<script type="application/ld+json">
    {"@context":"http://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":0,"item":{"@id":"https://dev-juniors.tistory.com/19","name":"Model2 Architecture - (6) 회원탈퇴"}},{"@type":"ListItem","position":1,"item":{"@id":"https://dev-juniors.tistory.com/18","name":"Model2 Architecture - (5) 회원수정"}},{"@type":"ListItem","position":2,"item":{"@id":"https://dev-juniors.tistory.com/13","name":"Model2 Architecture - (4) 로그아웃"}},{"@type":"ListItem","position":3,"item":{"@id":"https://dev-juniors.tistory.com/12","name":"Model2 Architecture - (3) 로그인"}},{"@type":"ListItem","position":4,"item":{"@id":"https://dev-juniors.tistory.com/11","name":"Model2 Architecture - (2) 회원조회"}},{"@type":"ListItem","position":5,"item":{"@id":"https://dev-juniors.tistory.com/10","name":"Model2 Architecture - (1) 회원가입"}},{"@type":"ListItem","position":6,"item":{"@id":"https://dev-juniors.tistory.com/9","name":"Model2 Architecture - table생성, 홈화면(index,dispatcher 등), error페이지"}}]}
</script>
<!-- END STRUCTURED_DATA -->
<link rel="stylesheet" type="text/css" href="//t1.daumcdn.net/tistory_admin/www/style/top/font.css"/>
 
                 
                </head>
<body>
    <a href="index.jsp">HOME</a><br><br>
    <%
        ClientDTO updateDTO = (ClientDTO)session.getAttribute("updateDTO");
    %>
     
    아이디 : <%=updateDTO.getClientId() %><br>
    비밀번호 : <%=updateDTO.getClientPassword() %><br>
    이름 : <%=updateDTO.getClientName() %><br>
    주소 : <%=updateDTO.getClientAddress() %><br>
    전화번호 : <%=updateDTO.getClientPhone() %><br>
2. Controller 1)src = controller.HandlerMapping.java에 update 추가
1
2
if(command.equals("update")) {
       c = new UpdateController();
2)src - controller.UpdateController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  package controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import model.ClientDAO;
import model.ClientDTO;
 
public class UpdateController implements Controller{
 
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("enter to UpdateController()...");
        HttpSession session = request.getSession(false);
        ClientDTO dto = (ClientDTO) session.getAttribute("clientDTO");
        ClientDTO updateDTO = ClientDAO.getInstance().updateMemberInfo(dto);
 
        session.setAttribute("updateDTO", updateDTO);
        return "redirect:update_result.jsp";
    }
 
}
3. model 변경 1) src - model.ClientDAO.java에 updateMemberInfo 메소드 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public ClientDTO updateMemberInfo(ClientDTO clientDTO) throws SQLException {
     String ret = null;
     Connection con = null;
     PreparedStatement pstmt = null;
     ClientDTO updateDTO = null;
     try {
         con = getConnection();
         String sql = "update client set clientPassword=?, clientName=?, clientEmail=?,clientPhone=?,clientAddress=? where clientId=?";
         pstmt = con.prepareStatement(sql);
         pstmt.setString(1, clientDTO.getClientPassword());
         pstmt.setString(2, clientDTO.getClientName());
         pstmt.setString(3, clientDTO.getClientEmail());
         pstmt.setString(4, clientDTO.getClientPhone());
         pstmt.setString(5, clientDTO.getClientAddress());
         pstmt.setString(6, clientDTO.getClientId());
         System.out.println("update clientdto : " + clientDTO);
          
         int result = pstmt.executeUpdate();
 
         if (result > 0) {
             System.out.println("success to update");
             updateDTO = clientDTO;
         }else
             System.out.println("fail to update");
     } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     } finally {
         closeAll(pstmt, con);
     }
     return clientDTO;
 
 }

[실행화면]


1. 로그아웃 링크 연결

43
<td><a href="./DispatcherServlet?command=logout">로그아웃</a></td>

2. controller

1) src - controller.HandlerMapping.java에 logout 추가

21
22
else if(command.equals("logout"))
    c=new LogoutController();

2) src - controller.LogoutController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
public class LogoutController implements Controller {
 
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession(false);
        if(session!=null)
            session.invalidate();
        return "redirect:index.jsp";
    }
}

[로그인 폼]

[로그인 성공 폼]

[실행화면]

1. 로그인 폼

1) WebContent - index.jsp 변경(로그인/로그아웃에 따른 메뉴 변경)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="model.ClientDTO"%> 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>model2</title>
</head>
<body>
    <h3>Model2 Architecture</h3>
    <p>Model2 Controller(servlet)을 통해 Business로직과 view를 분리 </p>
    <img src="model2.gif"/>
    <h3>메뉴</h3>
    <p>Model2를 이용해 회원관리하고, 회원만 게시판을 이용할 수 있게함.</p>
    <!-- 아래의 링크(#)를 채워 나갑니다 -->
    <%
        ClientDTO clientDTO=(ClientDTO)session.getAttribute("clientDTO");
    %>
    <table border="1">
        <tr>
            <td rowspan="4">회원관리</td>
            <% if(clientDTO==null) {%>
            <td><a href="./register.jsp">회원등록</a></td><!-- 로그인 후에는 감추기-->
            <% }else{ %>
            <td><%=clientDTO.getClientName() %>님 환영합니다.</td><!-- 로그인 후에는 감추기-->
            <% } %>
        </tr>
        <tr>
            <td><a href="#">회원수정</a></td><!-- 로그인시 보이게, 로그아웃시에 안보이게 -->
        </tr>
        <tr>
            <td><a href="#">회원삭제</a></td><!-- 로그인시 보이게, 로그아웃시에 안보이게 -->
        </tr>
        <tr>
            <td><a href="./findClient.jsp">회원조회</a></td>
        </tr>
        <tr>
            <td rowspan="2">회원전용</td>
            <% if(clientDTO==null) {%>
            <td><a href="./login.jsp">로그인</a></td><!-- 로그인 후에는 로그아웃으로 변경 --> 
            <% }else{ %>
            <td><a href="#">로그아웃</a></td>
            <% } %>  
        </tr>
        <tr>
            <td><a href="#">게시판</a></td><!-- 로그인 안하면 alert -->           
        </tr>
    </table>
</body>
</html>

2) WebContent - login.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 로그인</title>
</head>
<body>
    <h3>회원 로그인</h3>
    <form method="post" action="DispatcherServlet">
        <input type="hidden" name="command" value="login">
        아이디 <input type="text" name="clientId">
        패스워드 <input type="password" name="clientPassword">
        <input type="submit" value="로그인">
    </form>
</body>
</html>

3) WebContent - login_ok.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="model.ClientDTO"%>   
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인 성공</title>
</head>
<body>
    <%
        ClientDTO clientDTO=(ClientDTO)session.getAttribute("clientDTO");
    %>
    <script>
        alert("<%=clientDTO.getClientName() %>"+"님 로그인에 성공하셨습니다.");
        location.href="http://localhost:8080/webstudy5-model2/";
    </script>
</body>
</html>

4) WebContent - login_fail.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("utf-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인 실패</title>
</head>
<body>
    <script>
        alert("아이디와 패스워드가 맞지 않아 로그인에 실패하셨습니다.");
        location.href="http://localhost:8080/webstudy5-model2/";
    </script>
</body>
</html>

2. controller 

1) src - controller.HandlerMapping.java에 login추가

19
20
else if(command.equals("login"))
    c=new LoginController();

2) src - controller.LoginController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import model.ClientDAO;
import model.ClientDTO;
 
public class LoginController implements Controller {
 
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String clientId=request.getParameter("clientId");
        String clientPassword=request.getParameter("clientPassword");
        ClientDTO clientDTO=ClientDAO.getInstance().login(new ClientDTO(clientId,clientPassword,null,null,null,null));
        String url =null;
        if(clientDTO!=null) {
            //로그인 성공시
            HttpSession session=request.getSession();
            session.setAttribute("clientDTO", clientDTO);
            url="login_ok.jsp";
        }else {
            //로그인 실패시
            url="login_fail.jsp";
        }
        return "redirect:"+url;
    }
}

3. model

1) src - model.ClientDAO.java에 login메소드 추가

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public ClientDTO login(ClientDTO clientDTO) throws SQLException {
    ClientDTO resultDTO=null;
    Connection con=null;
    PreparedStatement pstmt=null;
    ResultSet rs=null;
    try {
        con=getConnection();
        String sql="select clientId,clientName from client where clientId=? and clientPassword=?";
        pstmt=con.prepareStatement(sql);
        pstmt.setString(1, clientDTO.getClientId());
        pstmt.setString(2, clientDTO.getClientPassword());
        rs=pstmt.executeQuery();
        if(rs.next())
            resultDTO=new ClientDTO(rs.getString(1), null, rs.getString(2), null, null, null);
    }finally {
        closeAll(rs, pstmt, con);
    }
    return resultDTO;
}

[회원검색 폼]

[검색결과 폼]

[실행화면]

1. 회원조회 폼

1) WebContent - index.jsp에 가입 url 기입

28
<td><a href="./findClient.jsp">회원조회</a></td>

2) WebContent - findClient.jsp (검색 페이지)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원검색</title>
</head>
<body>
    <h3>회원검색</h3>
    <form method="post" action="DispatcherServlet">
        <input type="hidden" name="command" value="findClient">
        검색어 입력  <input type="text" name="keyword">
        <input type="submit" value="검색">
    </form>
</body>
</html>

3) WebContent - findClient_result.jsp (검색결과 확인 페이지)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="model.ClientDTO"%>   
<%@page import= "java.util.ArrayList"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
table, td, th { 
  border: 1px solid #ddd;
  text-align: left;
}
 
table {
  border-collapse: collapse;
  width: 100%;
}
 
th, td {
  padding: 15px;
}
</style>
</head>
<body>
<h3><span style="color:blue">"<%=request.getParameter("keyword") %>"</span> 검색결과</h3>
<%
    ArrayList<ClientDTO> clientList=(ArrayList<ClientDTO>)request.getAttribute("clientList");
%>
 
<%
    if(clientList.size()==0){
%>
<script>
    alert("검색 결과가 없습니다.");
</script>
<%
    }
%>
<table>
    <tr>
        <th>아이디</th>
        <th>이름</th>
        <th>이메일</th>
        <th>휴대번호</th>
        <th>주소</th>
    </tr>
<%
    for(int i=0;i<clientList.size();i++){
        ClientDTO client=clientList.get(i);
%>
    <tr>
        <td><%=client.getClientId() %></td>
        <td><%=client.getClientName() %></td>
        <td><%=client.getClientEmail() %></td>
        <td><%=client.getClientPhone() %></td>
        <td><%=client.getClientAddress() %></td>
    </tr>
<%
    }
%>  
</table>
</body>
</html>

2. controller 변경

1) src - controller.HandlerMapping.java에 findClient추가

17
18
else if(command.equals("findClient"))
        c=new FindClientController();

2) src - controller.FindClientControlle.java 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package controller;
 
import java.util.ArrayList;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import model.ClientDAO;
import model.ClientDTO;
 
public class FindClientController implements Controller {
 
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String keyword=request.getParameter("keyword");
        ArrayList<ClientDTO> clientList=ClientDAO.getInstance().findClient(keyword);
        request.setAttribute("clientList", clientList);
        return "findClient_result.jsp";
    }
}

3. model 변경

1) src - model.ClientDAO.java에 findclient 메소드 추가

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public ArrayList<ClientDTO> findClient(String keyword) throws SQLException{
    ArrayList<ClientDTO> clientList=new ArrayList<ClientDTO>();
    Connection con=null;
    PreparedStatement pstmt=null;
    ResultSet rs=null;
    try {
        con=getConnection();
        String sql="SELECT * FROM CLIENT WHERE clientId LIKE ? OR clientName LIKE ? OR clientEmail LIKE ? OR ClientAddress LIKE ?";
        pstmt=con.prepareStatement(sql);
        pstmt.setString(1, "%"+keyword+"%");
        pstmt.setString(2, "%"+keyword+"%");
        pstmt.setString(3, "%"+keyword+"%");
        pstmt.setString(4, "%"+keyword+"%");
        rs=pstmt.executeQuery();
        while(rs.next()) {
            ClientDTO client=new ClientDTO(rs.getString(1),null,rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6));
            clientList.add(client);
        }
    }finally {
        closeAll(rs,pstmt,con);
    }
    return clientList;
}

[회원가입 폼]

[실행화면]

1. 회원가입 폼

1) WebContent - index.jsp에 가입 url 기입

19
<td><a href="./register.jsp">회원등록</a></td><!-- 로그인 후에는 감추기-->

2) WebContent - register.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원가입</title>
<style>
table {
  border-collapse: collapse;
  width: 100%;
}
 
th, td {
  text-align: left;
  padding: 8px;
}
 
tr:nth-child(even) {background-color: #f2f2f2;}
input[type=submit] {
  width: 20%;
  background-color: #4CAF50;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
   
}
 
input[type=submit]:hover {
  background-color: #45a049;
}
</style>
</head>
<body>
    <h3>회원가입</h3>
    <form method="post" action="DispatcherServlet">
        <input type="hidden" name="command" value="register">
        <table>
            <tr>
                <td>아이디</td><td><input type="text" name="clientId"></td>
            </tr>
            <tr>
                <td>패스워드</td><td><input type="password" name="clientPassword"></td>
            </tr>
            <tr>
                <td>이름</td><td><input type="text" name="clientName"></td>
            </tr>
            <tr>
                <td>이메일</td><td><input type="email" name="clientEmail"></td>
            </tr>
            <tr>
                <td>휴대폰</td><td><input type="tel" name="clientPhone"></td>
            </tr>
            <tr>
                <td>주소</td><td><input type="text" name="clientAddress"></td>
            </tr>
            <tr>
                <td colspan="2" ><input type="submit" value="등록"></td>
            </tr>
        </table>
    </form>
</body>
</html>

3) WebContent - register_result.jsp (회원가입 성공시 등록 확인 페이지)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원등록 성공</title>
</head>
<body>
    <script>
        alert("등록이 완료되었습니다.");
        location.href="http://localhost:8080/webstudy5-model2/";
    </script>
</body>
</html>

2. controller 변경

1) src - controller.HandlerMapping.java에 register 추가

13
14
if(command.equals("register"))
        c=new RegisterController();

2) src - controller.RegisterController.java 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import model.ClientDAO;
import model.ClientDTO;
 
public class RegisterController implements Controller {
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String clientId=request.getParameter("clientId");
        String clientPassword=request.getParameter("clientPassword");
        String clientName=request.getParameter("clientName");
        String clientEmail=request.getParameter("clientEmail");
        String clientPhone=request.getParameter("clientPhone");
        String clientAddress=request.getParameter("clientAddress");
        ClientDAO.getInstance().register(new ClientDTO(clientId, clientPassword, clientName, clientEmail, clientPhone, clientAddress));
        return "redirect:register_result.jsp";
    }
}

3. model 변경

1) src - model.ClientDAO.java에 register 메소드 추가

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public void register(ClientDTO clientDTO) throws SQLException{
    Connection con=null;
    PreparedStatement pstmt=null;
    try {
        con=getConnection();
        String sql="insert into client(clientId, clientPassword, clientName, clientEmail, clientPhone, clientAddress) values(?,?,?,?,?,?)";
        pstmt=con.prepareStatement(sql);
        pstmt.setString(1,clientDTO.getClientId());
        pstmt.setString(2,clientDTO.getClientPassword());
        pstmt.setString(3,clientDTO.getClientName());
        pstmt.setString(4,clientDTO.getClientEmail());
        pstmt.setString(5,clientDTO.getClientPhone());
        pstmt.setString(6,clientDTO.getClientAddress());
        pstmt.execute();
    }finally {
        closeAll(pstmt,con);
    }
}

- 회원 등록/수정/삭제/조회가 가능한 웹서비스 

1. study_db - client table 생성

1
2
3
4
5
6
7
8
9
  create table client(
   clientId varchar(50) not null,
   clientPassword varchar(100) not null,
   clientName varchar(30) not null,
   clientEmail varchar(100) not null,
   clientPhone varchar(100),
   clientAddress varchar(100),
   primary key(clientId)
)

 

2. 홈화면

   1)  WebContent - index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>model2</title>
</head>
<body>
    <h3>Model2 Architecture</h3>
    <p>Model2 Controller(servlet)을 통해 Business로직과 view를 분리 </p>
    <img src="model2.gif"/>
    <h3>메뉴</h3>
    <p>Model2를 이용해 회원관리하고, 회원만 게시판을 이용할 수 있게함.</p>
    <!-- 아래의 링크(#)를 채워 나갑니다 -->
    <table border="1">
        <tr>
            <td rowspan="4">회원관리</td>
            <td><a href="#">회원등록</a></td><!-- 로그인 후에는 감추기-->
        </tr>
        <tr>
            <td><a href="#">회원수정</a></td><!-- 로그인시 보이게, 로그아웃시에 안보이게 -->
        </tr>
        <tr>
            <td><a href="#">회원삭제</a></td><!-- 로그인시 보이게, 로그아웃시에 안보이게 -->
        </tr>
        <tr>
            <td><a href="#">회원조회</a></td>
        </tr>
        <tr>
            <td rowspan="2">회원전용</td>
            <td><a href="#">로그인</a></td><!-- 로그인 후에는 로그아웃으로 변경 -->           
        </tr>
        <tr>
            <td><a href="#">게시판</a></td><!-- 로그인 안하면 alert -->           
        </tr>
    </table>
</body>
</html>

- 12번째 줄 이미지

[이미지출처]http://blog.naver.com/PostView.nhn?blogId=kimstcool01&logNo=220908881839&parentCategoryNo=&categoryNo=27&viewDate=&isShowPopularPosts=true&from=search

-15번째 줄부터 채워나가면 됨.

3. Controller 

   2) src - loader.DbDriverLoader.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package loader;
 
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
 
@WebListener
public class DbDriverLoader implements ServletContextListener {
 
    public DbDriverLoader() {
    }
 
    public void contextDestroyed(ServletContextEvent arg0) {
    }
 
    public void contextInitialized(ServletContextEvent arg0) {
        try {
            Class.forName("org.mariadb.jdbc.Driver");
            System.out.println("db driver loading...");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

   3) src - controller.DispatcherServlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package controller;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@WebServlet("/DispatcherServlet")
public class DispatcherServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    public DispatcherServlet() {
        super();
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        handleRequest(request, response);
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        handleRequest(request, response);
    }
     
     
    private void handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        try {
            // 클라이언트의 요청(command)를 확인 -> HandlerMapping을 통해 구현체를 반환받아 실행
            String command=request.getParameter("command");
            // 리턴 정보는 1.이동할 view와 2.이동방식(forward/redirect)
            String url=HandlerMapping.getInstance().create(command).execute(request,response);
            // 리턴 방식에 따라 클라이언트에게 이동되도록 처리
            if(url.trim().startsWith("redirect:"))
                response.sendRedirect(url.trim().substring(9));
            else
                request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception e) {
            //Exception 발생 시 error.jsp 페이지로 응답 처리
            e.printStackTrace();
            response.sendRedirect("error.jsp");
        }
    }
 
}

   4) src - controller.HandlerMapping.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package controller;
/*
 * 컨트롤러 구현체 생성을 전담하는 클래스
 * DispatcherServlet과 Controller 계열 구현체들과 결합도를 낮춤
*/public class HandlerMapping {
    private static HandlerMapping instance=new HandlerMapping();
    private HandlerMapping() {}
    public static HandlerMapping getInstance() {
        return instance;
    }
    public Controller create(String command) {
        Controller c=null;
        //여기서부터 처리할 command와 실제 Controller Mapping
        return c;
    }
}

   5) src - controller.Controller.java

1
2
3
4
5
6
7
8
9
10
11
12
package controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/*
 * 실제 컨트롤러 업무를 수행하는 구상클래스(구현체)들이 implements 하는 인터페이스
 * DispatcherServlet은 실제 컨트롤러 구현체의 메소드를 확인하지 않아도 상위 Controller 인터페이스만 보고 사용 가능.
*/
public interface Controller {
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception;
}

4. Model

   1) src - model.ClientDAO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package model;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class ClientDAO {
    private static ClientDAO instance=new ClientDAO();
    private ClientDAO() {}
    public static ClientDAO getInstance() {
        return instance;
    }
    public Connection getConnection() throws SQLException{
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/study_db","study_user","user");
    }
    public void closeAll(PreparedStatement pstmt,Connection con) throws SQLException{
        if(pstmt!=null)
            pstmt.close();
        if(con!=null)
            con.close();
    }
    public void closeAll(ResultSet rs,PreparedStatement pstmt,Connection con) throws SQLException{
        if(rs!=null)
            rs.close();
        closeAll(pstmt, con);
    }
    //여기서부터 필요한 메소드 추가
}

   2) src - model.ClientDTO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package model;
 
public class ClientDTO {
    private String clientId;
    private String clientPassword;
    private String clientName;
    private String clientEmail;
    private String clientPhone;
    private String clientAddress;
    public ClientDTO() {
        super();
    }
    public ClientDTO(String clientId, String clientPassword, String clientName, String clientEmail, String clientPhone,
            String clientAddress) {
        super();
        this.clientId = clientId;
        this.clientPassword = clientPassword;
        this.clientName = clientName;
        this.clientEmail = clientEmail;
        this.clientPhone = clientPhone;
        this.clientAddress = clientAddress;
    }
    public String getClientId() {
        return clientId;
    }
    public void setClientId(String clientId) {
        this.clientId = clientId;
    }
    public String getClientPassword() {
        return clientPassword;
    }
    public void setClientPassword(String clientPassword) {
        this.clientPassword = clientPassword;
    }
    public String getClientName() {
        return clientName;
    }
    public void setClientName(String clientName) {
        this.clientName = clientName;
    }
    public String getClientEmail() {
        return clientEmail;
    }
    public void setClientEmail(String clientEmail) {
        this.clientEmail = clientEmail;
    }
    public String getClientPhone() {
        return clientPhone;
    }
    public void setClientPhone(String clientPhone) {
        this.clientPhone = clientPhone;
    }
    public String getClientAddress() {
        return clientAddress;
    }
    public void setClientAddress(String clientAddress) {
        this.clientAddress = clientAddress;
    }
    @Override
    public String toString() {
        return "ClientDTO [clientId=" + clientId + ", clientPassword=" + clientPassword + ", clientName=" + clientName
                + ", clientEmail=" + clientEmail + ", clientPhone=" + clientPhone + ", clientAddress=" + clientAddress
                + "]";
    }  
}

5. 에러페이지

  1) WebContent - error.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <img src="return.jpeg" /><br>
    <p>Console 확인 <a href="index.jsp">돌아가기</a></p
</body>
</html>

- 10째 줄 이미지 

[이미지 출처] https://namu.wiki/w/%EC%95%88%20%EB%8F%BC%20%EC%95%88%20%EB%B0%94%EA%BF%94%EC%A4%98%20%EB%B0%94%EA%BF%80%20%EC%83%9D%EA%B0%81%20%EC%97%86%EC%96%B4%20%EB%B9%A8%EB%A6%AC%20%EB%8F%8C%EC%95%84%EA%B0%80

 

[참고] 모델2 개념 참고 https://thatisgood.tistory.com/entry/model1-vs-model2-%EC%B0%A8%EC%9D%B4%EC%A0%90

 

+ Recent posts