19.05.18 jsp 쇼핑몰 차량 전체보기 (동영상 62강)

Back-End/JSP 2019. 5. 18. 18:09
728x90
반응형

쇼핑몰 차량 전체 보기 구현

 

 

RentcarMain.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
<%@ 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 center = request.getParameter("center");
        //처음 실행시에는 center 값이 넘어오지 않기에 반드시  null처리를 해야한다.
        //처리를 하지않으면 에러가 발생될수 있다.
 
        if (center == null) {
            center = "Center.jsp"//디폴트 center값을 부여(첫 화면에는 center이 뜨도록 한다는 말)    
        }
    %>
    <center>
        <table width="1000">
 
            <!-- Top 부분 -->
            <tr height="140" align="center">
                <!-- include page를 사용하여서 main페이지에서 각 페이지가 호출될수 있도록 한다. -->
                <td align="center" width="1000"><jsp:include page="Top.jsp" /></td>
            </tr>
 
            <!-- Center 부분 -->
            <!-- Top랑,Bottom은 화면이 넘어가더라도 바뀌지 않지만 center은 계속 바뀌기 때문에 center값을 준다 -->
 
            <tr height="100" align="center">
                <td align="center" width="1000"><jsp:include
                        page="<%=center%>" /></td>
            </tr>
 
            <!-- Bottom 부분 -->
            <tr height="140" align="center">
                <td align="center" width="1000"><jsp:include page="Bottom.jsp" /></td>
            </tr>
 
        </table>
    </center>
</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
<%@ 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%> 님 반갑습니다.</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

 

 

Center.jsp (중간)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ 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>
    <center>
        <table width="1000">
            <tr height="600">
                <!-- 센터에 출력될 이미지를 추가 -->
                <td align="center"><img alt="" src="img/Main.jpg" height="470"
                    width="1000"></td>
            </tr>
        </table>
    </center>
</body>
</html>
 
cs

 

 

Bottom.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=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>
    <center>
        <table width="1000">
            <tr height="100">
                <td align="center">
                    <hr color="red" size="5"> 이용약관 이메일 무단수집거부 개인정보 취급(처리)방침 윤리경영
                    보안신고 Contact Us 사업장 소개 사이트맵 웹접근성 도움말<br> 배드민턴단 COPYRIGHT@2015
                    SAMSUNG ELECTRO-MECHANICS, All rights reserved.
                </td>
            </tr>
        </table>
    </center>
</body>
</html>
 
cs

 

 

CarReserveMain.jsp (최신 자동차 3대 출력)

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
<%@page import="db.CarListBean"%>
<%@page import="java.util.Vector"%>
<%@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>
<body>
    <center>
 
        <!-- 데이터베이스에 연결하여 최신순 자동차 3대만 뿌려주는 데이터를 가져옴 -->
 
        <%
            RentcarDAO rdao = new RentcarDAO();
            //벡터를 이용하여 자동차데이터를 저장함
            Vector<CarListBean> v = rdao.getSelectCar();
        %>
 
        <table width="1000">
            <tr height="100">
                <td align="center" colspan="3"><font size="6" color="gray">최신형
                        자동차</font></td>
            </tr>
 
 
            <tr height="240">
                <!-- 벡터에 저장된 이미지를 하나씩 출력해야하기 때문에 for문을 돌리고, 그 값들을 빈클래스 변수에 넣어준다. -->
                <%
                    for (int i = 1; i < v.size(); i++) {
                        CarListBean bean = v.get(i);
                %>
                <td width="333" align="center">
                    <!-- 이미지는 벡터의 0번지에 해당되는걸 가져온다. for문을 돌려 td를 출력해야 한다.--> <!-- 확장자는 이미지안에 있으므로 궂이 적지 않아도 된다. -->
                    <!-- 이미지를 누르면 바로 상세정보로 넘어갈수 있도록 하기위해  <a>태그를 걸고, No에 대한 상세정보가 출력되도록 하고
        이름을 사진밑에 출력하도록 한다. --> <a
                    href="CarReserveInfo.jsp?no=<%=bean.getNo()%>"> <img alt=""
                        src="img/<%=bean.getImg()%>" width="300" height="220">
                </a>
                    <p>
                        차량명 :
                        <%=bean.getName()%>
                </td>
 
 
                <%
                    }
                %>
            
        </table>
 
        <p>
            <font size="4" color="gray"> 차량 검색 하기 </font><br> <br> <br>
            <!-- 종류를 선택하고 검색을 하면 종류라는 데이터들 가지고 넘어가야하기 때문에 form형식을 사용한다. -->
        <form action="RentcarMain.jsp?center=CarCategoryList.jsp"
            method="post">
            <font size="3" color="gray"> <b>차량 검색 하기</b>
            </font>&nbsp;&nbsp; <select name="category">
                <option value="1">소형</option>
                <option value="2">중형</option>
                <option value="3">대형</option>
            </select>&nbsp;&nbsp; <input type="submit" value="검색"> &nbsp;&nbsp;
 
        </form>
        <button
            onclick="location.href='RentcarMain.jsp?center=CarAllList.jsp'">전체
            검색</button>
 
    </center>
</body>
 
</html>
 
cs

 

 

