관리 메뉴

프로그래밍 삽질 중

session 이용한 jsp 책 리스트, 상세페이지 만들기 본문

과거 프로그래밍 자료들/자바(Java)

session 이용한 jsp 책 리스트, 상세페이지 만들기

평부 2021. 6. 8. 23:51

※ 문제점 1 : DB와 연결이 익숙해졌다고 생각했는데, 아니었다....

https://opentutorials.org/module/3569/21223 (프로젝트와 DB연결 오류 시 반드시 확인할 것)

※ 문제점 2 : 간단하게 만들 수 있어야 하는데, 아직도 버벅대는 게 많다

→ 즉, 이 글은 문제점2를 해결하기 위한 복습 내용인 셈

※ 사용된 파일은 크게 4개의 jsp(jsp와 controller 분리 안 됨) + 1개의 sql(oracle 사용)

→ booklist1.jsp / bookDetail1.jsp / regBookFrm1.jsp / regBookProc1.jsp

 

booklist1 -> bookDetail1 -> regBookFrm1 -> regBookProc 순으로 이동

[oracle : scott 계정 사용]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
create table book1 (
bookid varchar2(100),
bookname varchar2(100),
writer varchar2(50),
publisher varchar2(100),
price number);
 
/*yes24*/
insert into book1 values('a001''문명1,2''베르나르 베르베르''열린책들'29600);
insert into book1 values('e002''살려주시오''박종석''위즈덤하우스'10080);
insert into book1 values('p007''별것 아닌 선의''이소영','어크로스'9090);
insert into book1 values('m102''툇마루에서 모든 게 달라졌다''쓰루타니 가오리''북폴리오'7800);
insert into book1 values('en111''영숙어 덕분에 영어 공부가 쉬워졌습니다''시미즈켄지 외''키출판사'15000);
 
commit;
 
select * from book1;
cs

 

[booklist1.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, javax.sql.*, javax.naming.*"%>
<%request.setCharacterEncoding("utf-8"); %>
<!DOCTYPE html>
<html>
<!-- 부트스트랩 링크 -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<head>
<style>
body {
    padding: 50px;
}
table {
    width: 500px;
    table-layout: fixed;
}
tr {
    height: 50px;
}
tr th {
    background-color:  #99FFCC;
    text-align: center; 
}
tr td {
    width: 20%; 
    text-align: center;
}
</style>
<meta charset="UTF-8">
<title>책 목록 조회</title>
</head>
<body>
<table border=1>
        <tr>
            <th>책제목</th>
            <th>작가</th>
            <th>출판사</th>
            <th>가격</th>
        </tr>
        
<!--  DB 연결 -->
<%
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = null;
        
        InitialContext ic = new InitialContext();
        DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/myoracle");
        conn = ds.getConnection();
        sql = "select * from book1";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        while (rs.next()) {
%>
<tr>
            <td><a href='bookDetail1.jsp?bookId=<%=rs.getString(1)%>'><%=rs.getString(2)%></a></td>
            <td><%=rs.getString(3)%></td>
            <td><%=rs.getString(4)%></td>
            <td><%=rs.getInt(5)%></td>
</tr>
<%
            }
        rs.close();
        pstmt.close();
        conn.close();
%>
        <tr>
            <td colspan="4"> <button type="button" class="btn btn-success" onclick="location.href='regBookFrm1.jsp'">등록</button></td>
        </tr>
</table>
</body>
</html>
cs

 

 

[bookDetail1.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, javax.sql.*, javax.naming.*"%>
<!DOCTYPE html>
<html>
<head>
<!-- 부트스트랩 링크 -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
body {
    padding: 50px;
}
table {
    width: 500px;
    table-layout: fixed;
}
tr {
     border-color: 99FFCC; 
     width: 500px;
     height: 50px;
}
tr th {
    width: 50%; 
    text-align: center;
}
tr td {
    width: 50%; 
    text-align: center;
}
</style>
</head>
<body>
    <table border="1">
        <%
        String bookId = request.getParameter("bookId");
        request.setCharacterEncoding("utf-8");
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = null;
        InitialContext ic = new InitialContext();
        DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/myoracle");
        conn = ds.getConnection();
        sql = "select * from book1 where bookid=?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, bookId);
        rs = pstmt.executeQuery();
        rs.next();
        %>
        <tr>
            <th>책제목</th>
            <td><%=rs.getString(2)%></td>
        </tr>
        <tr>
            <th>작가</th>
            <td><%=rs.getString(3)%></td>
        </tr>
        <tr>
            <th>출판사</th>
            <td><%=rs.getString(4)%></td>
        </tr>
        <tr>
            <th>가격</th>
            <td><%=rs.getInt(5)%></td>
        </tr>
        <tr>
            <td colspan="2"><button type="button" class="btn btn-success" onclick="location.href='booklist1.jsp'">목록</button></td>
        </tr>
        <%
        rs.close();
        pstmt.close();
        conn.close(); 
        %>
    </table>
</body>
</html>
cs

 

 

[regBookFrm1.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<!-- 부트스트랩 링크 -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
body {
    padding: 50px;
}
table {
    width: 400px;
    table-layout: fixed;
}
fieldset{
        width: 50%;
        height: 50%;
    }
th{
        text-align: center;
    }
td {
        width: 50px;
        height: 50px;
    }
td input {
    width: 500px;
    height: 35px;
}
 
</style>
</head>
<body>
<fieldset>
    <legend>도서등록</legend>
    <form action="regBookProc1.jsp">
        <table>
            <tr>
                <th>책아이디</th>
                <td><input type="text" name="bookId"></td>
            </tr>
            <tr>
                <th>책제목</th>
                <td><input type="text" name="bookName"></td>
            </tr>
            <tr>
                <th>작가</th>
                <td><input type="text" name="writer"></td>
            </tr>
            <tr>
                <th>출판사</th>
                <td><input type="text" name="publisher"></td>
            </tr>
            <tr>
                <th>가격</th>
                <td><input type="text" name="price"></td>
            </tr>
            <tr>
                <td colspan="4"><button type="submit" class="btn btn-success">전송</button></td>
            </tr>
        </table>
    </form>
</fieldset>
</body>
</html>
cs

 

 

[regBookProc1.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, javax.sql.*, javax.naming.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    String sql = null;
    String bookId = request.getParameter("bookId");
    String bookName = request.getParameter("bookName");
    String writer = request.getParameter("writer");
    String publisher = request.getParameter("publisher");
    int price = Integer.parseInt(request.getParameter("price"));
    
    InitialContext ic = new InitialContext();
    DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/myoracle");
    conn = ds.getConnection();
    sql = "insert into book1(bookid, bookname, writer, publisher, price) values(?,?,?,?,?)";
    pstmt = conn.prepareStatement(sql);
    pstmt.setString(1,bookId);
    pstmt.setString(2,bookName);
    pstmt.setString(3,writer);
    pstmt.setString(4,publisher);
    pstmt.setInt(5,price);
    int result = pstmt.executeUpdate();
    
    if(result==1) {
        out.println("<script>");
        out.println("alert('책이 등록되었습니다.')");
        out.println("location.href='booklist1.jsp'");
        out.println("</script>");
    }
    
    pstmt.close();
    conn.close();
    %>
</body>
</html>
cs