본문 바로가기

Side Project

#04. [Side Project 1] JSP 로그인, 게시판 만들기 (4) :: 답글, 댓글,

 

 

■ answer.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
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
<%@page import="dto.MemberDto"%>
<%@page import="dao.BbsDao"%>
<%@page import="dto.BbsDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
<%
String sseq = request.getParameter("seq");
int seq = Integer.parseInt(sseq);
%>    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>answer.jsp</title>
</head>
<body>
 
<%
BbsDao dao = BbsDao.getInstance();
BbsDto parentBbs = dao.getBbs(seq);
%>
 
<%--
기본글 <table>
    작성자
    제목
    작성일
    조회수
    정보
    내용
    
답글    <table>
    아이디 login id <- session id    
    제목
    내용
 
 --%>
<h1>기본글</h1>
 
<div align="center">
 
<table border="2">
<col width="200"><col width="500">
<tr>
    <th>작성자</th>
    <td><%=parentBbs.getId() %></td>
</tr>
<tr>
    <th>제목</th>
    <td><%=parentBbs.getTitle() %></td>
</tr>
<tr>
    <th>작성일</th>
    <td><%=parentBbs.getWdate() %></td>
</tr>
<tr>
    <th>조회수</th>
    <td><%=parentBbs.getReadcount() %></td>
</tr>
<tr>
    <th>정보</th>
    <td></td>
</tr>
<tr>
    <th>내용</th>
    <td>
        <textarea rows="10" cols="70"><%=parentBbs.getContent() %></textarea>
    </td>
</tr>
</table>
 
<hr>
 
<%
MemberDto mem = (MemberDto)session.getAttribute("login");
%>
 
<h1 align="left">답글</h1>
 
<form action="answerAf.jsp" method="post">
<input type="hidden" name="seq" value="<%=parentBbs.getSeq() %>">
 
<table border="1">
<col width="200"><col width="500">
 
<tr>
    <th>아이디</th>
    <td>
        <input type="text" name="id" readonly="readonly" size="50"
            value="<%=mem.getId() %>">
    </td>
</tr>
 
<tr>
    <th>제목</th>
    <td>
        <input type="text" name="title" size="50">
    </td>
</tr>
 
<tr>
    <th>내용</th>
    <td>
        <textarea rows="10" cols="70" name="content"></textarea>
    </td>
</tr>
 
<tr>
    <td colspan="2" align="center">
        <input type="submit" value="답글추가">
    </td>
</tr>
 
</table>
</form>
 
</div>
 
</body>
</html>
cs

 

■ answerAf.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
<%@page import="dto.BbsDto"%>
<%@page import="dao.BbsDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
 
int seq = Integer.parseInt(request.getParameter("seq"));    // 목적: ref(그룹번호)설정
String id = request.getParameter("id");
String title = request.getParameter("title");
String content = request.getParameter("content");
 
System.out.println("seq:" + seq);
System.out.println("id:" + id);
System.out.println("title:" + title);
System.out.println("content:" + content);
%>    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>answerAf.jsp</title>
</head>
<body>
 
<%
BbsDao dao = BbsDao.getInstance();
boolean isS = dao.answer(seq, new BbsDto(id, title, content));
 
if(isS){
%>
    <script type="text/javascript">
    alert("답글 입력 성공!");
    location.href = "bbslist.jsp";
    </script>
<%
}else{
%>
    <script type="text/javascript">
    alert("답글 입력 실패");
    location.href = "bbslist.jsp";
    </script>
<%
}
%>
 
 
 
</body>
</html>
 
cs

 

■ BbsDao.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
public boolean answer(int seq, BbsDto bbs) {        
        // update
        String sql1 = " UPDATE BBS "
                    + " SET STEP=STEP+1 "
                    + " WHERE REF=(SELECT REF FROM BBS WHERE SEQ=? ) "
                    + "     AND STEP>(SELECT STEP FROM BBS WHERE SEQ=? ) ";
        
        // insert
        String sql2 = " INSERT INTO BBS "
                    + " (SEQ, ID, "
                    + " REF, STEP, DEPTH, "
                    + " TITLE, CONTENT, WDATE, DEL, READCOUNT) "
                    + " VALUES(SEQ_BBS.NEXTVAL, ?, "
                    + "        (SELECT REF FROM BBS WHERE SEQ=?), "
                    + "        (SELECT STEP FROM BBS WHERE SEQ=?) + 1, "
                    + "     (SELECT DEPTH FROM BBS WHERE SEQ=?) + 1, "
                    + "        ?, ?, SYSDATE, 0, 0) ";
        
        Connection conn = null;
        PreparedStatement psmt = null;
        
        int count = 0;
        
        try {
            conn = DBConnection.getConnection();
            conn.setAutoCommit(false);
            System.out.println("1/6 answer success");
            
            // update
            psmt = conn.prepareStatement(sql1);
            psmt.setInt(1, seq);
            psmt.setInt(2, seq);
            System.out.println("2/6 answer success");
            
            count = psmt.executeUpdate();
            System.out.println("3/6 answer success");
                        
            // psmt 초기화
            psmt.clearParameters();
            
            // insert
            psmt = conn.prepareStatement(sql2);
            psmt.setString(1, bbs.getId());
            psmt.setInt(2, seq);
            psmt.setInt(3, seq);
            psmt.setInt(4, seq);
            psmt.setString(5, bbs.getTitle());
            psmt.setString(6, bbs.getContent());
            System.out.println("4/6 answer success");
            
            count = psmt.executeUpdate();
            System.out.println("5/6 answer success");
            
            conn.commit();
            
        } catch (Exception e) {            
            e.printStackTrace();            
            try {
                conn.rollback();
            } catch (SQLException e1) {                
                e1.printStackTrace();
            }
        } finally {            
            try {
                conn.setAutoCommit(true);
            } catch (SQLException e) {                
                e.printStackTrace();
            }
            DBClose.close(psmt, conn, null);
            System.out.println("6/6 answer success");
        }
        return count>0?true:false;
    }
cs