■ 글 작성 (bbswrite.jsp)
지난 JSP 로그인, 게시판 만들기 (1) 에서 글쓰기 View까지 만들었다.
사실 나도 복습 겸 포스팅 하는거지만, 미래의 나에게 헷깔리지 말라고 진도 표시를 좀 해둬야겠다 ㅎㅎ
이번 포스팅에선 write 부분을 하려고 한다. 차근차근,, 포스팅 하면서 하나하나 새겨봐야지
■ bbswrite.jsp
bbswrite.jsp
<%@page import="dto.MemberDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>bbswrite.jsp</title>
</head>
<body>
<%
MemberDto mem = (MemberDto)session.getAttribute("login");
if(mem == null){
response.sendRedirect("login.jsp");
%>
<script type="text/javascript">
location.href = "";
</script>
<%
}
%>
<a href="logout.jsp">로그 아웃</a>
<h1>글쓰기</h1>
<div align="center">
<form action="bbswriteAf.jsp" method="post">
<table border="1">
<col width="200"><col width="400">
<tr>
<th>아이디</th>
<td>
<%-- <input type="text" name="id" size="50px" value="<%=mem.getId() %>"> --%>
<input type="text" name="id" size="50px" value=${login.id } readonly="readonly">
</td>
</tr>
<tr>
<th>제목</th>
<td>
<input type="text" name="title" size="50px">
</td>
</tr>
<tr>
<th>내용</th>
<td>
<textarea rows="20" cols="50px" name="content"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="글쓰기">
</td>
</tr>
</table>
</form>
</div>
<a href="bbslist.jsp">글목록</a>
</body>
</html>
우선, View 부분을 다시 재정비해주었다.
1. readonly 어트리뷰트는 textfield를 더 이상 수정하지 않고 고정해주는 역할이다.
2. value에 "" 대신 ${login.id }로 값을 넣어줄 수도 있다.
■ bbswriteAf.jsp
로그인 이후의 처리를 담은 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="dto.BbsDto"%>
<%@page import="dao.BbsDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("utf-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String id = request.getParameter("id");
String title = request.getParameter("title");
String content = request.getParameter("content");
System.out.println("id:" + id);
System.out.println("title:" + title);
System.out.println("content:" + content);
BbsDao dao = BbsDao.getInstance();
boolean isS = dao.writeBbs(new BbsDto(id, title, content));
if(isS){
%>
<script type="text/javascript">
alert("글쓰기 성공!");
location.href = "bbslist.jsp";
</script>
<%
}else{
%>
<script type="text/javascript">
alert("글 입력을 다시 하십시오");
location.href = "bbswrite.jsp";
</script>
<%
}
%>
</body>
</html>
|
cs |
■ bbsDao.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
public class BbsDao {
private static BbsDao dao = new BbsDao();
private BbsDao() {
}
public static BbsDao getInstance() {
return dao;
}
public List<BbsDto> getBbsList() {
String sql = " SELECT SEQ, ID, REF, STEP, DEPTH, "
+ " TITLE, CONTENT, WDATE, "
+ " DEL, READCOUNT "
+ " FROM BBS "
+ " ORDER BY REF DESC, STEP ASC ";
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
List<BbsDto> list = new ArrayList<BbsDto>();
try {
conn = DBConnection.getConnection();
System.out.println("1/6 getBbsList success");
psmt = conn.prepareStatement(sql);
System.out.println("2/6 getBbsList success");
rs = psmt.executeQuery();
System.out.println("3/6 getBbsList success");
while(rs.next()) {
int i = 1;
BbsDto dto = new BbsDto(rs.getInt(i++),
rs.getString(i++),
rs.getInt(i++),
rs.getInt(i++),
rs.getInt(i++),
rs.getString(i++),
rs.getString(i++),
rs.getString(i++),
rs.getInt(i++),
rs.getInt(i++));
list.add(dto);
}
System.out.println("4/6 getBbsList success");
} catch (Exception e) {
e.printStackTrace();
} finally {
DBClose.close(psmt, conn, rs);
}
return list;
}
public boolean writeBbs(BbsDto dto) {
String sql = " INSERT INTO BBS "
+ " (SEQ, ID, REF, STEP, DEPTH, "
+ " TITLE, CONTENT, WDATE, "
+ " DEL, READCOUNT) "
+ " VALUES( SEQ_BBS.NEXTVAL, ?, "
+ " (SELECT NVL(MAX(REF), 0)+1 FROM BBS), 0, 0, "
+ " ?, ?, SYSDATE, "
+ " 0, 0) ";
Connection conn = null;
PreparedStatement psmt = null;
int count = 0;
try {
conn = DBConnection.getConnection();
System.out.println("1/6 writeBbs success");
psmt = conn.prepareStatement(sql);
psmt.setString(1, dto.getId());
psmt.setString(2, dto.getTitle());
psmt.setString(3, dto.getContent());
System.out.println("2/6 writeBbs success");
count = psmt.executeUpdate();
System.out.println("3/6 writeBbs success");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBClose.close(psmt, conn, null);
}
return count>0?true:false;
}
public BbsDto getBbs(int seq) {
String sql = " SELECT SEQ, ID, REF, STEP, DEPTH, "
+ " TITLE, CONTENT, WDATE,"
+ " DEL, READCOUNT "
+ " FROM BBS "
+ " WHERE SEQ=? ";
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
BbsDto dto = null;
try {
conn = DBConnection.getConnection();
System.out.println("1/6 getBbs success");
psmt = conn.prepareStatement(sql);
System.out.println("2/6 getBbs success");
psmt.setInt(1, seq);
rs = psmt.executeQuery();
System.out.println("3/6 getBbs success");
if(rs.next()) {
int i = 1;
dto = new BbsDto(rs.getInt(i++),
rs.getString(i++),
rs.getInt(i++),
rs.getInt(i++),
rs.getInt(i++),
rs.getString(i++),
rs.getString(i++),
rs.getString(i++),
rs.getInt(i++),
rs.getInt(i++));
}
System.out.println("4/6 getBbs success");
} catch (Exception e) {
System.out.println("getBbs fail");
e.printStackTrace();
} finally {
DBClose.close(psmt, conn, rs);
}
return dto;
}
|
cs |
'Side Project' 카테고리의 다른 글
#06. [Side Project 1] JSP 로그인, 게시판 만들기 (6) :: 글 삭제 (0) | 2020.07.30 |
---|---|
#05. [Side Project 1] JSP 로그인, 게시판 만들기 (5) :: 글 내용 Detail 자세히 보기 (0) | 2020.07.30 |
#04. [Side Project 1] JSP 로그인, 게시판 만들기 (4) :: 답글, 댓글, (0) | 2020.07.30 |
#03. [Side Project 1] JSP 로그인, 게시판 만들기 (3) :: 글 목록, List, 조회수 (0) | 2020.07.30 |
#01. [Side Project 1] JSP 로그인, 게시판 만들기 (1) 로그인, 회원가입, ID체크 Model + View (0) | 2020.07.28 |