19.05.24 회원가입 (model 2 동영상 11강)

Back-End/JSP 2019. 5. 24. 12:03

모든 회원 보기 페이지 구현




MemberlistCon.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
package control;
 
import java.io.IOException;
import java.util.Vector;
 
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import model.MemberBean;
import model.MemberDAO;
 
//Servlet과 매핑될 URL 패턴을 지정한다.
//사용자를 화면에 출력하는 페이지
@WebServlet("/MemberlistCon.do")
public class MemberlistCon extends HttpServlet {
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        reqPro(request, response);
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        reqPro(request, response);
    }
 
    protected void reqPro(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        // 데이터 베이스에 연결하여 회원의 모든 정보를 리턴
        MemberDAO mdao = new MemberDAO();
        // 유동적이기 때문에 벡터 타입을 사용한다.
        Vector<MemberBean> v = mdao.getAllMember();
 
        request.setAttribute("v", v); // 벡터를 jsp 쪽으로 넘겨준다.
        System.out.println(v);
 
        // Dispatcher 방식은 클라이언트가 요청하면서 전송한 데이터를 그대로 유지한다.
        RequestDispatcher dis = request.getRequestDispatcher("MemberList.jsp");
        dis.forward(request, response);
    }
 
}
 
cs



MemberList.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
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
 
    <center>
        <h2>모든 회원 보기</h2>
        <table width="800" border="1" bordercolor="gray">
            <tr height="40">
                <td align="center" width="50">아이디</td>
                <td align="center" width="200">이메일</td>
                <td align="center" width="150">전화</td>
                <td align="center" width="150">취미</td>
                <td align="center" width="150">직업</td>
                <td align="center" width="100">나이</td>
            </tr>
            <!-- forEach문을 돌려서 빈에 저장된 값을 차례대로 출력한다. -->
            <!-- c태그를 사용하였으므로 $를 사용하여 출력한다. -->
 
            <c:forEach var="bean" items="${v }">
 
                <tr height="40">
                    <td align="center" width="50">${bean.id }</td>
                    <td align="center" width="200"><a href="#">${bean.email }</a>
                    </td>
                    <td align="center" width="150">${bean.tel }</td>
                    <td align="center" width="150">${bean.hobby }</td>
                    <td align="center" width="150">${bean.job }</td>
                    <td align="center" width="100">${bean.age }</td>
                </tr>
            </c:forEach>
        </table>
    </center>
</body>
</html>
cs



MemberDAO.java (DB연결) 

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
package model;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
 
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
 
public class MemberDAO {
 
    Connection con;
    PreparedStatement pstmt;
    ResultSet rs;
 
    public void getCon() {
        try {
 
            // 데이터베이스 연결 코드
            Context initctx = new InitialContext();
            Context envctx = (Context) initctx.lookup("java:comp/env");
            DataSource ds = (DataSource) envctx.lookup("jdbc/pool");
            con = ds.getConnection();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
    // 회원 한사람에 대한 정보를 저장하는 메소드
    public void insertMember(MemberBean bean) {
        getCon();
 
        try {
 
            // 쿼리 준비하기
            String sql = "insert into member values (?,?,?,?,?,?,?,?) ";
 
            // 쿼리 실행할 객체 선언
            pstmt = con.prepareStatement(sql);
 
            // ?에 값을 대입시켜준다.
            pstmt.setString(1, bean.getId());
            pstmt.setString(2, bean.getPass1());
            pstmt.setString(3, bean.getEmail());
            pstmt.setString(4, bean.getTel());
            pstmt.setString(5, bean.getHobby());
            pstmt.setString(6, bean.getJob());
            pstmt.setString(7, bean.getAge());
            pstmt.setString(8, bean.getInfo());
 
            // 쿼리 실행
            pstmt.executeUpdate();
            con.close();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
    // 모든 회원의 정보를 리턴하는 메소드
    public Vector<MemberBean> getAllMember() {
 
        // 리턴타입을 먼저 선언(객체를 먼저 생성)한다.
        Vector<MemberBean> v = new Vector<>();
 
        getCon();
 
        try {
            // 쿼리 준비
            String sql = "select * from member";
            // 쿼리 객체 준비
            pstmt = con.prepareStatement(sql);
            // 쿼리 실행후 결과를 리턴한다.
            rs = pstmt.executeQuery();
            // 반복문을 돌면서 회원의 정보를 저장한다.
            // 저장을 하려면 빈 클래스안에 빈공간이 있어야 하기 때문에 객체를 생성한다.
            while (rs.next()) {
                MemberBean bean = new MemberBean();
                bean.setId(rs.getString(1));
                bean.setPass1(rs.getString(2));
                bean.setEmail(rs.getString(3));
                bean.setTel(rs.getString(4));
                bean.setHobby(rs.getString(5));
                bean.setJob(rs.getString(6));
                bean.setAge(rs.getString(7));
                bean.setInfo(rs.getString(8));
 
                // 벡터의 빈 클래스에 저장함
                v.add(bean);
            }
 
            con.close();
 
        } catch (Exception e) {
 
            e.printStackTrace();
        }
        return v;
    }
 
}
 
cs




: