본문 바로가기
JSP/JSP & Servlet

MVC패턴 게시판 만들기 - 글보기

by avvin 2019. 5. 24.

MVC패턴 게시판 만들기 - 글보기



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
package control;
 
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 model.BoardBean;
import model.BoardDAO;
 
@WebServlet("/BoardInfoControl.do")
public class BoardInfoControl extends HttpServlet {
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        reqPro(request, response);
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        reqPro(request, response);
    }
    
    protected void reqPro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
        //get방식으로 글 Num받아옴 //trim()으로 공백 제거 후 정수형으로 
        int num = Integer.parseInt(request.getParameter("num").trim());
 
        //데이터베이스 접근
        BoardDAO bdao = new BoardDAO();
        //bean에  하나의 게시글 리턴 패키징
        BoardBean bean = bdao.getOneBoard(num);
        
        request.setAttribute("bean", bean);
        RequestDispatcher rd = request.getRequestDispatcher("BoardInfo.jsp");
        rd.forward(request, response);
        
    }
}
 
cs





BoardDAO.java(Moddel) 중 일부 

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
    //하나의 게시글을 리턴하는 메서드, 실행될때마다 조회수도 올라가게 한다.
    public BoardBean getOneBoard(int num) {
        
        BoardBean bean= new BoardBean();
        getCon();
        try {
            //조회수 올리기
            String readsql = "update board set readcount=readcount+1 where num=?";
            pstmt = con.prepareStatement(readsql);
            pstmt.setInt(1, num);
            pstmt.executeUpdate();
            //게시글 불러오기 //조회수 업데이트해준 뒤에 불러와야함
            String sql="select * from board where num=?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, num);
            rs = pstmt.executeQuery();
            if(rs.next()) {
                bean.setNum(rs.getInt(1));
                bean.setWriter(rs.getString(2));
                bean.setEmail(rs.getString(3));
                bean.setSubject(rs.getString(4));
                bean.setPassword(rs.getString(5));
                // ResultSet은 getDate()메서드도 제공한다
                // byte[]타입으로 리턴되므로 toSting()처리해준다.
                bean.setReg_date(rs.getDate(6).toString());
                bean.setRef(rs.getInt(7));
                bean.setRe_step(rs.getInt(8));
                bean.setRe_level(rs.getInt(9));
                bean.setReadcount(rs.getInt(10));
                bean.setContent(rs.getString(11));
            }
            
        }catch(Exception e) {
            e.printStackTrace();
        }
        
        return bean;
    }
cs





BoardInfo.jsp

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
51
<%@page import="model.BoardBean"%>
<%@page import="model.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
 
    <div style="text-align: center">
        <h2>게시글 보기</h2>
        <table  align="center" width="600" border="1" >
            <tr height="40">
                <td align="center" width="120">글번호</td>
                <td align="left" width="180">${bean.num }
                <td align="center" width="120">조회수</td>
                <td align="left" width="180">${bean.readcount }</td>
            </tr>
            <tr height="40">
                <td align="center" width="120">작성자</td>
                <td align="left" width="180">${bean.writer }
                <td align="center" width="120">작성일</td>
                <td align="left" width="180">${bean.reg_date}</td>
            </tr>
            <tr height="40">
                <td align="center" width="120">이메일</td>
                <td align="left" colspan ="3">${bean.email }</td>
            </tr>
            <tr height="40">
                <td align="center" width="120">제목</td>
                <td align="left" colspan ="3">${bean.subject }</td>
            </tr>
            <tr height="80">
                <td align="center" width="120">글내용</td>
                <td align="left" colspan ="3">${bean.content }</td>
            </tr>
            <tr height="40">
                <td align="center" colspan="4">
                <input type="button" value ="답글쓰기" 
                onclick="location.href='BoardReWriteCon.do?num=${bean.num }&ref=${bean.ref }&re_step=${bean.re_step }&re_level=${bean.re_level}'">
                <input type="button" value ="수정하기" 
                onclick="location.href='BoardUpdateCon.do?num=${bean.num }'">    
                <input type="button" value ="삭제하기" 
                onclick="location.href='BoardDeleteCon.do?num=${bean.num }'">
                <input type="button" value ="목록보기" 
                onclick="location.href='BoardListCon.do'">                                            
            </tr>            
            
        </table>
    </div>
</body>
</html>
cs