게시판게시판해. jsp board.bean

it/JSP 2014. 8. 27. 15:01 Posted by 하얀나다

다오 아직 미작성.. 좀더 완성 시켜야됨


boardinputform.jsp


<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>게시판 글쓰기</title>

<script type="text/javascript">

function insertBoard(){

document.frm.action = "BoardSave.jsp";

document.frm.submit();

}

</script>

</head>

 <body>

<h4> 게시판 글 쓰기 </h4><br/>

나중에 이쁘게 만드시오 <br/><br/>

<form name='frm' method='post'>

작성자 : <input type='text' name = 'writerName'><br/><br/>

제  목 : <input type='text' name = 'title'><br/><br/>

내  용 : <textarea rows='10' cols='40' name = 'content'></textarea><br/><br/>

패스워드(수정/삭제시 필요) :

<input type='password' name = 'password'><br/><br/>

<input type='button' value='작성' onclick = "insertBoard()">

<input type='reset' value='취소'>

</form>


</body>

</html>


boardsave.jsp


<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

<%@ page import="board.model.*,board.service.*"%>


<%

// 0. 넘겨받는 한글 깨지지 않도록 지정

request.setCharacterEncoding("euc-kr");

%>


<!--  1. 전 화면 입력값을 넘겨받아 BoardRec 클래스의 각 멤버필드에 지정 -->

<jsp:useBean id="rec" class="board.model.BoardRec">

<jsp:setProperty name="rec" property="*" />

</jsp:useBean>

<%

// 2. Service클래스에 write() 함수호출

BoardRec result = WriteArticleService.getInstance().write(rec);

//3.result 값을 화면에 출력하세요.

%>


<%=result.getWriterName() %>

<%=result.getTitle() %>

<%=result.getContent() %>

<%=result.getPassword() %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>게시판글저장</title>

</head>

<body>

입력되었는지 확인해보시구염...

<br /> 만일 안되어도..환장하지 맙시다 !!! ^^

</body>

</html>


boardlist.jsp



<%@ page contentType="text/html;charset=euc-kr"%>

<%@ page import="board.model.*, board.service.*"%>

<%@ page import="java.util.List"%>


<%

//웹브라우저가 게시글 목록을 캐싱할 경우 새로운 글이 추가되더라도 새글이 목록에 안 보일 수 있기 때문에 설정

response.setHeader("Pragma", "No-cache"); // HTTP 1.0 version

response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1 version

response.setHeader("Cache-Control", "no-store"); // 일부 파이어폭스 버스 관련

response.setDateHeader("Expires", 1L); // 현재 시간 이전으로 만료일을 지정함으로써 응답결과가 캐쉬되지 않도록 설정

%>


<%

// Service에 getArticleList()함수를 호출하여 전체 메세지 레코드 검색 


ListArticleService service = ListArticleService.getInstance();


List<BoardRec> mList = service.getArticleList();

%>


<HTML>

<head>

<title>게시글 목록</title>

</head>


<BODY>


<h3>게시판 목록</h3>


<table border="1" bordercolor="darkblue">

<tr>

<td>글번호</td>

<td>제 목</td>

<td>작성자</td>

<td>작성일</td>

<td>조회수</td>

</tr>


<%

if (mList == null) {

%>

<tr>

<td colspan="5">등록된 게시물이 없습니다.</td>

</tr>

<%

} else {

%>

<%

for (BoardRec rec : mList) {

%>

<tr>

<td><%=rec.getArticleId() %></td>

<td><a href = "BoardView.jsp?articleid=<%=rec.getArticleId() %>"><%=rec.getTitle() %></a></td>

<td><%=rec.getWriterName() %></td>

<td><%=rec.getPostingDate() %></td>

<td><%=rec.getReadCount() %></td>

</tr>

<%

}

%>

<%

} // end else

%>


<tr>

<td colspan="5"><a href="BoardInputForm.jsp">글쓰기</a></td>

