2019.02.18 Model2 : 게시판 만들기(1)

2019. 2. 18. 16:37JSP

#1 사용될 변수를 생성 

-> DTO: 데이터 전송 객체(Data transfer object) 


***bbsDto.java


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
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.bbs.dto;
 
public class bbsDto {
    
    private String bbsId;
    private String bbsCategory;
    private String bbsTitle;
    private String bbsContent;
    private String bbsDate;
    private String bbsHit;
    private String id;
    
    
    public String getBbsId() {
        return bbsId;
    }
    public void setBbsId(String bbsId) {
        this.bbsId = bbsId;
    }
    public String getBbsCategory() {
        return bbsCategory;
    }
    public void setBbsCategory(String bbsCategory) {
        this.bbsCategory = bbsCategory;
    }
    public String getBbsTitle() {
        return bbsTitle;
    }
    public void setBbsTitle(String bbsTitle) {
        this.bbsTitle = bbsTitle;
    }
    public String getBbsContent() {
        return bbsContent;
    }
    public void setBbsContent(String bbsContent) {
        this.bbsContent = bbsContent;
    }
    public String getBbsDate() {
        return bbsDate;
    }
    public void setBbsDate(String bbsDate) {
        this.bbsDate = bbsDate;
    }
    public String getBbsHit() {
        return bbsHit;
    }
    public void setBbsHit(String bbsHit) {
        this.bbsHit = bbsHit;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    
    @Override
    public String toString() {
        return "bbsDto [bbsId=" + bbsId + ", bbsCategory=" + bbsCategory + ", bbsTitle=" + bbsTitle + ", bbsContent="
                + bbsContent + ", bbsDate=" + bbsDate + ", bbsHit=" + bbsHit + ", id=" + id + "]";
    }
    
}
 
cs



#2 사용자의 화면에 보이게 될 jsp페이지 생성(WebContent/WEB-INF폴더안에서 생성)

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
52
53
54
55
56
57
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판</title>
</head>
<body style="background-color: #999999;">
    <div>
        <div align="center">게시판</div>
        <div align="center">
            <form method="post" action="bbsUpload.do">
                <table border=0px>
                    <tr>
                        <td><h6>카테고리</h6></td>
                        <td colspan="4"><select name="category">
                                <option value="math">수학</option>
                                <option value="enjoy">여행</option>
                                <option value="pic">사진</option>
                                <option value="java">자바</option>
                                <option value="web">웹프로그래밍</option>
                                <option value="estate">부동산</option>
                                <option value="food">음식</option>
                                <option value="common">상식</option>
                        </select></td>
                    <tr>
                        <td><h5>제목</h5></td>
                        <td colspan="4"><input name="title" type="text"></td>
                    </tr>
                    <tr>
                        <td><h5>내용</h5></td>
                        <td colspan="4"><textarea name="content" rows="20" cols="80"></textarea>
                    </tr>
                    <tr>
                        <td colspan="3"></td>
                        <td colspan="2">
                            <button type="submit">보내기</button>
                        </td>
                    </tr>
                </table>
            </form>
            <div>
                <button>
                    <a href="">게시판</a>
                </button>
            </div>
            <div>
                <button>
                    <a href="">처음으로</a>
                </button>
            </div>
        </div>
    </div>
 
</body>
</html>
cs



#3 WEB-INF안에 jsp페이지가 존재하기 때문에 임의의 컨트롤러로서 페이지를 호출한다.


① web.xml안에서의 설정


직접동작하게될 controller 생성 -> 생성될 컨트롤러는 HttpServlet을 상속시킨다.


***bbsUploadController.java

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
52
53
package com.bbs.controller;
 
import java.io.IOException;
 
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import com.bbs.dto.bbsDto;
 
public class bbsUploadController extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/bbsUpload.jsp");
        rd.forward(req, resp);
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String sessionID = null;
 
        if (session.getAttribute("sessionID"!= null) {
            sessionID = (String) session.getAttribute("sessionID");
        }
 
 
        // post방식으로 받아왔기 때문에 utf-8선언->한글의 깨짐을 방지
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charest=UTF-8");
 
        // 입력값 가져오기
        String category = req.getParameter("category");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
 
        // bbsDto에 값을 입력하기 -> 받아온 값을 bbsDto에 갱신
        bbsDto bDto = new bbsDto();
        bDto.setId(sessionID);
        bDto.setBbsCategory(category);
        bDto.setBbsTitle(title);
        bDto.setBbsContent(content);
 
        System.out.println(bDto.toString()); //bDto에 제대로작성도는지 확인 
 
    }
 
}
 
cs


'JSP' 카테고리의 다른 글

2019.02.19 Model2 : 게시판 만들기(3)  (0) 2019.02.19
2019.02.19 Model2 : 게시판 만들기(2)  (0) 2019.02.19
2019.02.18 JSPL functions  (0) 2019.02.18
2019.02.18 JSTL - JSTL core  (0) 2019.02.18
2019.02.18 EL(Expression Langauage)  (0) 2019.02.18