📝 DB를 처리하는 VO, DAO 파일
✔ BoardVO.java
package edu.global.board.vo; import java.sql.Timestamp; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; public class BoardVO { private int bid; private String bname; private String btitle; private String bcontent; private Timestamp bdate; private int bhit; private int bgroup; private int bstep; private int bindent; public BoardVO() { } public BoardVO(int bid, String bname, String btitle, String bcontent, Timestamp bdate, int bhit, int bgroup, int bstep, int bindent) { super(); this.bid = bid; this.bname = bname; this.btitle = btitle; this.bcontent = bcontent; this.bdate = bdate; this.bhit = bhit; this.bgroup = bgroup; this.bstep = bstep; this.bindent = bindent; } public int getBid() { return bid; } public void setBid(int bid) { this.bid = bid; } public String getBname() { return bname; } public void setBname(String bname) { this.bname = bname; } public String getBtitle() { return btitle; } public void setBtitle(String btitle) { this.btitle = btitle; } public String getBcontent() { return bcontent; } public void setBcontent(String bcontent) { this.bcontent = bcontent; } public Timestamp getBdate() { return bdate; } public void setBdate(Timestamp bdate) { this.bdate = bdate; } public int getBhit() { return bhit; } public void setBhit(int bhit) { this.bhit = bhit; } public int getBgroup() { return bgroup; } public void setBgroup(int bgroup) { this.bgroup = bgroup; } public int getBstep() { return bstep; } public void setBstep(int bstep) { this.bstep = bstep; } public int getBindent() { return bindent; } public void setBindent(int bindent) { this.bindent = bindent; } }
✔ BDao.java
package edu.global.board.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import edu.global.board.vo.BoardVO; public class BDao { private DataSource dataSource; public BDao() { try { Context context = new InitialContext(); dataSource = (DataSource) context.lookup("java:comp/env/jdbc/oracle"); } catch (Exception e) { e.printStackTrace(); } } public List<BoardVO> list() { List<BoardVO> boards = new ArrayList<BoardVO>(); Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { con = dataSource.getConnection(); String query = "Select * from mvc_board order by bgroup desc, bstep asc"; pstmt = con.prepareStatement(query); rs = pstmt.executeQuery(); while (rs.next()) { int bid = rs.getInt("bid"); String bname = rs.getString("bname"); String btitle = rs.getString("btitle"); String bcontent = rs.getString("bcontent"); Timestamp bdate = rs.getTimestamp("bdate"); int bhit = rs.getInt("bhit"); int bgroup = rs.getInt("bgroup"); int bstep = rs.getInt("bstep"); int bindent = rs.getInt("bindent"); BoardVO vo = new BoardVO(bid, bname, btitle, bcontent, bdate, bhit, bgroup, bstep, bindent); boards.add(vo); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (con != null) con.close(); } catch (Exception e2) { e2.printStackTrace(); } } return boards; } public BoardVO contentView(String strID) { BoardVO board = null; Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { String query = "Select * from mvc_board where bid = ?"; con = dataSource.getConnection(); pstmt = con.prepareStatement(query); pstmt.setInt(1, Integer.valueOf(strID)); // 쿼리문 ? 에 넣을 값 설정 - (인덱스, 내용) rs = pstmt.executeQuery(); while (rs.next()) { int bid = rs.getInt("bid"); String bname = rs.getString("bname"); String btitle = rs.getString("btitle"); String bcontent = rs.getString("bcontent"); Timestamp bdate = rs.getTimestamp("bdate"); int bhit = rs.getInt("bhit"); int bgroup = rs.getInt("bgroup"); int bstep = rs.getInt("bstep"); int bindent = rs.getInt("bindent"); board = new BoardVO(bid, bname, btitle, bcontent, bdate, bhit, bgroup, bstep, bindent); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (con != null) con.close(); } catch (Exception e2) { e2.printStackTrace(); } } return board; } public int modify(String bid, String bname, String btitle, String bcontent) { Connection con = null; PreparedStatement pstmt = null; int rn = 0; try { String query = "update mvc_board set bname = ?, btitle = ?, bcontent = ? where bid = ?"; con = dataSource.getConnection(); pstmt = con.prepareStatement(query); pstmt.setString(1, bname); // 쿼리문 ? 에 넣을 값 설정 - (인덱스, 내용) pstmt.setString(2, btitle); pstmt.setString(3, bcontent); pstmt.setInt(4, Integer.valueOf(bid)); rn = pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (con != null) con.close(); } catch (Exception e2) { e2.printStackTrace(); } } return rn; } public int delete(String strID) { Connection con = null; PreparedStatement pstmt = null; int rs = 0; try { String query = "delete from mvc_board where bid = ?"; con = dataSource.getConnection(); pstmt = con.prepareStatement(query); pstmt.setInt(1, Integer.valueOf(strID)); rs = pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (pstmt != null) pstmt.close(); if (con != null) con.close(); } catch (Exception e2) { e2.printStackTrace(); } } return rs; } }
📝 각 기능을 가진 Command 파일
✔ BCommand.java
package edu.global.board.command; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class BoardController */ public interface BCommand { public abstract void execute(HttpServletRequest request, HttpServletResponse response); }
✔ BListCommand.java
package edu.global.board.command; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import edu.global.board.dao.BDao; import edu.global.board.vo.BoardVO; public class BListCommand implements BCommand{ @Override public void execute(HttpServletRequest request, HttpServletResponse response) { BDao dao= new BDao(); List<BoardVO> vos = dao.list(); request.setAttribute("boards", vos); } }
✔ BContentCommand.java
package edu.global.board.command; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import edu.global.board.dao.BDao; import edu.global.board.vo.BoardVO; public class BContentCommand implements BCommand { @Override public void execute(HttpServletRequest request, HttpServletResponse response) { String bid = request.getParameter("bid"); BDao dao = new BDao(); BoardVO vo = dao.contentView(bid); request.setAttribute("content_view", vo); } }
✔ BModifyCommand.java
package edu.global.board.command; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import edu.global.board.dao.BDao; import edu.global.board.vo.BoardVO; public class BModifyCommand implements BCommand { @Override public void execute(HttpServletRequest request, HttpServletResponse response) { String bid = request.getParameter("bid"); String bname = request.getParameter("bname"); String bcontent = request.getParameter("bcontent"); String btitle = request.getParameter("btitle"); BDao dao = new BDao(); dao.modify(bid, bname, bcontent, btitle); } }
✔ BDeleteCommand.java
package edu.global.board.command; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import edu.global.board.dao.BDao; import edu.global.board.vo.BoardVO; public class BDeleteCommand implements BCommand { @Override public void execute(HttpServletRequest request, HttpServletResponse response) { String bid = request.getParameter("bid"); BDao dao = new BDao(); dao.delete(bid); } }
✔ list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <table width="500" cellpadding="0" cellspacing="0" border="1"> <tr> <td>번호</td> <td>이름</td> <td>제목</td> <td>날짜</td> <td>히트</td> </tr> <c:forEach var="board" items="${boards}"> <tr> <td>${board.bid}</td> <td>${board.bname}</td> <td><c:forEach begin="1" end="${board.bindent}">-</c:forEach> <a href="content_view.do?bid=${board.bid}">${board.btitle}</a></td> <td>${board.bdate}</td> <td>${board.bhit}</td> </tr> </c:forEach> <tr> <td colspan="5"><a href="write_view.do">글작성</a></td> </tr> </table> </body> </html>
✔ content_view.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <table width="500" cellpadding="0" cellspacing="0" border="1"> <form action="modify.do" method="post"> <input type="hidden" name="bid" value="${content_view.bid}"> <tr> <td> 번호 </td> <td> ${content_view.bid} </td> </tr> <tr> <td> 히트 </td> <td> ${content_view.bhit} </td> </tr> <tr> <td> 이름 </td> <td> <input type="text" name="bname" value="${content_view.bname}"></td> </tr> <tr> <td> 제목 </td> <td> <input type="text" name="btitle" value="${content_view.btitle}"></td> </tr> <tr> <td> 내용 </td> <td> <textarea rows="10" name="bcontent" >${content_view.bcontent}</textarea></td> </tr> <tr > <td colspan="2"> <input type="submit" value="수정"> <a href="list.do">목록보기</a> <a href="delete.do?bid=${content_view.bid}">삭제</a> <a href="reply_view.do?bid=${content_view.bid}">답변</a></td> </tr> </form> </table> </body> </html>
- 결과
✔ BoardController.java
package edu.global.board.controller; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import edu.global.board.command.BCommand; import edu.global.board.command.BContentCommand; import edu.global.board.command.BDeleteCommand; import edu.global.board.command.BListCommand; import edu.global.board.command.BModifyCommand; /** * Servlet implementation class BoardController */ @WebServlet("*.do") public class BoardController extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public BoardController() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { actionDo(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { actionDo(request, response); } private void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("actionDo() .."); request.setCharacterEncoding("UTF-8"); String viewPage = null; BCommand command = null; String uri = request.getRequestURI(); // 'servlet_mj_board/list.do' String contextPath = request.getContextPath(); // contextPath: '/servlet_mj_board' 톰캣이 관리하는 서버명 String com = uri.substring(contextPath.length()); // com: '/list.do' System.out.println("uri:" + uri); System.out.println("contextPath:" + contextPath); System.out.println("com:" + com); if (com.equals("/list.do")) { command = new BListCommand(); command.execute(request, response); viewPage = "list.jsp"; } else if (com.equals("/content_view.do")) { command = new BContentCommand(); command.execute(request, response); viewPage = "content_view.jsp"; } else if (com.equals("/modify.do")) { command = new BModifyCommand(); command.execute(request, response); viewPage = "list.do"; } else if (com.equals("/delete.do")) { System.out.println("/delete.do .."); command = new BDeleteCommand(); command.execute(request, response); viewPage = "list.do"; } RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage); // viewPage에 request, response를 포워딩시키는 코드 dispatcher.forward(request, response); } }