</tr>

</table>

</BODY>

</HTML>


boardview.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="board.service.*, board.model.*" %>
<%
// 1. 해당 게시물의 게시글번호값을 얻어온다
String id = request.getParameter("articleid");
// 2. Service에 getArticleById() 호출하여 그 게시글번호를 갖는 레코드를 검색한다.
ViewArticleService service = ViewArticleService.getInstance();
 
BoardRec rec = service.getArticleById(id);
%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title> 게시글 보기 </title>
</head>
<body>

<h4> 게시판 글 보기 </h4><br/>
<table border="1" bordercolor="red">
<tr>
<td> 제  목 </td>
<td><%=rec.getTitle() %></td>
</tr>
<tr>
<td> 작성자 </td>
<td><%=rec.getWriterName() %></td>
</tr>
<tr>
<td> 작성일자  </td>
<td><%=rec.getPostingDate() %></td>
</tr>
<tr>
<td> 내  용  </td>
<td><%=rec.getContent() %></td>
</tr>
<tr>
<td colspan="2">
<input type = "hidden" value="1">
<a href ="BoardList.jsp">목록보기 </a>
<a href ="#">답변하기 </a>
<a href ="BoardModifyForm.jsp?id=<%=id%>">수정하기 </a>
<a href ="BoardDeleteForm.jsp?id=<%=id%>">삭제하기 </a>
</td>
</tr>
</table>


</body>
</html>

boarddeleteform.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="board.service.*, board.model.*" %>
<%
// 1. 삭제할 레코드의 게시글번호 넘겨받기
String id = request.getParameter("id");
%>      
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title> 게시글 삭제하기 </title>
</head>
<body>

<form method="post" action="BoardDelete.jsp">
삭제할 글암호를 입력하세요 <br/>
<input type="password" name="password">
<!-- 게시글번호를 다음 페이지로 넘기기 위해 hidden 태그로 지정 -->
<input type="hidden" name = "id" value="<%=id%>">
<input type="submit" value="삭제하기">
</form>

</body>
</html>

boarddelete.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="board.model.*,board.service.*"%>

<%
int result = 0;
// 1. 삭제할 레코드의 게시글번호와 비밀번호를 넘겨받기
String id = request.getParameter("id");
String pass = request.getParameter("password");
// 2. Service에 delete() 호출
DeleteArticleService service = DeleteArticleService.getInstance();
  result = service.delete(id,pass);

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>게시글 삭제</title>
</head>
<body>
<%
if (result != 0) {
%>
글을 삭제하였습니다.
<%
} else {
%>
삭제가 실패되었습니다.
<%
}
%>
<br />
<br />
<a href="BoardList.jsp"> 목록보기 </a>
</body>
</html>

boardmodifyform.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="board.service.*, board.model.*"%>
<%
// 1. 수정할 레코드의 게시글번호를 넘거받기
String id = request.getParameter("id");
// 2. Service에 getArticleById()함수를 호출하여 그 게시글번호의 레코드를 검색
ViewArticleService service = ViewArticleService.getInstance();
 
BoardRec rec = service.getArticleById(id);
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>게시글 수정하기</title>
</head>
<body>
<h4>게시판 글 수정하기</h4>
<br />
<form name='frm' method='post' action="BoardModify.jsp">
제 목 : <input type='text' value = '<%=rec.getTitle() %>'  name='title'><br />
<br /> 패스워드(수정/삭제시 필요) : <input type='password' name = 'password'> <br />
<br /> 내 용 :
<textarea name='content' rows='10' cols='40'><%=rec.getContent() %></textarea>
<br />
<input type="hidden" name = "articleId" value="<%=id %>">
<br /> <input type='submit' value='수정하기'> <input type='button'
value='목록보기' onclick="window.location='BoardList.jsp'">
</form>

</body>
</html>

boardmodify.jsp


