반응형

 

이전 글 보러가기 ↓↓↓↓↓

https://maxtime1004.tistory.com/76

 

[JSP] MVC를 이용한 회원관리(조회) - 1

다음 글 보러가기 ↓↓↓↓↓ https://maxtime1004.tistory.com/77 <%@ taglib prefix="fmt" uri="http://jav.." data-og-host="maxtime1004.tistory.com" data-og-source-url="https://maxtime1004.tistory.com/7..

maxtime1004.tistory.com

 

다음 글 보러가기 ↓↓↓↓↓

https://maxtime1004.tistory.com/78

 

[JSP] MVC를 이용한 회원관리(수정/삭제) - 3

이전 글 보러가기 ↓↓↓↓↓ https://maxtime1004.tistory.com/77 [JSP] MVC를 이용한 회원관리(추가/회원가입) - 2 이전 글 보러가기 ↓↓↓↓↓ https://maxtime1004.tistory.com/76 [JSP] MVC를 이용한 회원..

maxtime1004.tistory.com


 

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
<%@ 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>
<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>
        </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>
                    </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
<%@ 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">&nbsp;</p>
            </th>
            <td>
            <input type="submit" value="가입하기">
            <input type="reset" value="다시입력">
            </td>
        </tr>
    </table>
</form>
</body>
</html>
cs

 

 

MemberVO

 

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
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");
                System.out.println(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();
        }
    }
}
 
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
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 = "/test02/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")) {
            System.out.println("여기");
            nextPage = "/test02/memberForm.jsp";
        }else {
            List<MemberVO> membersList = memberDAO.listMembers();
            request.setAttribute("membersList", membersList);
            nextPage = "/test02/listMembers.jsp";
        }
        RequestDispatcher dispatch = request.getRequestDispatcher(nextPage);
        dispatch.forward(request, response);
        
    }
 
}
 
cs

 

 

실행화면

회원 정보 테이블이 나타난다. 회원 가입하기를 클릭한다.

 

 

회원 정보를 입력하고 가입하기 버튼을 클릭하면 다시 이전 화면으로 돌아와 회원 테이블을 보여준다.

 

 

 

반응형

+ Recent posts