CREATE SEQUENCE SEQ_BOARD
NOCACHE;
CREATE TABLE board (
num number PRIMARY KEY,
name varchar2(20) NOT NULL,
subject varchar2(50) NOT NULL,
content varchar2(4000) NOT NULL,
pos number,
ref number,
depth number,
regdate date,
pass varchar2(15) NOT NULL,
ip varchar2(15),
count number default 0
);
insert into board values(SEQ_BOARD.NEXTVAL, '박길동', '제목1', '내용1', 0, SEQ_BOARD.CURRVAL, 0, '2023-04-01', '1234', '0:0:0:0:0:0:0:1', default);
insert into board values(SEQ_BOARD.NEXTVAL, '김길동', '제목2', '내용2', 0, SEQ_BOARD.CURRVAL, 0, '2023-04-05', '1234', '0:0:0:0:0:0:0:1', default);
insert into board values(SEQ_BOARD.NEXTVAL, '송길동', '제목3', '내용3', 0, SEQ_BOARD.CURRVAL, 0, '2023-04-12', '1234', '0:0:0:0:0:0:0:1', default);
insert into board values(SEQ_BOARD.NEXTVAL, '한길동', '제목4', '내용4', 0, SEQ_BOARD.CURRVAL, 0, '2023-04-14', '1234', '0:0:0:0:0:0:0:1', default);
insert into board values(SEQ_BOARD.NEXTVAL, '곽길동', '제목5', '내용5', 0, SEQ_BOARD.CURRVAL, 0, '2023-04-25', '1234', '0:0:0:0:0:0:0:1', default);
insert into board values(SEQ_BOARD.NEXTVAL, '원길동', '제목6', '내용6', 0, SEQ_BOARD.CURRVAL, 0, '2023-05-04', '1234', '0:0:0:0:0:0:0:1', default);
insert into board values(SEQ_BOARD.NEXTVAL, '임길동', '제목7', '내용7', 0, SEQ_BOARD.CURRVAL, 0, '2023-05-07', '1234', '0:0:0:0:0:0:0:1', default);
insert into board values(SEQ_BOARD.NEXTVAL, '신길동', '제목8', '내용8', 0, SEQ_BOARD.CURRVAL, 0, '2023-05-11', '1234', '0:0:0:0:0:0:0:1', default);
insert into board values(SEQ_BOARD.NEXTVAL, '박길동', '제목9', '내용9', 0, SEQ_BOARD.CURRVAL, 0, '2023-05-22', '1234', '0:0:0:0:0:0:0:1', default);
insert into board values(SEQ_BOARD.NEXTVAL, '정길동', '제목10', '내용10', 0, SEQ_BOARD.CURRVAL, 0, '2023-05-29', '1234', '0:0:0:0:0:0:0:1', default);
commit;
package board;
public class Board {
private int num;
private String name;
private String subject;
private String content;
private int pos;
private int ref;
private int depth;
private String regdate;
private String pass;
private String ip;
private int count;
public Board() {
}
public Board(int num, String name, String subject, String content, int pos, int ref, int depth, String regdate,
String pass, String ip, int count) {
super();
this.num = num;
this.name = name;
this.subject = subject;
this.content = content;
this.pos = pos;
this.ref = ref;
this.depth = depth;
this.regdate = regdate;
this.pass = pass;
this.ip = ip;
this.count = count;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getPos() {
return pos;
}
public void setPos(int pos) {
this.pos = pos;
}
public int getRef() {
return ref;
}
public void setRef(int ref) {
this.ref = ref;
}
public int getDepth() {
return depth;
}
public void setDepth(int depth) {
this.depth = depth;
}
public String getRegdate() {
return regdate;
}
public void setRegdate(String regdate) {
this.regdate = regdate;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public String toString() {
return "Board [num=" + num + ", name=" + name + ", subject=" + subject + ", content=" + content + ", pos=" + pos
+ ", ref=" + ref + ", depth=" + depth + ", regdate=" + regdate + ", pass=" + pass + ", ip=" + ip
+ ", count=" + count + "]";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*, board.*" %>
<jsp:useBean id="bDao" class="board.BoardDao" />
<%
request.setCharacterEncoding("UTF-8");
int totalRecord = 0; // 전체 레코드 수(board테이블의 전체 행의 수)
int numPerPage = 10; // 1page에 보여줄 레코드 수
int pagePerBlock = 5; // 블록당 페이지 수 = [1][2][3][4][5]
int totalPage = 0; // 전체 페이지 : 레코드수 67개라면, totalPage=7 [1][2][3][4][5][6][7]
int totalBlock = 0; // 전체 블록 수 : 레코드수 67개라면, totalBlock=2
int nowPage = 1; // 현재 해당하는 페이지
int nowBlock = 1; // 현재 해당하는 블록
int start = 0; // board테이블의 select시 시작번호(한페이지에 필요한 만큼 게시물만 가져오려고)
int end = 0; // 1page에 보여줄 레코드의 갯수(보통은 10개, 맨 마지막page는 전체레코드수에 따라 달라짐)
int listSize = 0; // 현재 읽어온 게시물의 수
String keyField = "", keyWord = "";
if(request.getParameter("keyWord") != null) {
keyField = request.getParameter("keyField");
keyWord = request.getParameter("keyWord");
}
/* [처음으로]를 누르면 keyField와 keyWord에 들어있는값 지우기 */
if(request.getParameter("reload") != null) {
if(request.getParameter("reload").equals("true")){
keyField = "";
keyWord = "";
}
}
if(request.getParameter("nowPage") != null) {
nowPage = Integer.parseInt(request.getParameter("nowPage"));
}
start = (nowPage * numPerPage) - numPerPage + 1; // 각 페이지당 시작번호
end = nowPage * numPerPage; // 각 페이지당 끝나는 번호
totalRecord = bDao.getTotalCount(keyField, keyWord);
totalPage = (int)Math.ceil((double)totalRecord / numPerPage); // 전체 페이지 수
nowBlock = (int)Math.ceil((double)nowPage / pagePerBlock); // 현재 속한 블럭
totalBlock = (int)Math.ceil((double)totalPage / pagePerBlock); // 전체 블럭 계산
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판</title>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" ></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
* {margin: 0 auto;}
div {width:800px;}
h2, .cen{text-align:center; }
.m50 {margin-top:50px;}
table {margin-top: 30px; width:800px;}
table th, table td {text-align:center;}
/* .a {text-decoration:none; color:black; cursor:pointer;} */
a:link {text-decoration:none; color:rgb(53, 53, 53);}
a:hover {text-decoration:underline; color:coral; cursor:pointer;}
a:visited {text-decoration:none; color:black;}
</style>
<script>
function block(value) {
document.readFrm.nowPage.value=<%=pagePerBlock%>*(value-1)+1;
document.readFrm.action="list.jsp";
document.readFrm.submit();
}
function pageing(page) {
document.readFrm.nowPage.value=page;
document.readFrm.action="list.jsp";
document.readFrm.submit();
}
function list() {
/* document.listFrm.action = "list.jsp"; */
document.listFrm.submit();
}
function read(num) {
document.readFrm.num.value=num;
document.readFrm.action="read.jsp";
document.readFrm.submit();
}
</script>
</head>
<body>
<div>
<h2 class="m50">JSPBoard</h2><p/>
<table class="table">
<tr>
<td colspan="5" style="text-align:left;">Total : <%=totalRecord %>Articles <font color="red">(<%=nowPage %>/<%=totalPage %>)</font></td>
</tr>
<tr>
<th width="10%">번호</th>
<th width="40%">제목</th>
<th width="15%">이름</th>
<th width="25%">날짜</th>
<th width="10%">조회수</th>
</tr>
<%
ArrayList<Board> alist = bDao.getBoardList(keyField,keyWord,start,end);
listSize = alist.size();
for(int i=0; i<listSize; i++) {
Board board = alist.get(i);
String rdate = board.getRegdate().substring(0,10);
int depth = board.getDepth();
%>
<tr>
<%-- <td><%=board.getNum() %></td> --%>
<td>
<%=totalRecord-(nowPage-1)*numPerPage - i %>
</td>
<td style="text-align:left;">
<%
if(depth > 0) {
for(int j=0; j<depth; j++) {
out.print(" ");
}
out.print("↳");
}
%>
<a href="javascript:read('<%=board.getNum() %>')" class="a"><%=board.getSubject() %></a>
</td>
<td><%=board.getName() %></td>
<td><%=rdate %></td>
<td><%=board.getCount() %></td>
</tr>
<%
}
%>
<tr>
<td colspan="5" style="border:none;"><br></td>
</tr>
<tr>
<td colspan="3">
<%
int pageStart = (nowBlock-1) * pagePerBlock + 1; // 어디블록 속하는지에 따른 첫번째 페이지 1블록=1, 2블록=6, 3블럭=11
int pageEnd = pageStart + pagePerBlock <= totalPage ? (pageStart + pagePerBlock) : totalPage + 1; // 어디블록 속하는지에 따른 마지막 페이지 1블럭=5, 2블럭=10, 3블럭=15
if(totalPage != 0) {
if(nowBlock > 1) {
%>
<a href="javascript:block('<%=nowBlock-1 %>')">prev...</a> 
<%
}
for(; pageStart<pageEnd; pageStart++) {
%>
<a href="javascript:pageing('<%=pageStart %>')" class="a">[<%=pageStart %>]</a>
<%
}
if(totalBlock > nowBlock) {
%>
<a href="javascript:block('<%=nowBlock+1 %>')">...next</a> 
<%
}
}
%>
</td>
<td colspan="2" style="text-align:right;">
<a href="post.jsp" class="a">[글쓰기]</a> 
<a href="javascript:list();" class="a">[처음으로]</a>
</td>
</tr>
</table>
<form name="searchFrm" method="get" action="list.jsp">
<table align="center" width="700">
<tr>
<td align="center">
<select name="keyField">
<option value="name">이름</option>
<option value="subject">제목</option>
<option value="content">내용</option>
</select>
<input name="keyWord" required>
<input type="submit" value="찾기">
</td>
</tr>
</table>
</form>
<!-- 처음으로 누르면 화면 reload -->
<form name="listFrm">
<input type="hidden" name="reload" value="true">
<input type="hidden" name="nowPage" value="1">
</form>
<!-- 사용자가 만약 [2]를 누르면 2번째 페이지 보여주기와 상세보기 -->
<form name="readFrm">
<input type="hidden" name="num">
<input type="hidden" name="nowPage" value="<%=nowPage %>">
<input type="hidden" name="keyField" value="<%=keyField %>">
<input type="hidden" name="keyWord" value="<%=keyWord %>">
</form>
</div>
</body>
</html>
package board;
import java.sql.*;
import java.util.ArrayList;
public class BoardDao {
private DBConnectionMgr pool;
Connection con = null;
PreparedStatement pstmt = null;
Statement stmt = null;
ResultSet rs = null;
String sql = null;
public BoardDao() {
pool = DBConnectionMgr.getInstance();
}
// 게시판 리스트
public ArrayList<Board> getBoardList(String keyField,String keyWord, int start, int end) {
ArrayList<Board> alist = new ArrayList<Board>();
try {
con = pool.getConnection();
if(keyWord == null || keyWord.equals("")) {
sql = "select * "
+ " from (select ROWNUM AS RNUM, BT1.* "
+ " from (select * from board order by ref desc, pos) BT1"
+ " )"
+ " where RNUM between ? and ?";
pstmt= con.prepareStatement(sql);
pstmt.setInt(1, start);
pstmt.setInt(2, end);
} else {
sql = "select * "
+ " from (select ROWNUM AS RNUM, BT1.* "
+ " from (select * from board order by ref desc, pos) BT1"
+ " where " + keyField + " like ?"
+ " )"
+ " where RNUM between ? and ?";
pstmt= con.prepareStatement(sql);
pstmt.setString(1, "%" + keyWord + "%");
pstmt.setInt(2, start);
pstmt.setInt(3, end);
}
rs = pstmt.executeQuery();
while(rs.next()) {
Board board = new Board();
board.setNum(rs.getInt("num"));
board.setSubject(rs.getString("subject"));
board.setName(rs.getString("name"));
board.setRegdate(rs.getString("regdate"));
board.setCount(rs.getInt("count"));
board.setRef(rs.getInt("ref"));
board.setPos(rs.getInt("pos"));
board.setDepth(rs.getInt("depth"));
alist.add(board);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con);
}
return alist;
}
// 게시물 총 레코드수
public int getTotalCount(String keyField, String keyWord) {
int totalCount = 0;
try {
con = pool.getConnection();
if(keyWord == null || keyWord.equals("")) {
sql = "select count(num) from board";
pstmt = con.prepareStatement(sql);
} else {
sql = "select count(num) from board where " + keyField + " like ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, "%" + keyWord + "%");
}
rs = pstmt.executeQuery();
if(rs.next())
totalCount = rs.getInt(1);
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con);
}
return totalCount;
}
// 조회수 증가
public void upCount(int num) {
try {
con = pool.getConnection();
sql = "update board set count = count+1 where num=" + num;
con.createStatement().executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con);
}
}
// 게시물 1행 얻어오기
public Board getOneBoard(int num) {
Board board = new Board();
try {
con = pool.getConnection();
sql = "select * from board where num=" + num;
rs = con.createStatement().executeQuery(sql);
if(rs.next()) {
board.setNum(rs.getInt("num"));
board.setName(rs.getString("name"));
board.setSubject(rs.getString("subject"));
board.setContent(rs.getString("content"));
board.setPos(rs.getInt("pos"));
board.setRef(rs.getInt("ref"));
board.setDepth(rs.getInt("depth"));
board.setRegdate(rs.getString("regdate"));
board.setPass(rs.getString("pass"));
board.setIp(rs.getString("ip"));
board.setCount(rs.getInt("count"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con);
}
return board;
}
// 게시물 등록하기
public boolean insertBoard(Board board) {
boolean flag = false;
try {
con = pool.getConnection();
sql = "insert into board values(SEQ_BOARD.NEXTVAL,?,?,?,0,SEQ_BOARD.currval,0,sysdate,?,?,default)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, board.getName());
pstmt.setString(2, board.getSubject());
pstmt.setString(3, board.getContent());
pstmt.setString(4, board.getPass());
pstmt.setString(5, board.getIp());
if(pstmt.executeUpdate() == 1)
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con);
}
return flag;
}
// 게시물 수정
public void updateBoard(Board board) {
try {
con = pool.getConnection();
sql = "update board set name=?, subject=?, content=? where num=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, board.getName());
pstmt.setString(2, board.getSubject());
pstmt.setString(3, board.getContent());
pstmt.setInt(4, board.getNum());
pstmt.executeQuery();
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con);
}
}
// 답변의 위치값을 증가
public void replyPosUpdate(int ref, int pos) {
try {
con = pool.getConnection();
sql = "update board set pos = pos+1 where ref = ? and pos > ?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, ref);
pstmt.setInt(2, pos);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con);
}
}
// 게시물 댓글
public void replyBoard(Board board) {
try {
con = pool.getConnection();
sql = "insert into board values(seq_board.nextval,?,?,?,?,?,?,sysdate,?,?,default)";
int depth = board.getDepth() + 1;
int pos = board.getPos() + 1;
pstmt = con.prepareStatement(sql);
pstmt.setString(1, board.getName());
pstmt.setString(2, board.getSubject());
pstmt.setString(3, board.getContent());
pstmt.setInt(4, pos);
pstmt.setInt(5, board.getRef());
pstmt.setInt(6, depth);
pstmt.setString(7, board.getPass());
pstmt.setString(8, board.getIp());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con);
}
}
// 게시물 삭제
public boolean deleteBoard(int num) {
boolean flag = false;
try {
con = pool.getConnection();
sql = "select count(*) from board where ref=" + num;
rs = con.createStatement().executeQuery(sql);
if(rs.next()) {
if(rs.getInt(1) <= 1) {
sql = "delete from board where num=" + num;
if(con.createStatement().executeUpdate(sql) == 1)
flag = true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.freeConnection(con);
}
return flag;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="board.*" %>
<jsp:useBean id="bDao" class="board.BoardDao" />
<%
request.setCharacterEncoding("UTF-8");
int num = Integer.parseInt(request.getParameter("num"));
String nowPage = request.getParameter("nowPage");
String keyField = request.getParameter("keyField");
String keyWord = request.getParameter("keyWord");
bDao.upCount(num);
Board board = bDao.getOneBoard(num);
session.setAttribute("bean", board);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글읽기</title>
<style>
* {margin: 0 auto;}
table {margin-top: 50px; width:600px; }
table th {height:40px; background-color: #B778FF; color:white;}
table td {text-align:center; height:30px;}
.col {background-color: #E8D9FF;}
.col2 {background-color: #F6F6F6;}
a {text-decoration:none; color:black;}
a:hover {text-decoration:underline; color:coral; cursor:pointer;}
</style>
<script>
function list() {
document.listFrm.submit();
}
</script>
</head>
<body>
<table>
<tr>
<th colspan="4">글 읽 기</th>
</tr>
<tr>
<td width="15%" class="col">이름</td>
<td width="35%" class="col2"><%=board.getName() %></td>
<td width="15%" class="col">등록날짜</td>
<td width="35%" class="col2"><%=board.getRegdate().substring(0,10) %></td>
</tr>
<tr>
<td width="15%" class="col">제목</td>
<td colspan="3" class="col2" style="text-align:left; padding-left:10px;"><%=board.getSubject() %></td>
</tr>
<tr>
<td colspan="4" style="height:120px; text-align:left;">
본문내용 :<br>
<%=board.getContent() %>
</td>
</tr>
<tr>
<td colspan="4" style="text-align:right;"><%=board.getIp() %>로 부터 글을 남기셨습니다 / 조회수 : <%=board.getCount() %> </td>
</tr>
<tr>
<td colspan="4"><hr><br>
[ <a href="javascript:list()">리스트</a> |
<a href="update.jsp?nowPage=<%=nowPage%>&num=<%=num%>">수 정</a> |
<a href="reply.jsp?nowPage=<%=nowPage%>">답 변</a> |
<a href="delete.jsp?nowPage=<%=nowPage%>&num=<%=num%>"> 삭 제</a> ]
</td>
</tr>
</table>
<form name="listFrm" action="list.jsp">
<input type="hidden" name="nowPage" value="<%=nowPage %>">
<%
if( !(keyWord==null || keyWord.equals("")) ) {
%>
<input type="hidden" name="keyField" value="<%=keyField %>">
<input type="hidden" name="keyWord" value="<%=keyWord %>">
<%
}
%>
</form>
</body>
</html>
<%@ 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>
<style>
table{width:600px; margin-top: 50px;}
table th{background-color:#0000C9; height:40px; color:white;}
table td {height:30px;}
textarea {resize: none;}
</style>
</head>
<body>
<form action="boardPost" method="post">
<table align="center">
<tr>
<th colspan="2">글 쓰 기</th>
</tr>
<tr>
<td colspan="2"><br></td>
</tr>
<tr>
<td width="15%">성명</td>
<td><input name="name"></td>
</tr>
<tr>
<td>제목</td>
<td><input name="subject"></td>
</tr>
<tr>
<td>내용</td>
<td><textarea name="content" rows="10" cols="50"></textarea></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td colspan="2"><br><br><hr></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="등록"> 
<input type="reset" value="다시쓰기"> 
<input type="button" value="리스트" onclick="location.href='list.jsp'">
</td>
</tr>
</table>
<input type="hidden" name="ip" value="<%=request.getRemoteAddr() %>">
</form>
</body>
</html>
package board;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class BoardPostServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Board board = new Board();
board.setName(request.getParameter("name"));
board.setSubject(request.getParameter("subject"));
board.setContent(request.getParameter("content"));
board.setPass(request.getParameter("pass"));
board.setIp(request.getParameter("ip"));
boolean result = new BoardDao().insertBoard(board);
response.sendRedirect("list.jsp");
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="board.*" %>
<%
String nowPage = request.getParameter("nowPage");
Board board = (Board)session.getAttribute("bean");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
table{width:600px; margin-top: 50px;}
table th{background-color:#DBC000; height:40px;}
table td {height:30px;}
textarea {resize: none;}
</style>
</head>
<body>
<form action="boardReply" method="post">
<table align="center">
<tr>
<th colspan="2">댓 글 등 록</th>
</tr>
<tr>
<td colspan="2"><br></td>
</tr>
<tr>
<td width="15%">성명</td>
<td><input name="name"></td>
</tr>
<tr>
<td>제목</td>
<td><input name="subject" value="답변 : <%=board.getSubject() %>"></td>
</tr>
<tr>
<td>내용</td>
<td>
<textarea name="content" rows="10" cols="50">
<%=board.getContent() %>
========= 댓글을 작성하세요 ===========
</textarea>
</td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="pass" required></td>
</tr>
<tr>
<td colspan="2"><br><br><hr></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="답변등록"> 
<input type="reset" value="다시쓰기"> 
<input type="button" value="뒤로" onclick="history.go(-1)">
</td>
</tr>
</table>
<!-- 앞에서 넘어온 값 -->
<input type="hidden" name="nowPage" value="<%=nowPage %>">
<!-- 댓글을 쓰는 작성자의 ip -->
<input type="hidden" name="ip" value="<%=request.getRemoteAddr() %>">
<!-- 댓글을 쓰는 부모의 정보 -->
<input type="hidden" name="ref" value="<%=board.getRef() %>">
<input type="hidden" name="pos" value="<%=board.getPos() %>">
<input type="hidden" name="depth" value="<%=board.getDepth() %>">
</form>
</body>
</html>
package board;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
public class BoardReplyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Board board = new Board();
board.setName(request.getParameter("name"));
board.setSubject(request.getParameter("subject"));
board.setContent(request.getParameter("content"));
board.setPass(request.getParameter("pass"));
board.setRef(Integer.parseInt(request.getParameter("ref")));
board.setPos(Integer.parseInt(request.getParameter("pos")));
board.setDepth(Integer.parseInt(request.getParameter("depth")));
board.setIp(request.getParameter("ip"));
BoardDao bDao = new BoardDao();
bDao.replyPosUpdate(board.getRef(), board.getPos());
bDao.replyBoard(board);
response.sendRedirect("list.jsp?nowPage=" + request.getParameter("nowPage"));
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="board.*" %>
<%
int num = Integer.parseInt(request.getParameter("num"));
String nowPage = request.getParameter("nowPage");
Board board = (Board)session.getAttribute("bean");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
table{width:600px; margin-top: 50px;}
table th{background-color:#813477; height:40px; color:white;}
table td {height:30px;}
textarea {resize: none;}
</style>
</head>
<body>
<form action="boardUpdate" method="post">
<table align="center">
<tr>
<th colspan="2">수 정 하 기</th>
</tr>
<tr>
<td colspan="2"><br></td>
</tr>
<tr>
<td width="15%">성명</td>
<td><input name="name" value="<%=board.getName() %>"></td>
</tr>
<tr>
<td>제목</td>
<td><input name="subject" value="<%=board.getSubject() %>"></td>
</tr>
<tr>
<td>내용</td>
<td><textarea name="content" rows="10" cols="50"><%=board.getContent() %></textarea></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="pass" required> 수정시에는 비밀번호가 필요합니다.</td>
</tr>
<tr>
<td colspan="2"><br><br><hr></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="수정완료"> 
<input type="reset" value="다시수정"> 
<input type="button" value="뒤로" onclick="history.go(-1)">
</td>
</tr>
</table>
<input type="hidden" name="num" value="<%=num %>">
<input type="hidden" name="nowPage" value="<%=nowPage %>">
</form>
</body>
</html>
package board;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
public class BoardUpdateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
String nowPage = request.getParameter("nowPage");
Board upBean = new Board();
upBean.setNum(Integer.parseInt(request.getParameter("num")));
upBean.setName(request.getParameter("name"));
upBean.setSubject(request.getParameter("subject"));
upBean.setContent(request.getParameter("content"));
upBean.setPass(request.getParameter("pass")); // read.jsp에서 사용자가 넣은 pass
HttpSession session = request.getSession();
Board inBean = (Board)session.getAttribute("bean"); // DB에 저장되어있는 pass
if(inBean.getPass().equals(upBean.getPass())) {
new BoardDao().updateBoard(upBean);
String url = "read.jsp?nowPage=" + nowPage + "&num=" + upBean.getNum();
response.sendRedirect(url);
} else {
out.println("<script>");
out.println(" alert('비밀번호가 맞지 않습니다.')");
out.println(" history.back()");
out.println("</script>");
}
}
}