<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

<%@ page import="board.model.*,board.service.*"%>


<%

// 0. 넘겨받는 데이타의 한글 처리

request.setCharacterEncoding("euc-kr");



%>


<!-- 1. 이전 화면의 입력값을 넘겨받아 BoardRec 객체의 각 멤버변수로 지정 -->

<jsp:useBean id="rec" class="board.model.BoardRec">

<jsp:setProperty name="rec" property="*" />

</jsp:useBean>


<%

// 2. Service에 update() 호출하여 레코드 수정

int result = ModifyArticleService.getInstance().update(rec);

String id = request.getParameter("articleId");

String pass = request.getParameter("password");

String title = request.getParameter("title");

String content = request.getParameter("content");

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>게시판글수정</title>

</head>

<body>

<%=rec.getArticleId()%>

<%=rec.getPassword()%>

<%=rec.getTitle()%>

<%=rec.getContent()%>





<%

// 게시글 수정이 성공적으로 되었다면 그 해당 게시글을 보여주는 페이지로 이동하고

// 그렇지 않다면, "암호가 잘못 입력되었습니다"를 출력

if (result != 0) {

response.sendRedirect("BoardView.jsp?articleid=" + rec.getArticleId());


} else { 

%>

암호가 잘못 입력되었다. 확인하라.

<%

}

%>


</body>

</html>


boarddao.java


package board.model;


import guest.model.Message;


import java.sql.*;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;


public class BoardDao {


// Single Pattern

private static BoardDao instance;


// DB 연결시 관한 변수

private static final String dbDriver = "oracle.jdbc.driver.OracleDriver";

private static final String dbUrl = "jdbc:oracle:thin:@localhost:1521:orcl";

private static final String dbUser = "scott";

private static final String dbPass = "tiger";


private Connection con;


// --------------------------------------------

// ##### 객체 생성하는 메소드

public static BoardDao getInstance() throws BoardException {

if (instance == null) {

instance = new BoardDao();

}

return instance;

}


private BoardDao() throws BoardException {


try {


/********************************************

* 1. 오라클 드라이버를 로딩 ( DBCP 연결하면 삭제할 부분 )

*/

Class.forName(dbDriver);

} catch (Exception ex) {

throw new BoardException("DB 연결시 오류  : " + ex.toString());

}


}


// --------------------------------------------

// ##### 게시글 입력전에 그 글의 그룹번호를 얻어온다

public int getGroupId() throws BoardException {

PreparedStatement ps = null;

ResultSet rs = null;

int groupId = 1;

try {

con = DriverManager.getConnection(dbUrl, dbUser, dbPass);

String sql = "SELECT  SEQ_GROUP_ID_ARTICLE.nextval AS gid FROM  dual";

ps = con.prepareStatement(sql);

rs = ps.executeQuery();

if (rs.next()) {

groupId = rs.getInt("gid");

}


return groupId;

} catch (Exception ex) {

throw new BoardException("게시판 ) 게시글 입력 전에 그룹번호 얻어올 때  : "

+ ex.toString());

} finally {

if (ps != null) {

try {

ps.close();

} catch (SQLException ex) {

}

}

if (con != null) {

try {

con.close();

} catch (SQLException ex) {

}

}

}

}


// --------------------------------------------

// ##### 게시판에 글을 입력시 DB에 저장하는 메소드

// ( Article 테이블에 게시글을 입력하고 시퀀스로 지정된 게시글번호를 리턴

public int insert(BoardRec rec) throws BoardException {


/************************************************

*/

ResultSet rs = null;

Statement stmt = null;

PreparedStatement ps = null;

try {


con = DriverManager.getConnection(dbUrl, dbUser, dbPass);

String Sql = "INSERT INTO ARTICLE (ARTICLE_ID,GROUP_ID,SEQUENCE_NO, POSTING_DATE,READ_COUNT,WRITER_NAME, TITLE, CONTENT,PASSWORD) VALUES( SEQ_ARTICLE_ID_ARTICLE.nextval, ? , ? , sysdate ,0 , ? , ? , ? , ? )";

ps = con.prepareStatement(Sql);


ps.setInt(1, rec.getGroupId());

ps.setString(2, rec.getSequenceNo());

ps.setString(3, rec.getWriterName());

ps.setString(4, rec.getTitle());

ps.setString(5, rec.getContent());

ps.setString(6, rec.getPassword());


ps.executeUpdate();


return -1;


} catch (Exception ex) {

throw new BoardException("게시판 ) DB에 입력시 오류  : " + ex.toString());

} finally {

if (rs != null) {

try {

rs.close();

} catch (SQLException ex) {

}

}

if (stmt != null) {

try {

stmt.close();

} catch (SQLException ex) {

}

}

if (ps != null) {

try {

ps.close();

} catch (SQLException ex) {

}

}

if (con != null) {

try {

con.close();

} catch (SQLException ex) {

}

}

}


}


// --------------------------------------------

// ##### 전체 레코드를 검색하는 함수

// 리스트에 보여줄거나 필요한 컬럼 : 게시글번호, 그룹번호, 순서번호, 게시글등록일시, 조회수, 작성자이름, 비밀번호, 제목

// ( 내용 제외 )

// 순서번호(sequence_no)로 역순정렬

public List<BoardRec> selectList() throws BoardException {

PreparedStatement ps = null;

ResultSet rs = null;

List<BoardRec> mList = new ArrayList<BoardRec>();

boolean isEmpty = true;


try {

Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPass);

String Sql = "SELECT * FROM ARTICLE order by article_id desc";

ps = con.prepareStatement(Sql);


rs = ps.executeQuery();

while (rs.next()) {

BoardRec msg = new BoardRec();

msg.setArticleId(rs.getInt("article_id"));

msg.setTitle(rs.getString("title"));

msg.setWriterName(rs.getString("writer_name"));

msg.setPostingDate(rs.getString("posting_date"));

msg.setReadCount(rs.getInt("read_count"));


mList.add(msg);

isEmpty = false;


}

if (isEmpty)

return Collections.emptyList();


return mList;

} catch (Exception ex) {

throw new BoardException("게시판 ) DB에 목록 검색시 오류  : " + ex.toString());

} finally {

if (rs != null) {

try {

rs.close();

} catch (SQLException ex) {

}

}

if (ps != null) {

try {

ps.close();

} catch (SQLException ex) {

}

}

if (con != null) {

try {

con.close();

} catch (SQLException ex) {

}

}

}

}


// --------------------------------------------

// ##### 게시글번호에 의한 레코드 검색하는 함수

public BoardRec selectById(int id) throws BoardException {

PreparedStatement ps = null;

ResultSet rs = null;


BoardRec rec = new BoardRec();


try {

Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPass);

String Sql = "SELECT * FROM ARTICLE WHERE article_id = ? order by article_id desc";

ps = con.prepareStatement(Sql);

ps.setInt(1, id);

rs = ps.executeQuery();

while (rs.next()) {


rec.setTitle(rs.getString("title"));

rec.setWriterName(rs.getString("writer_name"));

rec.setPostingDate(rs.getString("posting_date"));

rec.setContent(rs.getString("content"));


}


return rec;

} catch (Exception ex) {

throw new BoardException("게시판 ) DB에 글번호에 의한 레코드 검색시 오류  : "

+ ex.toString());

} finally {

if (rs != null) {

try {

rs.close();

} catch (SQLException ex) {

}

}

if (ps != null) {

try {

ps.close();

} catch (SQLException ex) {

}

}

if (con != null) {

try {

con.close();

} catch (SQLException ex) {

}

}

}

}


// --------------------------------------------

// ##### 게시글 보여줄 때 조회수 1 증가

public void increaseReadCount(String article_id) throws BoardException {


PreparedStatement ps = null;

try {

con = DriverManager.getConnection(dbUrl, dbUser, dbPass);

String Sql = "update ARTICLE set read_count = read_count+1 where article_id = ?";

ps = con.prepareStatement(Sql);

ps.setString(1, article_id);

ps.executeUpdate();


} catch (Exception ex) {

throw new BoardException("게시판 ) 게시글 볼 때 조회수 증가시 오류  : "

+ ex.toString());

} finally {

if (ps != null) {

try {

ps.close();

} catch (SQLException ex) {

}

}

if (con != null) {

try {

con.close();

} catch (SQLException ex) {

}

}

}


}


// --------------------------------------------

// ##### 게시글 수정할 때

// ( 게시글번호와 패스워드에 의해 수정 )

public int update(BoardRec rec) throws BoardException {


PreparedStatement ps = null;

try {

con = DriverManager.getConnection(dbUrl, dbUser, dbPass);

String Sql = "update ARTICLE set TITLE = ? ,CONTENT = ? where article_id = ? and PASSWORD = ?";

ps = con.prepareStatement(Sql);

ps.setString(1, rec.getTitle());

ps.setString(2, rec.getContent());

ps.setInt(3, rec.getArticleId());

ps.setString(4, rec.getPassword());

return ps.executeUpdate(); // 나중에 수정된 수를 리턴하시오.


} catch (Exception ex) {

throw new BoardException("게시판 ) 게시글 수정시 오류  : " + ex.toString());

} finally {

if (ps != null) {

try {

ps.close();

} catch (SQLException ex) {

}

}

if (con != null) {

try {

con.close();

} catch (SQLException ex) {

}

}

}


}


// --------------------------------------------

// ##### 게시글 삭제할 때

// ( 게시글번호와 패스워드에 의해 삭제 )

public int delete(int article_id, String password) throws BoardException {


PreparedStatement ps = null;

try {

con = DriverManager.getConnection(dbUrl, dbUser, dbPass);

String Sql = "DELETE FROM ARTICLE WHERE article_id = ? and password = ?";

ps = con.prepareStatement(Sql);


ps.setInt(1, article_id);

ps.setString(2, password);


return ps.executeUpdate(); // 나중에 수정된 수를 리턴하시오.


} catch (Exception ex) {

throw new BoardException("게시판 ) 게시글 수정시 오류  : " + ex.toString());

} finally {

if (ps != null) {

try {

ps.close();

} catch (SQLException ex) {

}

}

if (con != null) {

try {

con.close();

} catch (SQLException ex) {

}

}

}


}


// ----------------------------------------------------

// ##### 부모레코드의 자식레코드 중 마지막 레코드의 순서번호를 검색

// ( 제일 작은 번호값이 마지막값임)

public String selectLastSequenceNumber(String maxSeqNum, String minSeqNum)

throws BoardException {

PreparedStatement ps = null;

ResultSet rs = null;


try {

con = DriverManager.getConnection(dbUrl, dbUser, dbPass);

String sql = "SELECT min(sequence_no) as minseq FROM article WHERE sequence_no < ? AND sequence_no >= ?";

ps = con.prepareStatement(sql);

ps.setString(1, maxSeqNum);

ps.setString(2, minSeqNum);

rs = ps.executeQuery();

if (!rs.next()) {

return null;

}


return rs.getString("minseq");

} catch (Exception ex) {

throw new BoardException("게시판 ) 부모와 연관된 자식 레코드 중 마지막 순서번호 얻어오기  : "

+ ex.toString());

} finally {

if (rs != null) {

try {

rs.close();

} catch (SQLException ex) {

}

}

if (ps != null) {

try {

ps.close();

} catch (SQLException ex) {

}

}

if (con != null) {

try {

con.close();

} catch (SQLException ex) {

}

}

}

}

}