Please refer to my last post to learn what we did the last time. 

2022.09.26 - [Codes & Projects] - JSP / EL / JSTL / HTML / JavaScript / Oracle) Model2 Bulletin Board(1)

 

Before we start the following process, we will take a quick look at BoardListAction.java.

package service;

import java.util.List;

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

import dao.BoardDAO;
import model.BoardBean;

public class BoardListAction implements Action{

	@Override
	public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("BoardListAction");
		
		int page = 1;			// Current Page
		int limit = 10;			// How many pages will be printed? 10
		
		if(request.getParameter("page") != null) {
			 page = Integer.parseInt(request.getParameter("page"));
		}

		// ex) page = 1 : startRow = 1, endRow : 10
		int startRow = (page -1) * limit + 1;
		int endRow = page * limit;

		BoardDAO dao = BoardDAO.getInstance();
		int listcount = dao.getCount();	// Total data volume
		System.out.println("listcount: "+listcount);
		
		//To get List 
		List<BoardBean> boardlist = dao.getList(startRow, endRow);
		System.out.println("boardlist: "+boardlist);

		//Total pages number
		int pageCount = listcount / limit + (( listcount%limit == 0 )? 0 : 1); //if the result of listcount%limit is 0, increase 0, otherwise increase 1.

		int startPage = ((page-1)/ 10) * limit + 1; // 1, 11, 22...
		int endPage = startPage + 10 - 1; 	// 10, 20, 30...

		if(endPage > pageCount) endPage = pageCount;
		
		request.setAttribute("page", page);
		request.setAttribute("listcount", listcount);
		request.setAttribute("boardlist", boardlist);
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("startPage", startPage);
		request.setAttribute("endPage", endPage);
		
			ActionForward forward = new ActionForward();
			forward.setRedirect(false);  // Dispatcher method 
			forward.setPath("./board/qna_board_list.jsp");
			
			
		
		return forward;
	}
	
}

There are six variables: startRow, endRow, listcount, page count, startPage, endPage, and post number.

In terms of the listing, these variables are necessary. 

 

Now, let us get started on the next steps. So far, we are here, we can write posts, and we can see the list. 

Now, let us get started on the next steps.

 

1. Add pagination function in qna_board_list.jsp.

<!-- Pagination -->
<center>
	<c:if test="${listcount > 0 }">
		<!-- To move to the page 1 -->
		<a href="./BoardListAction.do?page=1" style="text-decoration: none">
			<< </a>
		<!--  Previous page -->
		<c:if test="${startPage > 10 }">
			<a href="./BoardListAction.do?page=${startPage-10}">[Prev]</a>
		</c:if>
		<!-- every block : 10 posts -->
		<c:forEach var="i" begin="${startPage}" end="${endPage}">
			<c:if test="${i == page}">
				<!-- current page -->
			[${i}]
			</c:if>
			<c:if test="${i != page}">
				<!-- Not a current page -->
				<a href="./BoardListAction.do?page=${i }"> [${i }]</a>
			</c:if>

		</c:forEach>

		<!-- Next page -->
		<c:if test="${endPage < pageCount }">
			<a href="./BoardListAction.do?page=${startPage+10}">[Next]</a>
		</c:if>
		<!-- To move to the last page -->
		<a href="./BoardListAction.do?page=${pageCount}"> >> </a>
	</c:if>
</center>

2. Create BoardDeatilAction service class in src - service.

3. Add a detail page part in the controller class.

//Detail page
		}else if(command.equals("/BoardDetailAction.do")) {
			try {
				action = new BoardDetailAction();
				forward = action.execute(request, response);
			}catch(Exception e) {
				e.printStackTrace();
			}
		}

3. Add a readCountUpdate and getDetail methods in the DAO class.

// View count + 1
	public void readcountUpdate(int board_num) {
		Connection con = null;
		PreparedStatement pstmt = null;
		
		try {
			con = getConnection();
			
			String sql="update model2 set board_readcount=board_readcount+1 ";
				   sql+=" where board_num = ?";
				   
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, board_num);
			pstmt.executeUpdate(); // To execute SQL
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(pstmt != null) try { pstmt.close();} catch(Exception e) {}
			if(con != null) try { con.close();} catch(Exception e) {}
		}
	}
	
// Detail page 
	public BoardBean getDetail(int board_num) {
		BoardBean board = new BoardBean();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			con = getConnection();
			
			String sql="select * from model2 where board_num=?";
			
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, board_num);
			rs= pstmt.executeQuery();		// To execute SQL
			
			if(rs.next()) {

				   board.setBoard_num(rs.getInt("board_num"));
				   board.setBoard_name(rs.getString("board_name"));
				   board.setBoard_pass(rs.getString("board_pass"));
				   board.setBoard_subject(rs.getString("board_subject"));
				   board.setBoard_content(rs.getString("board_content"));
				   board.setBoard_file(rs.getString("board_file"));
				   board.setBoard_re_ref(rs.getInt("board_re_ref"));
				   board.setBoard_re_lev(rs.getInt("board_re_lev"));
				   board.setBoard_re_seq(rs.getInt("board_re_seq"));
				   board.setBoard_readcount(rs.getInt("board_readcount"));
				   board.setBoard_date(rs.getTimestamp("board_date"));
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(pstmt != null) try { pstmt.close();} catch(Exception e) {}
			if(con != null) try { con.close();} catch(Exception e) {}
			if(rs != null) try { rs.close();} catch(Exception e) {}
		}
		return board;
	}

4. Create a qna_board_view.jsp.

<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Detail page</title>
</head>
<body>
	<table border=1 width=400 align=center>
		<caption>Detail page</caption>
		<tr>
			<td>Title</td>
			<td>${board.board_subject }</td>
		</tr>
		<tr>
			<td>Content</td>
			<td><pre>${board.board_content }</pre> <!-- Method 1 -->
				${content } <!-- Method 2 --></td>
		</tr>
		<tr>
			<td>Attached File</td>
			<!-- If there is an attached file -->
			<c:if test="${board.board_file != null }">
				<a href="./board/file_down.jsp?file_name=${board.board_file }">
					${board.board_file} </a>
			</c:if>
		</tr>
		<tr>
			<td colspan=2 align=center>
				<input type="button" value="Reply"
				onClick="location.href='./BoardReplyAction.do?board_num=${board.board_num}&page=${page}'">
				
				<input type="button" value="Edit"
				onClick="location.href='./BoardModifyAction.do?board_num=${board.board_num}&page=${page}'"> 
				
				<input type="button" value="Delete"
				onClick="location.href='./BoardDeleteAction.do?board_num=${board.board_num}&page=${page}'"> 
				
				<input type="button" value="Go back to list"
				onClick="location.href='./BoardListAction.do?page=${page}'">

			</td>
		</tr>
	</table>


</body>
</html>

5.Create BoardReplyAction.jsp for processing Replies.

package service;

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

import dao.BoardDAO;
import model.BoardBean;

public class BoardReplyAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("BoardReplyAction");

		int board_num = Integer.parseInt(request.getParameter("board_num"));
		String page = request.getParameter("page");

		BoardDAO dao = BoardDAO.getInstance();

		// To get the detail of the main post
		BoardBean board = dao.getDetail(board_num);

		// Share
		request.setAttribute("board", board);
		request.setAttribute("page", page);

		ActionForward forward = new ActionForward();
		forward.setRedirect(false); // dispatcher method
		forward.setPath("./board/qna_board_reply.jsp");

		return forward;
	}

}

6. Add the reply part in the controller class.

// Reply form
		} else if (command.equals("/BoardReplyAction.do")) {
			try {
				action = new BoardReplyAction();
				forward = action.execute(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

7. Create qna_board_reply.jsp.

<%@ page language="java" contentType="text/html; charset=utf-8"%>

<html>
<head>
<title>MVC Bulletin Board</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="<%=request.getContextPath()%>/board/script.js"></script>
<style>
@import
	url('https://fonts.googleapis.com/css2?family=Abril+Fatface&display=swap')
	;
</style>
</head>
<body>

	<form action="<%=request.getContextPath()%>/BoardReply.do"
		method="post">
		<input type="hidden" name="board_num" value="${board.board_num}">
		<input type="hidden" name="page" value="${page}"> <input
			type="hidden" name="board_re_ref" value="${board.board_re_ref}"> <input
			type="hidden" name="board_re_lev" value="${board.board_re_lev}"> <input
			type="hidden" name="board_re_seq" value="${board.board_re_seq}">

		<table cellpadding="0" cellspacing="0" align=center border=1>
			<tr align="center" valign="middle">
				<td style="font-family: 'Abril Fatface', cursive;" colspan="5">MVC
					Bulletin Board</td>
			</tr>
			<tr>
				<td style="font-family: 'Abril Fatface', cursive; font-size: 12"
					height="16">
					<div align="center">Writer</div>
				</td>
				<td><input name="board_name" id="board_name" type="text"
					size="10" maxlength="10" value="" /></td>
			</tr>
			<tr>
				<td style="font-family: 'Abril Fatface', cursive; font-size: 12"
					height="16">
					<div align="center">Password</div>
				</td>
				<td><input name="board_pass" id="board_pass" type="password"
					size="10" maxlength="10" value="" /></td>
			</tr>
			<tr>
				<td style="font-family: 'Abril Fatface', cursive; font-size: 12"
					height="16">
					<div align="center">Title</div>
				</td>
				<td><input name="board_subject" id="board_subject" type="text"
					size="50" maxlength="100" value="re." /></td>
			</tr>
			<tr>
				<td style="font-family: 'Abril Fatface', cursive; font-size: 12">
					<div align="center">Content</div>
				</td>
				<td><textarea name="board_content" id="board_content" cols="67"
						rows="15"></textarea></td>
			</tr>
			<tr>
				<td style="font-family: 'Abril Fatface', cursive; font-size: 12">
					<div align="center">Attach File</div>
				</td>
				<td><input name="board_file" type="file" /></td>
			</tr>
			<tr bgcolor="cccccc">
				<td colspan="2" style="height: 1px;"></td>
			</tr>
			<tr>
				<td colspan="2">&nbsp;</td>
			</tr>
			<tr align="center" valign="middle">
				<td colspan="5"><input type=submit value="Reply"> <input
					type=reset value="Cancel"></td>
			</tr>
		</table>
	</form>

</body>
</html>

8. Create BoardReply service class.

package service;

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

import dao.BoardDAO;
import model.BoardBean;

public class BoardReply implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("BoardReply");
		
		request.setCharacterEncoding("utf-8");

		int board_num = Integer.parseInt(request.getParameter("board_num"));
		int board_re_ref = Integer.parseInt(request.getParameter("board_re_ref"));
		int board_re_lev = Integer.parseInt(request.getParameter("board_re_lev"));
		int board_re_seq = Integer.parseInt(request.getParameter("board_re_seq"));
		String page = request.getParameter("page");
		
		BoardBean board = new BoardBean();
		board.setBoard_num(board_num);
		board.setBoard_re_ref(board_re_ref);
		board.setBoard_re_lev(board_re_lev);
		board.setBoard_re_seq(board_re_seq);
		board.setBoard_name(request.getParameter("board_name"));
		board.setBoard_pass(request.getParameter("board_pass"));
		board.setBoard_subject(request.getParameter("board_subject"));
		board.setBoard_content(request.getParameter("board_content"));
		
		BoardDAO dao = BoardDAO.getInstance();
		int result = dao.boardReply(board);
		if(result == 1) System.out.println("Successfully replied.");
		
		
		ActionForward forward = new ActionForward();
		forward.setRedirect(false);
		forward.setPath("./BoardListAction.do?page="+page);
		
		return forward;
	}

}

9. In the controller class, add the reply part. 

// Writing replies
		}else if(command.equals("/BoardReply.do")){
			try {
				action = new BoardReply();
				forward = action.execute(request, response);
			}catch(Exception e) {
				
			}
		}

10. In the DAO class, add boardReply method.

