1. BoardBean.java - 답글 기능을 위한 속성 3개 추가
package magic.board;
import java.sql.Timestamp;
public class BoardBean {
private int b_id;
private String b_name;
private String b_email;
private String b_title;
private String b_content;
private Timestamp b_date;
private int b_hit;
private String b_pwd;
private String b_ip;
private int b_ref;
private int b_step;
private int b_level;
public int getB_ref() {
return b_ref;
}
public void setB_ref(int b_ref) {
this.b_ref = b_ref;
}
public int getB_step() {
return b_step;
}
public void setB_step(int b_step) {
this.b_step = b_step;
}
public int getB_level() {
return b_level;
}
public void setB_level(int b_level) {
this.b_level = b_level;
}
public String getB_ip() {
return b_ip;
}
public void setB_ip(String b_ip) {
this.b_ip = b_ip;
}
public String getB_pwd() {
return b_pwd;
}
public void setB_pwd(String b_pwd) {
this.b_pwd = b_pwd;
}
public int getB_hit() {
return b_hit;
}
public void setB_hit(int b_hit) {
this.b_hit = b_hit;
}
public Timestamp getB_date() {
return b_date;
}
public void setB_date(Timestamp b_date) {
this.b_date = b_date;
}
public int getB_id() {
return b_id;
}
public void setB_id(int b_id) {
this.b_id = b_id;
}
public String getB_name() {
return b_name;
}
public void setB_name(String b_name) {
this.b_name = b_name;
}
public String getB_email() {
return b_email;
}
public void setB_email(String b_email) {
this.b_email = b_email;
}
public String getB_title() {
return b_title;
}
public void setB_title(String b_title) {
this.b_title = b_title;
}
public String getB_content() {
return b_content;
}
public void setB_content(String b_content) {
this.b_content = b_content;
}
}
2. BoardDBBean.java - 답글 기능을 위한 메소드 수정
public int insertboard(BoardBean board) throws Exception {
String sql = "SELECT MAX(B_ID) FROM boardT"; // id 최대값
int re = -1;
int number;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int id = board.getB_id();
int ref = board.getB_ref();
int step = board.getB_step();
int level = board.getB_level();
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
number = rs.getInt(1) + 1;
} else {
number = 1;
}
if (id != 0) { // 답변 글쓰기 이면 업데이트 쿼리 실행
sql = "UPDATE boardT SET B_STEP = B_STEP+1 WHERE B_REF = ? AND B_STEP > ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, ref);
pstmt.setInt(2, step);
pstmt.executeUpdate();
step = step + 1;
level = level + 1;
} else { // 그냥 글쓰기 이면 ref는 아이디와 동일, step,level은 0
ref = number;
step = 0;
level = 0;
}
sql = "INSERT INTO boardT VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, number);
pstmt.setString(2, board.getB_name());
pstmt.setString(3, board.getB_email());
pstmt.setString(4, board.getB_title());
pstmt.setString(5, board.getB_content());
pstmt.setTimestamp(6, board.getB_date());
pstmt.setInt(7, board.getB_hit());
// 조회수 컬럼의 초기값을 0으로 설정 > 데이터 구조에서 디폴트값을 0으로 설정(글쓰면 조회수 0이기 때문)
pstmt.setString(8, board.getB_pwd());
pstmt.setString(9, board.getB_ip());
pstmt.setInt(10, ref);
pstmt.setInt(11, step);
pstmt.setInt(12, level);
pstmt.executeUpdate();
re=1;
} catch (SQLException ex) {
System.out.print("글쓰기 실패");
ex.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return re;
}
public ArrayList<BoardBean> listBoard() throws Exception {
String selectQuery = "SELECT B_ID, B_NAME, B_EMAIL, B_TITLE, B_CONTENT"
+ ", B_DATE, B_HIT, B_PWD, B_IP, B_REF, B_STEP, B_LEVEL "
+ "FROM boardT ORDER BY B_REF DESC, B_STEP";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList<BoardBean> list = new ArrayList<BoardBean>();
try {
conn = getConnection();
pstmt = conn.prepareStatement(selectQuery);
rs = pstmt.executeQuery();
while (rs.next()) {
BoardBean bb = new BoardBean();
bb.setB_id(rs.getInt(1));
bb.setB_name(rs.getString(2));
bb.setB_email(rs.getString(3));
bb.setB_title(rs.getString(4));
bb.setB_content(rs.getString(5));
bb.setB_date(rs.getTimestamp(6));
bb.setB_hit(rs.getInt(7));
bb.setB_pwd(rs.getString(8));
bb.setB_ip(rs.getString(9));
bb.setB_ref(rs.getInt(10));
bb.setB_step(rs.getInt(11));
bb.setB_level(rs.getInt(12));
list.add(bb); // 반복하면서 list 객체에 bb객체 내용을 담음
}
} catch (SQLException ex) {
System.out.print("조회 실패");
ex.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
public BoardBean getBoard(int b_id, boolean hitAdd) throws Exception {
String sql = "";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
BoardBean bb = null;
try {
if (hitAdd == true) { // 게시글 볼때는 true, 수정할 때는 false
sql = "UPDATE boardT SET B_HIT=B_HIT+1 WHERE B_ID = ?";
// 조회수 수정하는 쿼리문 해당 메소드가 실행될 때 조회수 1씩 증가해서 업데이트 쿼리 실행
conn = getConnection();
;
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, b_id);
pstmt.executeUpdate(); // 쿼리 실행
} else {
conn = getConnection();
}
sql = "SELECT B_ID, B_EMAIL, B_TITLE, B_NAME, B_CONTENT, B_DATE, B_HIT, B_PWD"
+ ", B_IP, B_REF, B_STEP, B_LEVEL FROM boardT where B_ID = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, b_id);
rs = pstmt.executeQuery();
if (rs.next()) {
bb = new BoardBean();
bb.setB_id(b_id);
bb.setB_name(rs.getString("B_NAME"));
bb.setB_email(rs.getString("B_EMAIL"));
bb.setB_title(rs.getString("B_TITLE"));
bb.setB_content(rs.getString("B_CONTENT"));
bb.setB_date(rs.getTimestamp("B_DATE"));
bb.setB_hit(rs.getInt("B_HIT"));
bb.setB_pwd(rs.getString("B_PWD"));
bb.setB_ip(rs.getString("B_IP"));
bb.setB_ref(rs.getInt("B_REF"));
bb.setB_step(rs.getInt("B_STEP"));
bb.setB_level(rs.getInt("B_LEVEL"));
}
} catch (SQLException ex) {
System.out.print("조회 실패");
ex.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return bb;
}
3. write.jsp - 답변 표시를 위한 수정
<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="board.js" charset="UTF-8"></script>
</head>
<body>
<%
int bid = 0,ref = 1,level = 0,step = 0;
String title="";
if(request.getParameter("b_id") != null) {
bid = Integer.parseInt(request.getParameter("b_id"));
}
BoardDBBean bdb = BoardDBBean.getInstance();
BoardBean bb = bdb.getBoard(bid, false);
if(bb != null) {
ref = bb.getB_ref();
step = bb.getB_step();
level = bb.getB_level();
title = bb.getB_title();
}
%>
<center>
<h1>글 올 리 기</h1>
<form method="post" action="write_ok.jsp" name="write_frm">
<table>
<tr height="30">
<td width="80">작성자</td>
<td width="100">
<input type="text" id="name" name="name" size="10" maxlength="20"/>
</td>
<td width="80">이메일</td>
<td width="240">
<input type="text" id="email" name="email" size="24" maxlength="50" />
</td>
</tr>
<tr>
<td width="80">글제목</td>
<td colspan="3" width="460">
<%
if(bid == 0) { // id값이 0이 아니면 제목에 [답변]: 제목이 달려있는 상태
%>
<input type="text" id="title" name="title" size="58" maxlength="80" />
<%
} else {
%>
<input type="text" id="title" name="title" size="58" maxlength="80"
value="[답변]:<%= title %>" />
<%
}
%>
</td>
</tr>
<tr>
<td colspan="4">
<textarea cols="70" rows="10" id="content" name="content" maxlength="3000"></textarea>
</td>
<tr>
<tr>
<td width="80">암 호</td>
<td width="200">
<input type="password" id="pwd" name="pwd" size="12" maxlength="12" />
</td>
</tr>
<tr>
<td><input type="hidden" name="id" value="<%= bid %>" /></td>
<td><input type="hidden" name="ref" value="<%= ref %>" /></td>
<td><input type="hidden" name="step" value="<%= step %>" /></td>
<td><input type="hidden" name="level" value="<%= level %>" /></td>
</tr>
<tr height="50">
<td colspan="4" align="center">
<input type="button" value="글쓰기" onclick="check_ok()" />
<input type="reset" value="다시작성" />
<input type="button" value="글목록" onclick="location.href='list.jsp'" />
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
4. write_ok.jsp
<%@page import="java.net.InetAddress"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<jsp:useBean class="magic.board.BoardBean" id="bb"></jsp:useBean>
<%
String content = request.getParameter("content");
content = content.replace("\r\n", "<br>");
// DB에 콘텐트를 저장하면서 줄바꿈이 처리가 안되는 경우가 있기 때문에 자바에서 줄바꿈을 처리하는 명령어를 <br>로 대체
bb.setB_name(request.getParameter("name"));
bb.setB_email(request.getParameter("email"));
bb.setB_title(request.getParameter("title"));
bb.setB_date(new Timestamp(System.currentTimeMillis()));
bb.setB_content(content);
bb.setB_pwd(request.getParameter("pwd"));
bb.setB_id(Integer.parseInt(request.getParameter("id")));
bb.setB_ref(Integer.parseInt(request.getParameter("ref")));
bb.setB_step(Integer.parseInt(request.getParameter("step")));
bb.setB_level(Integer.parseInt(request.getParameter("level")));
// ip 주소 리턴 방법. 1
InetAddress address = InetAddress.getLocalHost();
String ip = address.getHostAddress();
bb.setB_ip(ip);
// ip 주소 리턴 방법. 2
// bb.setB_ip(request.getRemoteAddr());
BoardDBBean bdb = BoardDBBean.getInstance();
int re = bdb.insertboard(bb);
if(re == 1){
response.sendRedirect("list.jsp");
} else {
response.sendRedirect("write.jsp");
}
%>
- 추가된 속성 3개를 파라미터 값으로 받아옴
5. show.jsp - 답변 버튼 추가
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
BoardDBBean bdb = BoardDBBean.getInstance();
BoardBean bb = bdb.getBoard(Integer.parseInt(request.getParameter("b_id")),true);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
%>
<center>
<h1>글 내 용 보 기</h1>
<table border="1" width="600" cellspacing="0">
<tr height="30" align="center">
<td width="100">글번호</td>
<td width="200"><%= bb.getB_id() %></td>
<td width="100">조회수</td>
<td width="200"><%= bb.getB_hit() %></td>
</tr>
<tr height="30" align="center">
<td width="100" >작성자</td>
<td width="200"><%= bb.getB_name() %></td>
<td width="100" >작성일</td>
<td width="200"><%= sdf.format(bb.getB_date()) %></td>
<tr height="30">
<td width="100" align="center">글제목</td>
<td width="200" colspan="3"><%= bb.getB_title() %></td>
</tr>
<tr height="30">
<td width="100" align="center">글내용</td>
<td width="200" colspan="3"><%= bb.getB_content() %></td>
</tr>
<tr height="30">
<td colspan="4" align="right">
<input type="button" value="글수정" onclick="location.href='edit.jsp?b_id=<%= bb.getB_id() %>'" />
<input type="button" value="글삭제" onclick="location.href='delete.jsp?b_id=<%= bb.getB_id() %>'" />
<input type="button" value="답변글" onclick="location.href='write.jsp?b_id=<%= bb.getB_id() %>'" />
<input type="button" value="글목록" onclick="location.href='list.jsp'" />
</td>
</tr>
</table>
</center>
</body>
</html>
6. list.jsp
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<%
BoardDBBean bdb = BoardDBBean.getInstance();
ArrayList<BoardBean> list = bdb.listBoard();
int b_id,b_hit,b_level;
String b_name, b_email, b_title, b_content;
Timestamp b_date;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
// Timestamp로 받아온 날짜의 형식을 지정
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>게시판에 등록된 글 목록 보기</h1>
<table width="600">
<tr>
<td align="right">
<a href="write.jsp">글 쓰 기</a>
</td>
</tr>
</table>
<table border="1" width="800">
<tr height="25">
<td width="40" align="center">번호</td>
<td width="450" align="center">글제목</td>
<td width="120" align="center">작성자</td>
<td width="130" align="center">작성일</td>
<td width="60" align="center">조회수</td>
</tr>
<%
for(int i=0; i < list.size(); i++) {
BoardBean board = list.get(i); // 배열에 넣은 역순으로 board 객체에 값을 넣어줌
b_id=board.getB_id();
b_name=board.getB_name();
b_email=board.getB_email();
b_title= board.getB_title();
b_date= board.getB_date();
b_hit= board.getB_hit();
b_level = board.getB_level();
%>
<tr height="25" bgcolor="#E8E8E8" onmouseover="this.style.backgroundColor='#AEBAB4'"
onmouseout="this.style.backgroundColor='#E8E8E8'">
<td align="center"><%= b_id %></td>
<td id="title" >
<%
if(b_level > 0){
for(int j=0; j<b_level; j++){
%>
<%
}
%>
<img src="../images/AnswerLine.gif" />
<%
}
%>
<a href="show.jsp?b_id=<%=b_id %>"><%= b_title %></a>
</td>
<td align="center">
<a href="mailto:<%= b_email %>>"><!-- 작성자에게 메일을 보내는 a태그 -->
<%= b_name %>
</a>
</td>
<td align="center"><%= sdf.format(b_date) %></a>
</td>
</td>
<td align="center"><%= b_hit %></a>
</td>
</tr>
<% } %>
</table>
</center>
</body>
</html>
- 이미지를 추가해 답변인것 표시, 답변 단계에 따라 띄어쓰기 추가로 구분이 편하게 함
'java,jsp,spring > JSP' 카테고리의 다른 글
JSP 게시판 - 파일 업로드 (1) | 2022.09.15 |
---|---|
JSP 게시판 페이징 (1) | 2022.09.13 |
JSP 게시판 (4) 글 삭제 (0) | 2022.09.06 |
JSP 게시판 (3) 글 수정 (0) | 2022.09.06 |
JSP 게시판 (2) 글 목록, 보기 (0) | 2022.09.06 |