CarCategoryList.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 import="db.CarListBean"%>
<%@page import="java.util.Vector"%>
<%@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>
<body>
    <!-- 카테고리 분류값을 받아와서 소형,중형, 대형에 따라 화면에 출력함 -->
    <%
        int category = Integer.parseInt(request.getParameter("category"));
        String temp = "";
        if (category == 1)
            temp = "소형";
        else if (category == 2)
            temp = "중형";
        else if (category == 3)
            temp = "대형";
    %>
    <center>
        <table width="1000">
            <tr height="100">
                <td align="center" colspan="3"><font size="6" color="gray">
                        <%=temp%>자동차
                </font></td>
            </tr>
            <%
                //카테고리 분류값을 받아온다. (검색의 기준으로 삼아야하기 때문)
                //CarRserveMain페이지 에서 //select 옵션으로 준 값들을 가져온다.
                //소형=1, 중형=2 등등등
                //request.getParameter 값은 다 string 타입이기때문에 포장클래스를 사용해 타입변환을 한다.
 
                System.out.println("category");
 
                //DB연결을 위해 객체를 생성
                RentcarDAO rdao = new RentcarDAO();
 
                //값이 유동적이기 때문에 벡터로 받고, 파라미터로 받은 category (분류값들)를 매개값으로 준다.
                Vector<CarListBean> v = rdao.getCategoryCar(category);
 
                //tr을 3개씩 보여주고 다시 tr을 실행할 수 있도록 하는 변수 선언
                int j = 0;
                for (int i = 0; i < v.size(); i++) {
                    //벡터에 저장되어 있는 빈 클래스를 추출
                    CarListBean bean = v.get(i);
                    //3번마다 0이 돌아온다는 뜻. 즉 3번에 한번 실행하도록 하는 구문
                    if (j % 3 == 0) {
            %>
            <tr height="220">
                <%
                    }
                %>
                <td width="333" align="center"><a
                    href="RentcarMain.jsp?center=CarReserveInfo.jsp?no=<%=bean.getNo()%>">
                        <img alt="" src="img/<%=bean.getImg()%>" width="300" height="200">
                </a>
                    <p>
                        <font size="3" color="gray"><b>차량명 : <%=bean.getName()%>
                        </b> </font></td>
 
 
 
                <%
                    j = j + 1//j값을 증가하여 하나의 행에 총3개의 차량정보를 보여주기 위해서 증가
                    }
                %>
            
        </table>
    </center>
</body>
</html>
cs

 

 

CarAllList.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
<%@page import="db.CarListBean"%>
<%@page import="java.util.Vector"%>
<%@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>
    <center>
        <table width="1000">
            <tr height="100">
                <td align="center" colspan="3"><font size="6" color="gray">
                        전체 렌트카 보기 </font></td>
            </tr>
            <%
                //DB연결을 위해 객체를 생성
                RentcarDAO rdao = new RentcarDAO();
 
                //값이 유동적이기 때문에 벡터로 받고, 파라미터로 받은 category (분류값들)를 매개값으로 준다.
                Vector<CarListBean> v = rdao.getAllCar();
 
                //tr을 3개씩 보여주고 다시 tr을 실행할 수 있도록 하는 변수 선언
                int j = 0;
                for (int i = 0; i < v.size(); i++) {
                    //벡터에 저장되어 있는 빈 클래스를 추출
                    CarListBean bean = v.get(i);
                    //3번마다 0이 돌아온다는 뜻. 즉 3번에 한번 실행하도록 하는 구문
                    if (j % 3 == 0) {
            %>
            <tr height="220">
                <%
                    }
                %>
                <td width="333" align="center"><a
                    href="RentcarMain.jsp?center=CarReserveInfo.jsp?no=<%=bean.getNo()%>">
                        <img alt="" src="img/<%=bean.getImg()%>" width="300" height="200">
                </a>
                    <p>
                        <font size="3" color="gray"><b>차량명 : <%=bean.getName()%>
                        </b> </font></td>
 
 
 
                <%
                    j = j + 1//j값을 증가하여 하나의 행에 총3개의 차량정보를 보여주기 위해서 증가
                    }
                %>
            
        </table>
    </center>
</body>
 
</html>
cs

 

 

CarListBean.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
71
72
73
74
75
76
77
78
package db;
 
public class CarListBean { // 빈 클래스 생성함
 
    private int no;
    private String name;
    private int category;
    private int price;
    private int usepeople;
    private String company;
    private String img;
    private String info;
 
    public int getNo() {
        return no;
    }
 
    public void setNo(int no) {
        this.no = no;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getCategory() {
        return category;
    }
 
    public void setCategory(int category) {
        this.category = category;
    }
 
    public int getPrice() {
        return price;
    }
 
    public void setPrice(int price) {
        this.price = price;
    }
 
    public int getUsepeople() {
        return usepeople;
    }
 
    public void setUsepeople(int usepeople) {
        this.usepeople = usepeople;
    }
 
    public String getImg() {
        return img;
    }
 
    public void setImg(String img) {
        this.img = img;
    }
 
    public String getInfo() {
        return info;
    }
 
    public void setInfo(String info) {
        this.info = info;
    }
 
    public String getCompany() {
        return company;
    }
 
    public void setCompany(String company) {
        this.company = company;
    }
}
 
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
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;
 
    }
 
}
 
cs

 

 

 

728x90
반응형
: