19.05.19 jsp 쇼핑몰 로그인 (동영상 66강)

Back-End/JSP 2019. 5. 19. 20:30
728x90
반응형

쇼핑몰 로그인 페이지 구현

 

 

MemberLogin.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
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <!-- 로그인 페이지 -->
    <!-- 아이디랑 패스워드만 받게할 예정 -->
    <form action="MemberLoginProc.jsp" method="post">
        <table width="300" border="1" bordercolor="gray">
            <tr height="100">
                <td align="center" colspan="2"><font size="6" color="gray">로그인
                </font></td>
            </tr>
            <tr height="40">
                <td width="120" align="center">아이디</td>
                <td width="180"><input type="text" name="id" size="15"></td>
            </tr>
 
            <tr height="40">
                <td width="120" align="center">패스워드</td>
                <td width="180"><input type="password" name="pass" size="15"></td>
            </tr>
 
            <tr height="40">
                <td align="center" colspan="2"><input type="submit" value="로그인"></td>
            </tr>
 
 
 
 
        </table>
    </form>
</body>
</html>
cs

 

 

MemberLoginProc.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
<%@page import="db.RentcarDAO"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
 
    <% 
    //로그인 처리 페이지, 한글깨짐 방지
 
    String id = request.getParameter("id");
    String pass = request.getParameter("pass");
    //회원 아이디와 패스워드가 일치하는지 비교한다.
    RentcarDAO rdao = new RentcarDAO();
    
    //해당 회원이 있는지 여부를 숫자로 반환받는다.
    int result = rdao.getMember(id,pass);
    
    if(result==0)
    {
%>
 
    <script>//예약할시에 로그인이 안되어있을경우 출력되는 메시지
        alert("회원 아이디 또는 패스워드가 틀립니다.");
    //로그인이 안되어있을경우 로그인 페이지로 이동
        location.href='RentcarMain.jsp?center=MemberLogin.jsp';
    </script>
 
    <%
    }else
    {
        //로그인 처리가 되었다면 세션에 담아야 화면이 넘어갈때 유지된다.
        session.setAttribute("id", id);
        response.sendRedirect("RentcarMain.jsp");
    }
    
    
 
%>
 
 
</body>
</html>
cs

 

 

Top.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
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
 
    <!-- 세션을 이용한 로그인 처리 -->
    <!-- 세션으로 받아온 값은 오브젝트 타입이기 때문에 String 타입으로 컨버팅 한다. -->
 
    <%
        String id = (String) session.getAttribute("id");
 
        //로그인이 되어있지 않다면 id에 "GUEST"값을 준다
        if (id == null) {
            id = "GUEST";
        }
    %>
 
    <table width="1000" bordercolor="white">
        <tr height="70">
            <td colspan="4"><a href="RentcarMain.jsp"
                style="text-decoration: none"> <!-- 이미지를 불러오기위한 태그 작성 --> <img
                    alt="" src="img/RENT.jpg" height="150" width="250">
            </a></td>
            <td align="center" width="200"><%=id%> 님 <%
                if(id.equals("GUEST")){ %>
                <button
                    onclick="location.href='RentcarMain.jsp?center=MemberLogin.jsp'">
                    로그인</button> <%
                }else{%>
                <button onclick="location.href='MemberLogout.jsp'">로그아웃</button> <% 
                }
                
            %></td>
        </tr>
        <!-- 글자를 누르면 화면이 넘어갈수 있도록 a태그를 걸어줌 -->
        <tr height="50">
            <td align="center" width="200" bgcolor="pink"><font
                color="white" size="5"> <a
                    href="RentcarMain.jsp?center=CarReserveMain.jsp"
                    style="text-decoration: none"> 예 약 하 기 </a></font></td>
 
            <td align="center" width="200" bgcolor="pink">
                <!-- 글자를 누르면 화면이 넘어갈수 있도록 a태그를 걸어줌 --> <font color="white" size="5"><a
                    href="#" style="text-decoration: none"> 예 약 확 인</a></font>
            </td>
 
            <td align="center" width="200" bgcolor="pink">
                <!-- 글자를 누르면 화면이 넘어갈수 있도록 a태그를 걸어줌 --> <font color="white" size="5"><a
                    href="#" style="text-decoration: none"> 자 유 게 시 판 </a></font>
            </td>
 
            <td align="center" width="200" bgcolor="pink">
                <!-- 글자를 누르면 화면이 넘어갈수 있도록 a태그를 걸어줌 --> <font color="white" size="5"><a
                    href="#" style="text-decoration: none"> 이 벤 트 </a></font>
            </td>
 
            <td align="center" width="200" bgcolor="pink">
                <!-- 글자를 누르면 화면이 넘어갈수 있도록 a태그를 걸어줌 --> <font color="white" size="5"><a
                    href="#" style="text-decoration: none"> 고 객 센 터 </a></font>
            </td>
        </tr>
 
    </table>