// Reply 
	public int boardReply(BoardBean board) {
		int result = 0;
		Connection con = null;
		PreparedStatement pstmt = null;
		
		// Detail about the main post
		int re_ref = board.getBoard_re_ref();	// Post group number	
		int re_lev = board.getBoard_re_lev();	// Depth of reply
		int re_seq = board.getBoard_re_seq(); 	// Print sequence
		
		try {
			con = getConnection();
			
			String sql="update model2 set board_re_seq=board_re_seq+1 ";
				   sql+=" where board_re_ref=? and board_re_seq > ?";
			
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, re_ref);
			pstmt.setInt(2, re_seq);
			pstmt.executeUpdate();
			
			sql="insert into model2 values(model2_seq.nextval, ";
			sql+=" ?,?,?,?,?,?,?,?,?,sysdate)";
			
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, board.getBoard_name());
			pstmt.setString(2, board.getBoard_pass());
			pstmt.setString(3, board.getBoard_subject());
			pstmt.setString(4, board.getBoard_content());
			pstmt.setString(5, "");				// board_file : null
			pstmt.setInt(6, re_ref);	   
			pstmt.setInt(7, re_lev+1);	   
			pstmt.setInt(8, re_seq+1);	   
			pstmt.setInt(9, 0);					// board_readcount
			
			result = pstmt.executeUpdate(); 	// To execute SQL
			
		}catch(Exception e) {
			
		}finally {
			if(pstmt != null) try { pstmt.close();} catch(Exception e) {}
			if(con != null) try { con.close();} catch(Exception e) {}
		}
		
		return result;
	}

11. Create BoardModifyAction service class for Editing the post.

package service;

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

import dao.BoardDAO;
import model.BoardBean;

public class BoardModifyAction implements Action{

	@Override
	public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("BoardModifyAction");
		
		int board_num = Integer.parseInt(request.getParameter("board_num"));
		String page = request.getParameter("page");
		
		BoardDAO dao = BoardDAO.getInstance();
		
		// Detail info
		BoardBean board = dao.getDetail(board_num);
		
		//Share
		request.setAttribute("board", board);
		request.setAttribute("page", page);
		
		ActionForward forward = new ActionForward();
		forward.setRedirect(false); 		//dispatcher method
		forward.setPath("./board/qna_board_modify.jsp");
		
		return forward;
	}

}

12. Add the part in the Controller class.

// Edit form
		}else if(command.equals("/BoardModifyAction.do")) {
			try {
				action = new BoardModifyAction();
				forward = action.execute(request, response);
			}catch(Exception e) {
				e.printStackTrace();
			}
		}

 

It is like factory manufacturing. 

 

13. Create qna_board_modify.jsp.

<%@ page language="java" contentType="text/html; charset=utf-8"%>

<html>
<head>
<title>Edit My post</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="<%=request.getContextPath()%>/board/script.js"></script>
<style>
@import
	url('https://fonts.googleapis.com/css2?family=Abril+Fatface&display=swap')
	;
</style>
</head>
<body>

	<form action="<%=request.getContextPath()%>/BoardModify.do"
		method="post">
		<input type="hidden" name="board_num" value="${board.board_num}">
		<input type="hidden" name="page" value="${page}"> 
		<input
			type="hidden" name="board_re_ref" value="${board.board_re_ref}">
		<input type="hidden" name="board_re_lev" value="${board.board_re_lev}">
		<input type="hidden" name="board_re_seq" value="${board.board_re_seq}">

		<table cellpadding="0" cellspacing="0" align=center border=1>
			<tr align="center" valign="middle">
				<td style="font-family: 'Abril Fatface', cursive;" colspan="5">Edit MVC
					Bulletin Board</td>
			</tr>
			<tr>
				<td style="font-family: 'Abril Fatface', cursive; font-size: 12"
					height="16">
					<div align="center">Writer</div>
				</td>
				<td><input name="board_name" id="board_name" type="text"
					size="10" maxlength="10" value="${board.board_name}" /></td>
			</tr>
			<tr>
				<td style="font-family: 'Abril Fatface', cursive; font-size: 12"
					height="16">
					<div align="center">Password</div>
				</td>
				<td><input name="board_pass" id="board_pass" type="password"
					size="10" maxlength="10" value="" /></td>
			</tr>
			<tr>
				<td style="font-family: 'Abril Fatface', cursive; font-size: 12"
					height="16">
					<div align="center">Title</div>
				</td>
				<td><input name="board_subject" id="board_subject" type="text"
					size="50" maxlength="100" value="${board.board_subject}" /></td>
			</tr>
			<tr>
				<td style="font-family: 'Abril Fatface', cursive; font-size: 12">
					<div align="center">Content</div>
				</td>
				<td><textarea name="board_content" id="board_content" cols="67"
						rows="15">${board.board_content}</textarea></td>
			</tr>
			
			<tr align="center" valign="middle">
				<td colspan="5"><input type=submit value="Edit"> <input
					type=reset value="Cancel"></td>
			</tr>
		</table>
	</form>

