반응형
이전 글 보러가기 ↓↓↓↓↓
https://maxtime1004.tistory.com/77
listMembers.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
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
import=" java.util.*,sec01.ex01.*"
pageEncoding="UTF-8"
isELIgnored="false"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
request.setCharacterEncoding("utf-8");
%>
<c:set var="contextPath" value="${pageContext.request.contextPath }"/>
<!DOCTYPE html>
<html>
<head>
<c:choose>
<c:when test='${msg=="addMember" }'>
<script>
window.onload = function(){
alert("회원을 등록했습니다.");
}
</script>
</c:when>
<c:when test='${msg=="modified" }'>
<script>
window.onload = function(){
alert("회원 정보를 수정했습니다.");
}
</script>
</c:when>
<c:when test='${msg=="deleted"}'>
<script>
window.onload = function(){
alert("회원 정보를 삭제했습니다.");
}
</script>
</c:when>
</c:choose>
<style type="text/css">
table{
text-align: center;
border : 2px solid gray;
}
tr{
text-align: center;
border : 2px solid gray;
}
tr th{
border : 1px solid gray;
background-color: lightgreen;
}
td{
border : 1px solid gray;
width: 7%;
}
.cls1{
font-size: 40px;
text-align: center;
}
</style>
<meta charset="UTF-8">
<title>회원 정보 출력창</title>
</head>
<body>
<p class="cls1">회원정보</p>
<table>
<tr>
<th><b>아이디</b></th>
<th><b>비밀번호</b></th>
<th><b>이름</b></th>
<th><b>이메일</b></th>
<th><b>가입일</b></th>
<th><b>수정</b></th>
<th><b>삭제</b></th>
</tr>
<c:choose>
<c:when test="${empty membersList}">
<tr>
<td colspan="5">
<b>등록된 회원이 없습니다.</b>
</td>
</tr>
</c:when>
<c:when test="${!empty membersList}">
<c:forEach var="mem" items="${membersList }">
<tr>
<td>${mem.id }</td>
<td>${mem.pwd }</td>
<td>${mem.name }</td>
<td>${mem.email }</td>
<td>${mem.joinDate }</td>
<td><a href="${contextPath}/member/modMemberForm.do?id=${mem.id}">수정</a></td>
<td><a href="${contextPath}/member/delMember.do?id=${mem.id}">삭제</a></td>
</tr>
</c:forEach>
</c:when>
</c:choose>
</table>
<br>
<a href="${contextPath}/member/memberForm.do">회원 가입하기</a>
</body>
</html>
|
cs |
memberForm.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
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath }"/>
<!DOCTYPE html>
<html>
<head>
<style>
h1{
text-align: center;
}
table{
text-align: center;
}
th{
width: 200px;
}
td{
width: 400px;
}
</style>
<meta charset="UTF-8">
<title>회원가입창</title>
</head>
<body>
<form method="post" action="${contextPath}/member/addMember.do">
<h1>회원가입창</h1>
<table>
<tr>
<th>
<p align="right">아이디</p>
</th>
<td><input type="text" name="id"></td>
</tr>
<tr>
<th>
<p align="right">비밀번호</p>
</th>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<th>
<p align="right">이름</p>
</th>
<td><input type="text" name="name"></td>
</tr>
<tr>
<th>
<p align="right">이메일</p>
</th>
<td><input type="text" name="email"></td>
</tr>
<tr>
<th>
<p align="right"> </p>
</th>
<td>
<input type="submit" value="가입하기">
<input type="reset" value="다시입력">
</td>
</tr>
</table>
</form>
</body>
</html>
|
cs |
modMemberForm.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
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
isELIgnored="false"
%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="contextPath" value="${pageContext.request.contextPath }"/>
<%
request.setCharacterEncoding("utf-8");
%>
<!DOCTYPE html>
<html>
<head>
<style>
h1{
text-align: center;
}
table{
text-align: center;
}
th{
width: 200px;
}
td{
width: 400px;
}
.cls1{
font-size: 40px;
text-align: center;
}
</style>
<meta charset="UTF-8">
<title>회원정보 수정창</title>
</head>
<body>
<h1 class="cls1">회원 정보 수정창</h1>
<form method="post" action="${contextPath}/member/modMember.do?id=${memInfo.id}">
<table>
<tr>
<th>
<p align="right">아이디</p>
</th>
<td><input type="text" name="id" value="${memInfo.id}" disabled></td>
</tr>
<tr>
<th>
<p align="right">비밀번호</p>
</th>
<td><input type="password" name="pwd" value="${memInfo.pwd}" value="${memInfo.pwd }"></td>
</tr>
<tr>
<th>
<p align="right">이름</p>
</th>
<td><input type="text" name="name" value="${memInfo.name}"></td>
</tr>
<tr>
<th>
<p align="right">이메일</p>
</th>
<td><input type="text" name="email" value="${memInfo.email}"></td>
</tr>
<tr>
<th>
<p align="right">가입일</p>
</th>
<td><input type="text" name="joinDate" value="${memInfo.joinDate}" disabled></td>
</tr>
<tr align="center">
<td></td>
<td><input type="submit" value="수정하기"></td>
</tr>
<tr align="center">
<td></td>
<td ><input type="reset" value="다시입력"></td>
</tr>
</table>
</form>
</body>
</html>
|
cs |
MemberVO.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package sec01.ex01;
import java.util.Date;
public class MemberVO {
private String id;
private String pwd;
private String name;
private String email;
private Date joinDate;
public MemberVO(String id, String pwd, String name, String email) {
this.id = id;
this.pwd = pwd;
this.name = name;
this.email = email;
}
public MemberVO(String id, String pwd, String name, String email, Date joinDate) {
this.id = id;
this.pwd = pwd;
this.name = name;
this.email = email;
this.joinDate = joinDate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
}
|
cs |
MemberDAO
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
|
package sec01.ex01;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class MemberDAO {
private DataSource dataFactory;
private Connection conn;
private PreparedStatement pstmt;
public MemberDAO() {
try {
Context ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:/comp/env");
dataFactory = (DataSource) envContext.lookup("jdbc/oracle");
}catch(Exception e) {
e.printStackTrace();
}
}
public List<MemberVO> listMembers(){
List<MemberVO> membersList = new ArrayList<MemberVO>();
try {
conn = dataFactory.getConnection();
String query = "select * from t_member order by joinDate desc";
System.out.println(query);
pstmt = conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
String id = rs.getString("id");
String pwd = rs.getString("pwd");
String name = rs.getString("name");
String email = rs.getString("email");
Date joinDate = rs.getDate("joinDate");
MemberVO memberVO = new MemberVO(id, pwd, name, email, joinDate);
membersList.add(memberVO);
}
rs.close();
pstmt.close();
conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
return membersList;
}
public void addMember(MemberVO m) {
try {
conn = dataFactory.getConnection();
String id = m.getId();
String pwd = m.getPwd();
String name = m.getName();
String email = m.getEmail();
String query = "insert into t_member(id, pwd, name, email)"
+ " values(?, ?, ?, ?)";
System.out.println(query);
pstmt = conn.prepareStatement(query);
pstmt.setString(1, id);
pstmt.setString(2, pwd);
pstmt.setString(3, name);
pstmt.setString(4, email);
pstmt.executeUpdate();
pstmt.close();
conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
public MemberVO findMember(String _id) {
MemberVO memInfo = null;
try {
conn = dataFactory.getConnection();
String query = "select * from t_member where id=?";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, _id);
System.out.println(query);
ResultSet rs = pstmt.executeQuery();
rs.next();
String id = rs.getString("id");
String pwd = rs.getString("pwd");
String name = rs.getString("name");
String email = rs.getString("email");
Date joinDate = rs.getDate("joinDate");
memInfo = new MemberVO(id, pwd, name, email, joinDate);
pstmt.close();
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
return memInfo;
}
public void modMember(MemberVO memberVO) {
String id = memberVO.getId();
String pwd = memberVO.getPwd();
String name = memberVO.getName();
String email = memberVO.getEmail();
try {
conn = dataFactory.getConnection();
String query = "update t_member" +
" set pwd=?, name=?, email=?" +
" where id=?";
System.out.println(query);
pstmt = conn.prepareStatement(query);
pstmt.setString(1, pwd);
pstmt.setString(2, name);
pstmt.setString(3, email);
pstmt.setString(4, id);
pstmt.executeUpdate();
pstmt.close();
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public void delMember(String id) {
try {
conn = dataFactory.getConnection();
String query = "delete from t_member" +
" where id=?";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, id);
pstmt.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}
}
}
|
cs |
MemberController
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
|
package sec01.ex01;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MemberController
*/
@WebServlet("/member/*") // /mem.do -> /member/*
public class MemberController extends HttpServlet {
private static final long serialVersionUID = 1L;
MemberDAO memberDAO;
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
memberDAO = new MemberDAO();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doHandle(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doHandle(request,response);
}
//변경
private void doHandle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String nextPage = null; //변경
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String action = request.getPathInfo();
System.out.println("action : " + action);
if(action == null || action.equals("/listMembers.do")) {
List<MemberVO> membersList = memberDAO.listMembers();
request.setAttribute("membersList", membersList);
nextPage = "/test03/listMembers.jsp";
}else if(action.equals("/addMember.do")) {
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
String name = request.getParameter("name");
String email = request.getParameter("email");
MemberVO memberVO = new MemberVO(id,pwd,name,email);
memberDAO.addMember(memberVO);
nextPage="/member/listMembers.do";
}else if(action.equals("/memberForm.do")) {
String id = request.getParameter("id");
MemberVO memInfo = memberDAO.findMember(id);
request.setAttribute("memInfo", memInfo);
nextPage = "/test03/memberForm.jsp";
}else if(action.equals("/modMemberForm.do")){
String id = request.getParameter("id");
MemberVO memInfo = memberDAO.findMember(id);
request.setAttribute("memInfo", memInfo);
nextPage = "/test03/modMemberForm.jsp";
}else if(action.equals("/modMember.do")){
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
String name = request.getParameter("name");
String email = request.getParameter("email");
MemberVO memberVO = new MemberVO(id, pwd, name, email);
memberDAO.modMember(memberVO);
request.setAttribute("msg", "modified");
nextPage = "/member/listMembers.do";
}else if(action.equals("/delMember.do")){
String id = request.getParameter("id");
memberDAO.delMember(id);
request.setAttribute("msg", "deleted");
nextPage = "/member/listMembers.do";
}else {
List<MemberVO> membersList = memberDAO.listMembers();
request.setAttribute("membersList", membersList);
nextPage = "/test03/listMembers.jsp";
}
RequestDispatcher dispatch = request.getRequestDispatcher(nextPage);
dispatch.forward(request, response);
}
}
|
cs |
실행화면
회원 목록 페이지로 이동해 수정 버튼을 클릭합니다.
회원 정보를 수정한 뒤 수정하기 버튼을 클릭합니다.
정보가 수정된 것을 확인할 수 있습니다. 이번엔 삭제 버튼을 클릭합니다.
회원 정보를 삭제했다는 알림창이 뜨면 확인을 클릭합니다.
목록에서 해당 정보가 삭제된 것을 확인할 수 있습니다.
반응형
'코딩 기록 > JSP' 카테고리의 다른 글
[JSP] @RestController 문자열 전달하기 (0) | 2021.05.27 |
---|---|
[JSP] REST 란? (0) | 2021.05.27 |
[JSP] MVC를 이용한 회원관리(추가/회원가입) - 2 (0) | 2021.05.24 |
[JSP] MVC를 이용한 회원관리(조회) - 1 (0) | 2021.05.24 |
[JSP] JSTL 문자열 처리 함수 (0) | 2021.05.17 |