1. delete.jsp
<%@ 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 b_id = Integer.parseInt(request.getParameter("b_id"));
%>
<center>
<h1>글 삭 제 하 기</h1>
<form method="post" action="delete_ok.jsp?b_id=<%= b_id %>" name="del_frm">
<table>
<tr height="50">
<td colspan="2" align="left">
<h2>>> 암호를 입력하세요.<<</h2>
</td>
</tr>
<tr height="50">
<td width="80">암 호</td>
<td><input type="password" name="pwd" size="12" maxlength="12" /></td>
</tr>
<tr height="50">
<td colspan="2" align="center">
<input type="button" value="글삭제" onclick="delete_ok()" />
<input type="reset" value="다시작성" />
<input type="button" value="글목록" onclick="location.href='list.jsp'" />
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
2. delete_ok.jsp
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String pwd = request.getParameter("pwd");
int b_id = Integer.parseInt(request.getParameter("b_id"));
BoardDBBean bdb = BoardDBBean.getInstance();
int re = bdb.deleteBoard(pwd, b_id);
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>
<%
}
%>
3. BoardDBBean.java
public int deleteBoard(String b_pwd, int b_id) 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, b_id);
rs = pstmt.executeQuery();
if (rs.next()) {
String db_pwd = rs.getString("B_PWD"); // 속성값에 조회한 비밀번호 입력
if (db_pwd.equals(b_pwd)) {
sql = "DELETE FROM boardT WHERE B_ID = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, b_id);
pstmt.executeUpdate();
re = 1; // 번호, 비밀번호 일치
} else {
re = 0; // 비밀번호 불일치
}
} else {
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;
}
4. board.js
function delete_ok(){
if(del_frm.pwd.value.length == 0){ // 길이가 0 > 입력이 안됨
alert("비밀번호를 써주세요");
del_frm.pwd.focus();
return;
}
document.del_frm.submit();
}
'java,jsp,spring > JSP' 카테고리의 다른 글
JSP 게시판 페이징 (1) | 2022.09.13 |
---|---|
JSP 게시판 답글 기능 (1) | 2022.09.11 |
JSP 게시판 (3) 글 수정 (0) | 2022.09.06 |
JSP 게시판 (2) 글 목록, 보기 (0) | 2022.09.06 |
JSP 게시판 (1) 글쓰기 (0) | 2022.09.06 |