</body>
</html>

14. Create BoardModify service class and updateresult.jsp.

package service;

import java.io.PrintWriter;

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

import dao.BoardDAO;
import model.BoardBean;

public class BoardModify implements Action{

	@Override
	public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("BoardModify");
		
		response.setContentType("text/html; charset=utf-8");
		request.setCharacterEncoding("utf-8");
		
		PrintWriter out = response.getWriter();   //출력스트림 생성
		
		int board_num = Integer.parseInt(request.getParameter("board_num"));
		String page = request.getParameter("page");
		String board_pass = request.getParameter("board_pass");
		
		BoardBean board = new BoardBean();
		board.setBoard_num(board_num);
		board.setBoard_name(request.getParameter("board_name"));
		board.setBoard_subject(request.getParameter("board_subject"));
		board.setBoard_content(request.getParameter("board_content"));
		
		//To bring the saved password
		BoardDAO dao = BoardDAO.getInstance();
		BoardBean old = dao.getDetail(board_num);
		
		//To compare the password
		if(old.getBoard_pass().equals(board_pass)) {	// Correct password
			int result = dao.update(board); 
			if(result ==1) System.out.println("Successfully edited.");
			/*
			 * out.println("<script>");
			 * out.println("location.href='./board/updateresult.jsp?'");
			 * out.println("</script>"); out.close();
			 */
			
			response.sendRedirect("./board/updateresult.jsp?page="+page);
			
			return null;
			
		}else {											// Incorrect password
			out.println("<script>");
			out.println("alert('Incorrect password.');");
			out.println("history.go(-1);");
			out.println("</script>");
			out.close();
			
			return null;
		}

	//	ActionForward forward = new ActionForward();
	//	forward.setRedirect(false);
	//	forward.setPath("/BoardListAction.do?page="+page);	//To load to the list
	//	forward.setPath("/BoardDetailAction.do?board_num="+board_num+"&page="+page);	//To load to the detail page
		
	//	return forward;
	}

}

updateresult.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<script>
		//alert(${param.page});	
		alert("Your post is successfully edited.");
		location.href="<%=request.getContextPath()%>/BoardListAction.do?page=${param.page}";
	</script>
</body>
</html>

15.  Add the part in the Controller class.

// Edit 
		}else if(command.equals("/BoardModify.do")){
			try {
				action = new BoardModify();
				forward = action.execute(request, response);
			}catch(Exception e) {
				e.printStackTrace();
			}
        
// update.do
		}else if(command.equals("/update.do")) {
				forward = new ActionForward();
				forward.setRedirect(false);
				forward.setPath("./board/updateresult.jsp");
			try {
				
			}catch(Exception e) {
				e.printStackTrace();
			}

16. Add the update method in the DAO class.

//Update 
	public int update(BoardBean board) {
		int result = 0;
		Connection con = null;
		PreparedStatement pstmt = null;
		
		try {
			con = getConnection(); 		//Connect to the Database
			
			String sql="update model2 set board_name=?, board_subject=?,";
				   sql+="board_content=? where board_num=?";
				   
				   pstmt = con.prepareStatement(sql);
				   pstmt.setString(1, board.getBoard_name());
				   pstmt.setString(2, board.getBoard_subject());
				   pstmt.setString(3, board.getBoard_content());
				   pstmt.setInt(4, board.getBoard_num());
				   result = pstmt.executeUpdate();		//To execute SQL 
					
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			if(pstmt != null) try { pstmt.close();} catch(Exception e) {}
			if(con != null) try { con.close();} catch(Exception e) {}
	}
	return result;
}

Now, let us create a delete part.

17. Create BoardDelectAction service class to load the link when you click the "Delete" button.

package service;

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

public class BoardDeleteAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("BoardDeleteAction");
		ActionForward forward = new ActionForward();
		
		return forward;
	}

}

18. Go to the controller class, and add the delete form part.

