홈페이지만들기4 : 게시판글쓰기구현

홈페이지만들기4 : 게시판글쓰기구현

top.jsp수정

  • ../center/notice.html 을 ../center/notice.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
<% //화면상단에 위치하는 메뉴바 처리페이지
String id = (String)session.getAttribute("id"); //다운캐스팅 Object -> String
%>
<header>
<div id="login">
<%if(id == null){
%>
<div id="login"><a href="../member/loginForm.jsp">로그인</a> | <a href="../member/joinForm.jsp">회원가입</a></div>
<%
}else if(id != null){
%>
<div id="login"><%=id %>님 환영합니다 | <a href="../member/logout.jsp">로그아웃</a></div>
<%
}
%>
</div>
<div class="clear"></div>
<!-- 로고들어가는 곳 -->
<div id="logo"><img src="../images/logo.gif" width="265" height="62" alt="Fun Web"></div>
<!-- 로고들어가는 곳 -->
<nav id="top_menu">
<ul>
<li><a href="../index.jsp">홈</a></li>
<li><a href="../company/welcome.html">회사소개</a></li>
<li><a href="#">솔루션</a></li>
<li><a href="../center/notice.jsp">게시판</a></li>
<li><a href="#">무엇이든 물어보세요</a></li>
</ul>
</nav>
</header>




notice.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
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
<body>
<div id="wrap">
<!-- 헤더들어가는 곳 -->
<jsp:include page="../inc/top.jsp" />
<!-- 헤더들어가는 곳 -->

<!-- 본문들어가는 곳 -->
<!-- 메인이미지 -->
<div id="sub_img_center"></div>
<!-- 메인이미지 -->

<!-- 왼쪽메뉴 -->
<nav id="sub_menu">
<ul>
<li><a href="writeForm.jsp">글쓰기</a></li>
<li><a href="#">글 목록 보기</a></li>
<li><a href="#">파일업로드/다운로드</a></li>
<li><a href="#">Service Policy</a></li>
</ul>
</nav>
<!-- 왼쪽메뉴 -->

<!-- 게시판 -->
<article>
<h1>게시판</h1>
<table id="notice">
<tr><th class="tno">No.</th>
<th class="ttitle">Title</th>
<th class="twrite">Writer</th>
<th class="tdate">Date</th>
<th class="tread">Read</th></tr>
<tr><td>15</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>14</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>13</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>12</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>11</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>10</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>9</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>8</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>7</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>6</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>5</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>4</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>3</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>2</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
<tr><td>1</td><td class="left">Vivanus viveer portitor commodo.</td>
<td>Host Admin</td><td>2012.11.06</td><td>15</td></tr>
</table>
<div id="table_search">
<input type="text" name="search" class="input_box">
<input type="button" value="search" class="btn">
</div>
<div class="clear"></div>
<div id="page_control">
<a href="#">Prev</a>
<a href="#">1</a><a href="#">2</a><a href="#">3</a>
<a href="#">4</a><a href="#">5</a><a href="#">6</a>
<a href="#">7</a><a href="#">8</a><a href="#">9</a>
<a href="#">10</a>
<a href="#">Next</a>
</div>
</article>
<!-- 게시판 -->
<!-- 본문들어가는 곳 -->
<div class="clear"></div>
<!-- 푸터들어가는 곳 -->
<jsp:include page="../inc/bottom.jsp" />
<!-- 푸터들어가는 곳 -->
</div>
</body>




writeForm.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
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
<body>
<div id="wrap">
<!-- 헤더들어가는 곳 -->
<jsp:include page="../inc/top.jsp" />
<!-- 헤더들어가는 곳 -->

<!-- 본문들어가는 곳 -->
<!-- 메인이미지 -->
<div id="sub_img_center"></div>
<!-- 메인이미지 -->

<!-- 왼쪽메뉴 -->
<nav id="sub_menu">
<ul>
<li><a href="#">글쓰기</a></li>
<li><a href="notice.jsp">글 목록 보기</a></li>
<li><a href="#">파일업로드/다운로드</a></li>
<li><a href="#">Service Policy</a></li>
</ul>
</nav>
<!-- 왼쪽메뉴 -->

<!-- 게시판 -->
<article>
<h1> 게시판 글쓰기 </h1>
<!-- 로그인 한 사용자만 글쓰기 가능
로그인한 사람의 ID값을 글쓴이로 지정
-->
<%
// 세션ID값을 사용해서 글쓴이 지정
// 로그인 x -> 로그인 페이지로 이동
String id = (String) session.getAttribute("id");
if(id == null){
response.sendRedirect("../member/loginForm.jsp");
}
%>

<form action="writePro.jsp" method="post" name="fr">
<table id="notice">
<tr>
<td>글쓴이</td>
<td>
<input type="text" name="name" value="<%=id%>" readonly>
</td>
</tr>
<tr>
<td>글 비밀번호</td>
<td>
<input type="password" name="pass" >
</td>
</tr>
<tr>
<td>제목</td>
<td>
<input type="text" name="subject" >
</td>
</tr>
<tr>
<td>내용</td>
<td>
<textarea rows="10" cols="20" name="content"></textarea>
</td>
</tr>
</table>
<div id="table_search">
<input type="submit" value="글쓰기" class="btn">
</div>
<div class="clear"></div>
</form>
<div id="page_control">
</div>

</article>
<!-- 게시판 -->
<!-- 본문들어가는 곳 -->
<div class="clear"></div>
<!-- 푸터들어가는 곳 -->
<jsp:include page="../inc/bottom.jsp" />
<!-- 푸터들어가는 곳 -->
</div>
</body>




writePro.jsp생성하기 전에 해야할 세가지

  1. DB테이블 생성
  2. BoardBean객체 필요
  3. BoardDAO객체필요
  4. writePro.jsp생성




DB테이블 생성




BoardBean.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
public class BoardBean {
private int bno;
private String name;
private String pw;
private String subject;
private String content;
private int readcount;
private int re_ref;
private int re_seq;
private int re_lev;
private Date date;
private String ip;
private String file;

public int getBno() {
return bno;
}
public void setBno(int bno) {
this.bno = bno;
}

(중략)

@Override
public String toString() {
return "BoardBean [bno=" + bno + ", name=" + name + ", pw=" + pw + ", subject=" + subject + ", content="
+ content + ", readcount=" + readcount + ", re_ref=" + re_ref + ", re_seq=" + re_seq + ", re_lev="
+ re_lev + ", date=" + date + ", ip=" + ip + ", file=" + file + "]";
}
}




BoardDAO.java생성

  1. DB연결메서드 생성
  2. 자원해제메서드 생성
  3. insertBoard 메서드 생성
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
Connection con = null;
String sql = "";
PreparedStatement pstmt = null;
ResultSet rs = null;

private Connection getCon() throws Exception {
Context init = new InitialContext();
DataSource ds = (DataSource) init.lookup("java:comp/env/jdbc/webpro");
con = ds.getConnection();
System.out.println("디비연결성공");
return con;
}

//자원해제메서드
public void closeDB(){
try{
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

//글쓰기메서드
public void insertBoard(BoardBean bb){
int num = 0;
try {
// 디비연결
con = getCon();

// sql(1) 글번호 계산,(2) 글정보를 저장
sql = "select max(bno) from fun_board";

pstmt = con.prepareStatement(sql);
// 실행
rs = pstmt.executeQuery();

if(rs.next()){
num = rs.getInt(1)+1;
}

System.out.println("저장될 글번호 : "+num);

// (2) 글정보를 저장
// sql
sql = "insert into fun_board (bno,name,pw,subject,content,"
+ "readcount,re_ref,re_lev,re_seq,date,ip,file) "
+ "values(?,?,?,?,?,?,?,?,?,now(),?,?)";

pstmt = con.prepareStatement(sql);

pstmt.setInt(1, num);
pstmt.setString(2, bb.getName());
pstmt.setString(3, bb.getPw());
pstmt.setString(4, bb.getSubject());
pstmt.setString(5, bb.getContent());
pstmt.setInt(6, 0); //readcount
pstmt.setInt(7, num); // re_ref (답글 그룹번호 == 글번호)
pstmt.setInt(8, 0); // re_lev 일반글 0
pstmt.setInt(9, 0); // re_seq 일반글 0
pstmt.setString(10, bb.getIp());
pstmt.setString(11, bb.getFile());

// 실행
pstmt.executeUpdate();

System.out.println(num+"번 글쓰기 완료!");

} catch (Exception e) {
e.printStackTrace();
}finally {
closeDB();
}
}//insertBoard닫음




writePro.jsp 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%
request.setCharacterEncoding("UTF-8");
//1. DB테이블 생성
//2. BoardBean객체 필요
//3. BoardDAO객체필요
%>
<!-- 4. BoardBean 객체 저장(액션태그) -->
<jsp:useBean id="bb" class="com.itwillbs.bean.BoardBean"/>
<jsp:setProperty property="*" name="bb"/>
<%
//5. ip정보 추가
bb.setIp(request.getRemoteAddr());

//6. BoardDAO객체생성
BoardDAO bdao = new BoardDAO();
bdao.insertBoard(bb);

//7. 페이지이동
response.sendRedirect("notice.jsp");
%>

Comments