💡 핵심 요약 (TL;DR)
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 bdao = new BoardDAO(); int result = bdao.deleteBoard(bno, pw); System.out.println("M: DB처리완료 ->"+result); 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; } 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 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; }
|
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>
|
#TILITWILL(JAVA)