// Delete form 
		}else if(command.equals("/BoardDeleteAction.do")){
			forward = new ActionForward();
			forward.setRedirect(false);
			forward.setPath("./board/qna_board_delete.jsp");
		}
// Delete.do
		else if(command.equals("/BoardDelete.do)")){
			try{
				action = new BoardDelete();
				forward = action.execute(request, response);
			}catch(Exception e) {
				e.printStackTrace();
			}
		}

19. Create qna_board_delete.jsp.

<%@ page language="java" contentType="text/html; charset=utf-8"%>

<html>
<head>
<title>Edit My post</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="<%=request.getContextPath()%>/board/script.js"></script>
<style>
@import
	url('https://fonts.googleapis.com/css2?family=Abril+Fatface&display=swap')
	;
</style>
</head>
<body>

board_num1 : <%=request.getParameter("board_num") %><br>
page1 : <%=request.getParameter("page") %><br>

board_num2 : ${param.board_num }<br>
page2 : ${param.page }<br>

	<form action="<%=request.getContextPath()%>/BoardDelete.do"
		method="post">
		<input type="hidden" name="board_num" value="${param.board_num}">
		<input type="hidden" name="page" value="${param.page}">

		<table cellpadding="0" cellspacing="0" align=center border=1>
			<tr align="center" valign="middle">
				<td style="font-family: 'Abril Fatface', cursive;" colspan="5">Delete
					MVC Bulletin Board</td>
			</tr>
			<tr>
				<td style="font-family: 'Abril Fatface', cursive; font-size: 12"
					height="16">
					<div align="center">Password</div>
				</td>
				<td><input name="board_pass" id="board_pass" type="password"
					size="10" maxlength="10" value="" /></td>
			</tr>
			
			<tr align="center" valign="middle">
				<td colspan="5"><input type=submit value="Delete"> <input
					type=reset value="Cancel"></td>
			</tr>
		</table>
	</form>

</body>
</html>

20. Create BoardDelete service class.

package service;

import java.io.File;
import java.io.PrintWriter;

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

import dao.BoardDAO;
import model.BoardBean;

public class BoardDelete implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("BoardDelete");

		response.setContentType("text/html; charset=utf-8");
		request.setCharacterEncoding("utf-8");

		PrintWriter out = response.getWriter();

		int board_num = Integer.parseInt(request.getParameter("board_num"));
		String page = request.getParameter("page");
		String board_pass = request.getParameter("board_pass");

		String path = request.getRealPath("boardupload");
		System.out.println("path:" + path);

		BoardDAO dao = BoardDAO.getInstance();
		BoardBean old = dao.getDetail(board_num); // To get detail

		// To compare passwords
		if (old.getBoard_pass().equals(board_pass)) { // Correct password
			int result = dao.delete(board_num); // Delete SQL

			// If there is an attached file -> delete
			if (old.getBoard_file() != null) {

				File file = new File(path);
				file.mkdir(); // makedirectory

				// To bring boardupload directory
				File[] f = file.listFiles();
				for (int i = 0; i < f.length; i++) {
					if (f[i].getName().equals(old.getBoard_file())) {
						f[i].delete(); // To delete the attached file
					}
				}
			}

		} else { // Incorrect password
			out.println("<script>");
			out.println("alert('Incorrect password.');");
			out.println("history.go(-1);");
			out.println("</script>");
			out.close();

			return null;
		}

		ActionForward forward = new ActionForward();
		forward.setRedirect(false);
		forward.setPath("/BoardListAction.do?page=" + page);

		return forward;
	}

}

21. In the DAO class, add the delete method.

// Delete method
	public int delete(int board_num) {
		int result = 0;
		Connection con = null;
		PreparedStatement pstmt = null;
		
		try {
			con = getConnection();					
			
			String sql="delete from model2 where board_num=?";
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(pstmt != null) try { pstmt.close();} catch(Exception e) {}
			if(con != null) try { con.close();} catch(Exception e) {}
		}
		
		return result;
	}

So far, we have made an MVC pattern bulletin board with a reply and file upload function. 

To remind you, this figure and my last post compare model1 and 2.

2022.09.15 - [JSP] - JSP) Model1 vs Model2

+ Recent posts