1. edit.jsp
<%@page import="magic.board.BoardBean"%>
<%@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();
BoardBean bb = bdb.getBoard(Integer.parseInt(request.getParameter("b_id")),false);
String content = bb.getB_content();
content = content.replace("<br>","\r\n");
// textarea에 그대로 넣게 되면 <br>이 표시되어 나오기 때문에 다시 변환
%>
<!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>
<center>
<h1>글 수 정 하 기</h1>
<form method="post" action="edit_ok.jsp?b_id=<%= bb.getB_id() %>" name="write_frm">
<table>
<tr height="30">
<td width="80">작성자</td>
<td width="140">
<input type="text" id="name" name="b_name" size="10" readonly value="<%= bb.getB_name() %>"/>
</td>
<td width="80">이메일</td>
<td width="240">
<input type="text" id="email" name="b_email" size="24" readonly value="<%= bb.getB_email() %>" />
</td>
</tr>
<tr height="30">
<td width="80">글제목</td>
<td colspan="3">
<input type="text" id="title" name="b_title" size="58" value="<%= bb.getB_title() %>" />
</td>
</tr>
<tr>
<td colspan="4">
<textarea cols="70" rows="10" id="content" name="b_content" maxlength="3000"><%= content %></textarea>
</td>
<tr>
<tr height="30">
<td>
<td width="80" align="left">암호</td>
<td width="140" colspan="3" align="left">
<input type="password" id="pwd" name="b_pwd" size="12" maxlength="12"/>
</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>
2. edit_ok.jsp
<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean class="magic.board.BoardBean" id="bb"></jsp:useBean>
<% request.setCharacterEncoding("UTF-8"); %>
<%
String content = request.getParameter("b_content");
content = content.replace("\r\n", "<br>");
bb.setB_id(Integer.parseInt(request.getParameter("b_id")));
bb.setB_name(request.getParameter("b_name"));
bb.setB_email(request.getParameter("b_email"));
bb.setB_title(request.getParameter("b_title"));
bb.setB_content(content);
bb.setB_pwd(request.getParameter("b_pwd"));
BoardDBBean bdb = BoardDBBean.getInstance();
int re = bdb.editBoard(bb);
if(re == 1){
response.sendRedirect("list.jsp");
} else if(re == 0) {
%>
<script>
alert("비밀번호가 틀렸습니다.")
history.go(-1);
</script>
<%
}else if(re == -1) {
%>
<script>
alert("수정에 실패했습니다.")
history.go(-1);
</script>
<%
}
%>
- content <br> 신경 안쓰면 <jsp:setProperty property="*" name="bb">로 받아온 값을 한번에 set해도 됨
3.BoardDBBean.java
public int editBoard(BoardBean board) throws Exception {
String sql = "SELECT B_PWD FROM boardT WHERE B_ID = ?"; // 게시글에 맞는 비밀번호 조회
int re = -1; // 수정 실패
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, board.getB_id());
rs = pstmt.executeQuery();
if (rs.next()) {
String db_pwd = rs.getString("B_PWD"); // 속성값에 조회한 비밀번호 입력
if (db_pwd.equals(board.getB_pwd())) {
sql = "UPDATE boardT SET B_NAME=?, B_EMAIL=?, B_TITLE=?, B_CONTENT=? WHERE B_ID=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,board.getB_name());
pstmt.setString(2,board.getB_email());
pstmt.setString(3,board.getB_title());
pstmt.setString(4,board.getB_content());
pstmt.setInt(5, board.getB_id());
pstmt.executeUpdate();
re = 1; // 수정 성공
} else {
re = 0; // 비밀번호 불일치
}
} else {
re = -1; // 조회 실패
}
}catch (SQLException ex) {
System.out.print("수정 실패");
ex.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return re;
}
- 게시글의 id와 비밀번호가 일치하면 수정할 수 있게 함
'java,jsp,spring > JSP' 카테고리의 다른 글
JSP 게시판 답글 기능 (1) | 2022.09.11 |
---|---|
JSP 게시판 (4) 글 삭제 (0) | 2022.09.06 |
JSP 게시판 (2) 글 목록, 보기 (0) | 2022.09.06 |
JSP 게시판 (1) 글쓰기 (0) | 2022.09.06 |
JSP 게시판 (0) | 2022.09.06 |