--------------------------------------------/WEB-INF/CommandPro.properties 추가
/mvc2bbs/deleteForm.do=my.action.DeleteFormAction

------------------------------------------------------------DeleteFormAction.java
package my.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeleteFormAction implements CommandAction {

    @Override
    public String requestPro(HttpServletRequest request,
                    HttpServletResponse response) throws Throwable {
        int num=Integer.parseInt(request.getParameter("num"));
        String pageNum=request.getParameter("pageNum");
       
        request.setAttribute("num", new Integer(num));
        request.setAttribute("pageNum", new Integer(pageNum));
       
        return "deleteForm.jsp";       
    }
}
------------------------------------------------------------deleteForm.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/view/color.jspf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<link href="style.css" rel="stylesheet" type="text/css">
<title>게시판</title>
<script language="javaScript">
function deleteSave(){
    if(document.delForm.passwd.value==''){
        alert("비밀번호를 입력하십시요.");
        document.delForm.passwd.focus();
        return false;
    }
}
</script>
</head>
<body bgcolor="${bodyback_c }">
<center><b>글삭제</b></center>
<br/>
<form method="post" name="delForm" action="/mvc2bbs/deletePro.do?pageNum=${pageNum }" onsubmit="return deleteSave()">
<table border="1" align="center" cellspacing="0" cellpadding="0" width="360">
    <tr height="30">
        <td align="center" bgcolor="${value_c }">
            <b>비밀번호를 입력해주세요.</b>
        </td>
    </tr>
    <tr height="30">
        <td align="center"> 비밀번호:
            <input type="password" name="passwd" size="8" maxlength="12">
            <input type="hidden" name="num" value="${num }">
        </td>
    </tr>
    <tr height="30">
        <td align="center" bgcolor="${value_c }">
            <input type="submit" value="글삭제">
            <input type="button" value="글목록" onClick="document.location.href='/mvc2bbs/list.do?pageNum=${pageNum}'">
        </td>
    </tr>  
</table>
</form>
</body>
</html>
--------------------------------------------/WEB-INF/CommandPro.properties 추가
/mvc2bbs/deletePro.do=my.action.DeleteProAction

------------------------------------------------------------DeleteProAction.java
package my.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import my.board.BoardDBBean;

public class DeleteProAction implements CommandAction {

    @Override
    public String requestPro(HttpServletRequest request,
            HttpServletResponse response) throws Throwable {
        request.setCharacterEncoding("euc-kr");
       
        int num=Integer.parseInt(request.getParameter("num"));
        String pageNum=request.getParameter("pageNum");
        String passwd=request.getParameter("passwd");
       
        BoardDBBean dbPro=BoardDBBean.getInstance();
        int check=dbPro.deleteArticle(num,passwd);
       
        //해당뷰에서 사용할 속성
        request.setAttribute("pageNum", new Integer(pageNum));
        request.setAttribute("check", new Integer(check));
       
        return "deletePro.jsp";//해당뷰
    }

}
------------------------------------------------------------BoardDBBean.java추가
//글삭제
public int deleteArticle(int num, String passwd)throws Exception{
    Connection conn=null;
    PreparedStatement pstmt=null;
    ResultSet rs=null;
    String dbpasswd="";
    int x=-1;
    try{
        conn=getConnection();
        pstmt=conn.prepareStatement("SELECT passwd FROM board WHERE num=?");
        pstmt.setInt(1, num);
        rs=pstmt.executeQuery();
                   
        if(rs.next()){
            dbpasswd=rs.getString("passwd");
            if(dbpasswd.equals(passwd)){
                pstmt=conn.prepareStatement("DELETE FROM board WHERE num=?");
                pstmt.setInt(1, num);
                pstmt.executeUpdate();
                x=1;//글삭제 성공
            }
            else{
                x=0;//비밀번호 틀림
            }
        }
    }catch(Exception ex){
        ex.printStackTrace();
    }finally{
        if(rs!=null)try{rs.close();}catch(SQLException e){}
        if(pstmt!=null)try{pstmt.close();}catch(SQLException e){}
        if(conn!=null)try{conn.close();}catch(SQLException e){}
    }
    return x;
}//deleteArticle
------------------------------------------------------------deleteProjsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:if test="${check==1}">
    <meta http-equiv="Refresh" content="0;url=/mvc2bbs/list.do?pageNum=${pageNum }">
</c:if>
<c:if test="${check==0}">
     비밀번호가 다릅니다.
    <br>
    <a href="javascript:history.go(-1)">[글 삭제폼으로 돌아가기]</a>
</c:if>

 

 

+ Recent posts