1. 게시판 조회/작성/삭제/답변을 구현하시오.
※게시판 전체 조회
<List 출력 화면>
<BFrontController.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
65
|
@WebServlet("*.do") //do로 끝나면 FrontController 유입
public class BFrontController extends HttpServlet {
private static final long serialVersionUID = 1L;
public BFrontController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doGet");
actionDo(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doPost");
actionDo(request, response);
}
private void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("EUC-KR");
String viewPage = null;
BCommand command = null;
String uri = request.getRequestURI();
String conPath = request.getContextPath();
String com = uri.substring(conPath.length()); //uri를 com단위로 잘라주는 코드
System.out.println(uri);
System.out.println(conPath);
System.out.println(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 BContentViewCommand();
command.execute(request, response);
viewPage = "content_view.jsp";
}else if(com.equals("/write_view.do")) {
viewPage = "write_view.jsp";
}else if(com.equals("/write.do")) {
command = new BWriteCommand();
command.execute(request, response);
viewPage = "list.do";
}else if(com.equals("/delete.do")) {
command = new BDeleteCommand();
command.execute(request, response);
viewPage = "list.do";
}else if(com.equals("/reply_view.do")) {
command = new BReplyViewCommand();
command.execute(request, response);
viewPage = "reply_view.jsp";
}else if(com.equals("/reply.do")) {
command = new BReplyCommand();
command.execute(request, response);
viewPage = "list.do";
}
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage);
dispatcher.forward(request, response);
//(request.setAttribute("list", dtos))를 가지고 있음
}
}
|
cs |
<BCommand.java>
1
2
3
|
public interface BCommand {
abstract void execute(HttpServletRequest request, HttpServletResponse response);
}
|
cs |
<BListCommand.java>
1
2
3
4
5
6
7
8
9
|
public class BListCommand implements BCommand{
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
BDao dao = new BDao();
ArrayList<BDto> dtos = dao.list();
request.setAttribute("list", dtos);
}
}
|
cs |
<BDao.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
|
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 ArrayList<BDto> list(){
ArrayList<BDto> dtos = new ArrayList<BDto>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
String query = "select * from mvc_board order by bgroup desc, bstep asc";
ps = con.prepareStatement(query);
rs = ps.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");
BDto dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
dtos.add(dto);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(rs != null) rs.close();
if(ps != null) ps.close();
if(con != null) con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return dtos;
}
|
cs |
<BDto.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
public class BDto {
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 BDto() {
}
public BDto(int bId, String bName, String bTitle, String bContent, Timestamp bDate, int bHit, int bGroup, int bStep, int bIndent) {
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;
}
}
|
cs |
<list.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
|
<body>
<table cellpadding="0" cellspacing="0" border="1">
<tr>
<td>번호</td>
<td>이름</td>
<td>제목</td>
<td>날짜</td>
<td>히트</td>
</tr>
<c:forEach items="${list}" var="dto">
<tr>
<td>${dto.bId}</td>
<td>${dto.bName}</td>
<td>
<c:forEach begin="1" end="${dto.bIndent}">-[re]</c:forEach>
<a href="content_view.do?bId=${dto.bId}">${dto.bTitle}</a></td>
<td>${dto.bDae}</td>
<td>${dto.bHit}</td>
</tr>
</c:forEach>
<tr>
<td colspan="5"> <a href="write_view.do">글작성</a> </td>
</tr>
</table>
</body>
|
cs |
※게시판 글 1개 조회
<Content_view 출력 화면>
<BContentViewCommand.java>
1
2
3
4
5
6
7
8
9
10
|
public class BContentViewCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
String bId = request.getParameter("bId");
BDao dao = new BDao();
BDto dto = dao.contentview(bId);
request.setAttribute("content_view", dto);
}
}
|
cs |
<BDao .java> contentview 부분
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
|
public BDto contentview(String bId) {
upHit(bId);
BDto dtos = null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
String query = "select * from mvc_board where bId = ?";
ps = con.prepareStatement(query);
ps.setInt(1, Integer.parseInt(bId));
rs = ps.executeQuery();
while(rs.next()) {
int id = 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");
dtos = new BDto(id, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
}
}catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(rs != null) rs.close();
if(ps != null) ps.close();
if(con != null) con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
} return dtos;
}
private void upHit(String bId) {
Connection con = null;
PreparedStatement ps = null;
try {
con = dataSource.getConnection();
String query = "update mvc_board set bHit=(bHit + 1) where bId=?";
ps = con.prepareStatement(query);
ps.setInt(1, Integer.parseInt(bId));
int rn = ps.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(ps != null) ps.close();
if(con != null) con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
|
cs |
<content_view.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
|
<body>
<form action="modify.do" method="post">
<table width="500" cellpadding="0" cellspacing="0" border="1">
<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>
</table>
</form>
</body>
|
cs |
※게시판 글 작성
<Wirte 출력 화면>
<BWriteCommand.java>
1
2
3
4
5
6
7
8
9
10
11
|
public class BWriteCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
String bName = request.getParameter("bName");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
BDao dao = new BDao();
dao.write(bName, bTitle, bContent);
}
}
|
cs |
<BDao .java> write 부분
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
|
public void write(String bName, String bTitle, String bContent) {
Connection con = null;
PreparedStatement ps = null;
try {
con = dataSource.getConnection();
String query = "insert into mvc_board(bId, bName, bTitle, bContent)"
+ "values(mvc_board_seq.nextval, ?, ?, ?)";
ps = con.prepareStatement(query);
ps.setString(1, bName);
ps.setString(2, bTitle);
ps.setString(3, bContent);
int rn = ps.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}finally{
try {
if(ps != null) ps.close();
if(con != null) con.close();
}catch(Exception e2) {
e2.printStackTrace();
}
}
}
|
cs |
<write_view.jsp>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<body>
<form action="write.do" method="post">
<table width="500" cellpadding="0" cellspacing="0" border="1">
<tr>
<td>이름</td>
<td><input type="text" name="bName"></td>
</tr>
<tr>
<td>제목</td>
<td><input type="text" name="bTitle"></td>
</tr>
<tr>
<td>내용</td>
<td><textarea rows="8" name="bContent"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="입력"></td>
<td><a href="list.do">목록</a>
</tr>
</table>
</form>
</body>
|
cs |
※게시판 글 삭제
<Delete 출력 화면>
<BDeleteCommand.java>
1
2
3
4
5
6
7
8
|
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);
}
}
|
cs |
<BDao.java> delete 부분
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
|
public void delete(String bId) {
Connection con = null;
PreparedStatement ps = null;
try {
con = dataSource.getConnection();
String query = "delete from mvc_board where bId = ?";
ps = con.prepareStatement(query);
ps.setInt(1, Integer.parseInt(bId));
System.out.println("1");
int rn = ps.executeUpdate();
System.out.println("delete 성공");
}catch(Exception e) {
e.printStackTrace();
}finally{
try {
if(ps != null) ps.close();
if(con != null) con.close();
}catch(Exception e2) {
e2.printStackTrace();
}
}
}
|
cs |
※게시판 답변
<Reply 출력 화면>
<BReplyViewCommand.java>
1
2
3
4
5
6
7
8
9
10
|
public class BReplyViewCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
String bId = request.getParameter("bId");
BDao dao = new BDao();
BDto dto = dao.replyView(bId);
request.setAttribute("reply_view", dto);
}
}
|
cs |
<BReplyCommand.java>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class BReplyCommand implements BCommand {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
String bId = request.getParameter("bId");
String bName = request.getParameter("bName");
String bTitle = request.getParameter("bTitle");
String bContent = request.getParameter("bContent");
String bGroup = request.getParameter("bGroup");
String bStep = request.getParameter("bStep");
String bIndent = request.getParameter("bIndent");
BDao dao = new BDao();
dao.reply(bId, bName, bTitle, bContent, bGroup, bStep, bIndent);
}
}
|
cs |
<BDao .java> replyView, reply부분
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
public BDto replyView(String bId) {
BDto dto = null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
String query = "select * from mvc_board where bId=?";
ps = con.prepareStatement(query);
ps.setInt(1, Integer.parseInt(bId));
rs = ps.executeQuery();
while(rs.next()) {
int id = 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");
dto = new BDto(id, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
}
}catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(rs != null) rs.close();
if(ps != null) ps.close();
if(con != null) con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
} return dto;
}
public void reply(String bId, String bName, String bTitle, String bContent, String bGroup, String bStep,
String bIndent) {
replyShape(bGroup, bStep);
Connection con = null;
PreparedStatement ps = null;
try {
con = dataSource.getConnection();
String query = "insert into mvc_board(bId, bName, bTitle, bContent, bGroup, bStep, bIndent)"
+ "values(mvc_board_seq.nextval, ?, ?, ?, ?, ?, ?)";
ps = con.prepareStatement(query);
ps.setString(1, bName);
ps.setString(2, bTitle);
ps.setString(3, bContent);
ps.setInt(4, Integer.parseInt(bGroup));
ps.setInt(5, Integer.parseInt((bStep) + 1));
ps.setInt(6, Integer.parseInt((bIndent) + 1));
int rn = ps.executeUpdate();
System.out.println("완료" + rn);
}catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(ps != null) ps.close();
if(con != null) con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
private void replyShape(String strGroup, String strStep) {
Connection con = null;
PreparedStatement ps = null;
try {
con = dataSource.getConnection();
String query = "update mvc_board set bStep = bStep+1 where bGroup = ? and bStep > ?";
ps = con.prepareStatement(query);
ps.setInt(1, Integer.parseInt(strGroup));
ps.setInt(2, Integer.parseInt(strStep));
int rn = ps.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(ps != null) ps.close();
if(con != null) con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
|
cs |
<reply_view.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
|
<body>
<form action="reply.do" method="post">
<table width="500" cellpadding="0" cellspacing="0" border="1">
<input type="hidden" name="bId" value="${reply_view.bId}">
<input type="hidden" name="bGroup" value="${reply_view.bGroup}">
<input type="hidden" name="bStep" value="${reply_view.bStep}">
<input type="hidden" name="bIndent" value="${reply_view.bIndent}">
<tr>
<td>번호</td>
<td>${reply_view.bId}</td>
</tr>
<tr>
<td>히트</td>
<td>${reply_view.bHit}</td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="bName" value="${reply_view.bName}"></td>
</tr>
<tr>
<td>제목</td>
<td><input type="text" name="bTitle"
value="${reply_view.bTitle}"></td>
</tr>
<tr>
<td>내용</td>
<td><textarea rows="8" name="bContent">${reply_view.bContent}"</textarea></td>
</tr>
<tr>
<td><input type="submit" value="답변"></td>
<td><a href="list.do">목록</a>
</tr>
</table>
</form>
</body>
|
cs |
'bitcamp > JSP' 카테고리의 다른 글
JSP_emp_연습문제 (0) | 2021.02.12 |
---|---|
JSP_MVC패턴 (0) | 2021.02.12 |
SQL문 (0) | 2021.02.12 |
JSP_게시판 작성을 위한 DB 맛보기! (0) | 2021.02.11 |
JSP_bean_연습문제 (0) | 2021.02.11 |