JSP Model1 게시판 만들기(1) 게시판 조회수, 답글, 댓글 Model + View

박예린·2023년 2월 18일


1.답글 view(answer.jsp)

<%@page import="dto.MemberDto"%>
<%@page import="dao.BbsDao"%>
<%@page import="dto.BbsDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%
int seq = Integer.parseInt( request.getParameter("seq") );
%>    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
	BbsDto dto = BbsDao.getInstance().getBbs(seq);
%>

<div align="center">

<h2>부모글</h2>

<table border="2">
<col width="200"><col width="500">
<tr>
	<th>작성자</th>
	<td><%=dto.getId() %></td>
</tr>
<tr>
	<th>제목</th>
	<td><%=dto.getTitle() %></td>
</tr>
<tr>
	<th>작성일</th>
	<td><%=dto.getWdate() %></td>
</tr>
<tr>
	<th>조회수</th>
	<td><%=dto.getReadcount() %></td>
</tr>
<tr>
	<th>내용</th>
	<td>
		<textarea rows="10" cols="50" readonly><%=dto.getContent() %></textarea>
	</td>
</tr>
</table>

<%
MemberDto login = (MemberDto)session.getAttribute("login");
%>

<h2>답글</h2>

<form action="answerAf.jsp" method="post">
<input type="hidden" name="seq" value="<%=dto.getSeq() %>">

<table border="1">
<col width="200"><col width="500">
<tr>
	<th>아이디</th>
	<td>
		<input type="text" name="id" size="50" readonly="readonly" value="<%=login.getId() %>">
	</td>
</tr>
<tr>
	<th>제목</th>
	<td>
		<input type="text" name="title" size="50">
	</td>	
</tr>
<tr>
	<th>내용</th>
	<td>
		<textarea rows="10" cols="50" name="content"></textarea>
	</td>
</tr>
<tr>
	<td colspan="2" align="center">
		<input type="submit" value="작성완료">
	</td>
</tr>
</table>
</form>

</div>


</body>
</html>

2.답글 후 처리(answerAf.jsp)

<%@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") );

String id = request.getParameter("id");
String title = request.getParameter("title");
String content = request.getParameter("content");

BbsDao dao = BbsDao.getInstance();
boolean isS = dao.answer(seq, new BbsDto(id, title, content));
if(isS){
	%>      
    <script>
	alert("답글입력 성공!");
	location.href = "bbslist.jsp";
	</script>
	<%
}else{	
	%>
	<script>
	alert("답글입력 실패");
	location.href = "bbslist.jsp";
	</script>
	<%
}
%>

3.조회수(BbsDao.java)

public void readcount(int seq) {
		String sql = " update bbs"
				+ "    set readcount=readcount+1 "
				+ "    where seq=? ";
		
		Connection conn = null;
		PreparedStatement psmt = null;
		
		try {
			conn = DBConnection.getConnection();
				
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, seq);
			
			psmt.execute();
			
		} catch (SQLException e) {			
			e.printStackTrace();
		} finally {
			DBClose.close(conn, psmt, null);
		}		
	}

4.답글(BbsDao.java)

public boolean answer(int seq, BbsDto dto) {
		
		// update
		String sql1 = " update bbs "
					+ " set step=step+1 "
					+ " where ref=(select ref from (select ref from bbs a where seq=?) A) "
					+ "	  and step>(select step from (select step from bbs b where seq=?) B) ";
		
		// insert
		String sql2 = " insert into bbs(id, ref, step, depth, title, content, wdate, del, readcount) "
					+ " values(?, "
					+ "		(select ref from bbs a where seq=?), "
					+ "		(select step from bbs b where seq=?)+1, "
					+ "		(select depth from bbs c where seq=?)+1, "
					+ "		?, ?, now(), 0, 0) ";
		
		Connection conn = null;
		PreparedStatement psmt = null;
		int count1 = 0, count2 = 0;
		
		try {
			conn = DBConnection.getConnection();
			// commit을 비활성화		commit(=확정+적용)/rollback
			conn.setAutoCommit(false);
			
			// update
			psmt = conn.prepareStatement(sql1);
			psmt.setInt(1, seq);
			psmt.setInt(2, seq);
			
			count1 = psmt.executeUpdate();
			
			// psmt 초기화
			psmt.clearParameters();
			
			// insert
			psmt = conn.prepareStatement(sql2);
			psmt.setString(1, dto.getId());
			psmt.setInt(2, seq);
			psmt.setInt(3, seq);
			psmt.setInt(4, seq);
			psmt.setString(5, dto.getTitle());
			psmt.setString(6, dto.getContent());
			
			count2 = psmt.executeUpdate();
			
			conn.commit();
			
		} catch (SQLException e) {			
			try { 
				conn.rollback(); 
			} catch (SQLException e1) {	e1.printStackTrace();}			
			e.printStackTrace();
		} finally {			
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {				
				e.printStackTrace();
			}
			DBClose.close(conn, psmt, null);
		}
				
		if(count2 > 0) {
			return true;
		}else {
			return false;
		}			
	}
profile
개발자를 꿈꾸는 귀여운 나

0개의 댓글