</body>
</html>
 
cs

 

 

RentcarDAO.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
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
package db;
 
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 RentcarDAO {
 
    Connection con;
    PreparedStatement pstmt;
    ResultSet rs;
 
    // 커넥션풀을 이용한 데이터베이스 연결
 
    public void getcon() {
 
        // DB에 접속할때는 예외처리를 실시해야됨
        try {
 
            Context initctx = new InitialContext(); // 외부서버로 부터 데이터를 읽어들이는것이기 때문에 드라이버가 없을수 있어
            Context envctx = (Context) initctx.lookup("java:comp/env"); // 자바를 읽어들일수 있는 환경에서 사용 //예외처리를 해준다.
            DataSource ds = (DataSource) envctx.lookup("jdbc/pool");
            con = ds.getConnection();
            // 데이터소스에 username, url, password를 집어넣는다. 그렇게 하면 데이터소스가 커넥션을 얻어 온다.
            // jdbc/pool에 있는 데이터소스를 사용할수 있다.
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
 
        }
    }
 
    // 최신순 3대의 자동차를 리턴하는 메소드
    public Vector<CarListBean> getSelectCar() {
        // 리턴타입을 설정
        Vector<CarListBean> v = new Vector<>();
        getcon(); // 커넥션이 연결되어야 쿼리를 실행 가능
 
        try {
 
            String sql = "select * from rentcar order by no desc ";
            pstmt = con.prepareStatement(sql);
            // 쿼리 실행후 실행결과 Result리턴함
            rs = pstmt.executeQuery();
            int count = 0;
            while (rs.next())// 결과값이 끝날때까지만 실행
            {
                CarListBean bean = new CarListBean();
                bean.setNo(rs.getInt(1));
                bean.setName(rs.getString(2));
                bean.setCategory(rs.getInt(3));
                bean.setPrice(rs.getInt(4));
                bean.setUsepeople(rs.getInt(5));
                bean.setCompany(rs.getString(6));
                bean.setImg(rs.getString(7));
                bean.setInfo(rs.getString(8));
                // 벡터에 빈 클래스를 저장
                v.add(bean);
                count++;
                // 3개만 저장이 되야하기 때문..
                if (count > 3)
                    break// 반복문을 빠져나가시오.
 
            }
            con.close();
        } catch (Exception e) {// 내림차순으로 검색하는 쿼리문 작성
            e.printStackTrace();
        }
        return v;
 
    }
 
    // 카테고리별 자동차 리스트를 저장하는 메소드
    // 벡터로 받았으니 벡터로 리턴함..
    public Vector<CarListBean> getCategoryCar(int cate)
 
    {
        // 리턴타입이 벡터 객체이기 때문에 벡터 객체를 생성한다.
        Vector<CarListBean> v = new Vector<>();
 
        // 데이터를 저장할 빈 클래스 선언
        CarListBean bean = null;
 
        getcon();
 
        try {
            String sql = "select * from rentcar where category=?";
            pstmt = con.prepareStatement(sql);
 
            // ?에 값을 넣는다.
            pstmt.setInt(1, cate);
            // 결과를 리턴
            rs = pstmt.executeQuery();
            // 반복문을 돌려서 데이터를 저장
 
            while (rs.next()) { // 데이터를 저장할 빈 클래스 생성
                bean = new CarListBean();
                bean.setNo(rs.getInt(1));
                bean.setName(rs.getString(2));
                bean.setCategory(rs.getInt(3));
                bean.setPrice(rs.getInt(4));
                bean.setUsepeople(rs.getInt(5));
                bean.setCompany(rs.getString(6));
                bean.setImg(rs.getString(7));
                bean.setInfo(rs.getString(8));
                // 벡터에 빈 클래스를 저장
                v.add(bean);
 
            }
 
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return v;
 
    }
 
    // 모든 차량을 검색하는 메소드
    public Vector<CarListBean> getAllCar() {
 
        Vector<CarListBean> v = new Vector<>();
        // 데이터를 저장할 빈클래스 선언
        CarListBean bean = null;
 
        getcon();
 
        try {
            String sql = "select * from rentcar";
            pstmt = con.prepareStatement(sql);
            rs = pstmt.executeQuery();
            // 반복문을 돌리면서 데이터를 저장함
 
            while (rs.next()) {// 데이터를 저장할 빈클래스 생성
                bean = new CarListBean();
                bean.setNo(rs.getInt(1));
                bean.setName(rs.getString(2));
                bean.setCategory(rs.getInt(3));
                bean.setPrice(rs.getInt(4));
                bean.setUsepeople(rs.getInt(5));
                bean.setCompany(rs.getString(6));
                bean.setImg(rs.getString(7));
                bean.setInfo(rs.getString(8));
                // 벡터에 빈 클래스를 저장
                v.add(bean);
 
            }
 
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return v;
 
    }
 
    // 하나의 자동차 정보를 리턴하는 메소드
    public CarListBean getOneCar(int no) {
 
        // 리턴타입을 선언한다. 객체를 생성해서 그 객체에 값을 넣은후 리턴해야하기 때문
        CarListBean bean = new CarListBean();
 
        getcon();
 
        try {
            String sql = "select * from rentcar where no=?";
            pstmt = con.prepareStatement(sql);
            // sql문에 ?표가 들어가 있으면 초기 인덱스 값을 할당해주어야 한다.
            pstmt.setInt(1, no);
            rs = pstmt.executeQuery();
 
            if (rs.next()) {
                bean.setNo(rs.getInt(1));
                bean.setName(rs.getString(2));
                bean.setCategory(rs.getInt(3));
                bean.setPrice(rs.getInt(4));
                bean.setUsepeople(rs.getInt(5));
                bean.setCompany(rs.getString(6));
                bean.setImg(rs.getString(7));
                bean.setInfo(rs.getString(8));
 
            }
 
            con.close();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bean;
 
    }
 
    // 회원 정보가 있는지를 비교한다.
    public int getMember(String id, String pass) {
        int result = 0// 0이면 회원이 없다고 가정
        getcon();
 
        try { //아이디와 패스워드가 모두 있는 회원의 숫자를 반환하는 sql문
            String sql = "select count(*) from member where id=? and pass1=?";
            pstmt = con.prepareStatement(sql);
            // sql문에 ?표가 들어가 있으면 초기 인덱스 값을 할당해주어야 한다.
 
            pstmt.setString(1, id);
            pstmt.setString(2, pass);
 
            rs = pstmt.executeQuery();
 
            if (rs.next()) {
                result = rs.getInt(1); // 0또는 1이 저장된다. 0이면 회원이 없는거고, 1이면 있는것
            }
 
            con.close();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
 
    }
 
}
 
cs

 

 

 

728x90
반응형
: