Servlet게시판6: 글 삭제하기

BoardFrontController.java의 doProcess()의 주소비교 후 처리부분에 코드 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	//글삭제
}else if(command.equals("/BoardDelete.bo")){
System.out.println("C : /BoardDelete.bo 호출");
System.out.println("C : .jsp -> .bo");
//페이지이동
forward = new ActionForward();
forward.setPath("./board/deleteForm.jsp");
forward.setRedirect(false);
}else if(command.equals("/BoardDeleteAction.bo")){
System.out.println("C : /BoardDeleteAction.bo 호출");
action = new BoardDeleteAction();
try{ forward = action.execute(request, response);
}catch(Exception e){e.printStackTrace(); }
}




BoardDeleteAction.java 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class BoardDeleteAction implements Action{

@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("M : BoardListAction_execute() 호출");

//전달정보 저장
int bno = Integer.parseInt(request.getParameter("bno"));
String pageNum = request.getParameter("pageNum");
String pw = request.getParameter("pw");

//BoardDAO객체생성 deleteBoard
BoardDAO bdao = new BoardDAO();
int result = bdao.deleteBoard(bno, pw);
System.out.println("M: DB처리완료 ->"+result);

//처리결과를 js사용하여 페이지 이동
//인코딩
response.setContentType("text/html; charset=UTF-8");
//출력통로생성
PrintWriter out = response.getWriter();
if(result == 0){
out.print("<script>");
out.print(" alert('비밀번호 오류');");
out.print(" history.back();");
out.print("</script>");
out.close();

return null;
}else if(result == -1){
out.print("<script>");
out.print(" alert('해당 글 없음');");
out.print(" history.back();");
out.print("</script>");
out.close();

return null;
}

//result == 1인경우
out.print("<script>");
out.print(" alert('글 삭제완료');");
out.print(" location.href='./BoardList.bo?pageNum="+pageNum+"';");
out.print("</script>");
out.close();

return null;
}

}




BoardDAO.java에 deleteBoard()메서드 작성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//글삭제구현
public int deleteBoard(int bno, String pw) {
int result = -1;
try{
getCon();
// sql (select)
sql = "select pw from itwill_board where bno=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, bno);
rs = pstmt.executeQuery();
if(rs.next()){
if(pw.equals(rs.getString("pw"))){
sql = "delete from itwill_board where bno=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, bno);
pstmt.executeUpdate();
result = 1;
System.out.println("글삭제 성공: "+result);
}else{
result = 0;
System.out.println("글삭제 중 비번불일치: "+result);
}
}else{
result = -1;
System.out.println("글삭제 중 select오류"+result);
}
}catch(Exception e){
e.printStackTrace();
}finally{
closeDB();
}
return result;
}//end of deleteBoard()




deleteForm.jsp 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%
//1. 한글처리
request.setCharacterEncoding("UTF-8");

//2. 파라미터저장
int bno = Integer.parseInt(request.getParameter("bno"));
String pageNum = request.getParameter("pageNum");

%>
<fieldset>
<legend>게시글 삭제</legend>
<form action="./BoardDeleteAction.bo?pageNum=<%=pageNum %>" method="post">
<input type="hidden" name="bno" value="<%=bno%>">
비밀번호 : <input type="password" name="pw"><br>
<input type="submit" value="글삭제하기" class="btn">
<input type="button" value="뒤로가기" class="btn" onclick="history.back()">
</form>
</fieldset>

Comments