1. 게시글 목록조회 화면구현
- controller.board.BoardList.java
package controller.board;
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;
import service.BoardService;
@WebServlet("/board/list")
public class BoardList extends HttpServlet {
private BoardService boardService = BoardService.getInstance();
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("boards", boardService.list());
req.getRequestDispatcher("/WEB-INF/jsp/board/list.jsp").forward(req, resp);
}
}
- service.BoardService.java
// 글 목록
public List<Board> list() {
return boardDao.list();
}
- dao.BoardDao.java
public List<Board> list() {
List<Board> list = new ArrayList<Board>();
try {
Connection conn = DBConn.getConnection();
// 문장 생성
String sql = "SELECT BNO, TITLE, HITCOUNT,\r\n" +
" CASE\r\n" +
" WHEN SYSDATE - REGDATE > 1 THEN TO_CHAR(REGDATE, 'YY/MM/DD')\r\n" +
" ELSE TO_CHAR(REGDATE, 'HH24:MI:SS')\r\n" +
" END REGDATE,\r\n" +
" WRITER\r\n" +
"FROM TBL_BOARD ORDER BY 1 DESC";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 결과집합 생성
ResultSet rs = pstmt.executeQuery();
// 결과집합 순회 후 데이터 바인딩
while(rs.next()) {
Board board = new Board(rs.getLong(1), rs.getString(2), null,
rs.getInt(3), rs.getString(4), rs.getString(5));
list.add(board);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
- list.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" width="800" cellpadding="10" style="margin: 0 auto;">
<tr>
<th>글번호</th>
<th>제목</th>
<th>작성자</th>
<th>조회수</th>
<th>작성일</th>
</tr>
<c:forEach items="${boards}" var="board">
<tr>
<td>${board.bno}</td>
<td><a href="get?bno=${board.bno} ">${board.title}</a></td>
<td>${board.writer}</td>
<td>${board.hitCount}</td>
<td>${board.regDate}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
2. 게시글 상세조회 화면구현
- controller.board.Get.java
package controller.board;
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;
import service.BoardService;
@WebServlet("/board/get")
public class Get extends HttpServlet {
private BoardService boardService = BoardService.getInstance();
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Long bno = Long.parseLong(req.getParameter("bno"));
req.setAttribute("board", boardService.get(bno));
req.getRequestDispatcher("/WEB-INF/jsp/board/get.jsp").forward(req, resp);
}
}
- service.BoardService.java
// 글 상세
public Board get(Long bno) {
return boardDao.get(bno);
}
- dao.BoardDao.java
public Board get(Long bno) {
Board board = null;
try {
Connection conn = DBConn.getConnection();
String sql = "UPDATE TBL_BOARD SET\r\n" +
"HITCOUNT = HITCOUNT + 1\r\n" +
"WHERE BNO = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
// 파라미터 바인딩
pstmt.setLong(1, bno);
// 문장 실행(반영)
pstmt.executeUpdate();
// 문장 생성
sql = "SELECT BNO, TITLE, CONTENT, HITCOUNT,\r\n" +
" CASE\r\n" +
" WHEN SYSDATE - REGDATE > 1 THEN TO_CHAR(REGDATE, 'YY/MM/DD')\r\n" +
" ELSE TO_CHAR(REGDATE, 'HH24:MI:SS')\r\n" +
" END REGDATE,\r\n" +
" WRITER\r\n" +
"FROM TBL_BOARD\r\n"
+ "WHERE BNO = ?\r\n"
+ "ORDER BY 1 DESC";
pstmt = conn.prepareStatement(sql);
pstmt.setLong(1, bno);
// 결과집합 생성
ResultSet rs = pstmt.executeQuery();
// 결과집합 순회 후 데이터 바인딩
while(rs.next()) {
int idx = 1;
board = new Board(rs.getLong(idx++), rs.getString(idx++), rs.getString(idx++),
rs.getInt(idx++), rs.getString(idx++), rs.getString(idx++));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return board;
}
- get.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>${board.bno}</h1>
<h1>${board.title}</h1>
<h1>${board.content}</h1>
</body>
</html>