관리 메뉴

nkdk의 세상

24장 게시판을 만들어 봅시다. 본문

My Programing/JSP and Servlet

24장 게시판을 만들어 봅시다.

nkdk 2008. 3. 9. 01:07
간단한 기능 구현입니다.
boardwrite.jsp, index.jsp, board.css, boardData.java, boardMgr.java, connbean2.java, boardsave.jsp

boardwrite.jsp

<%@ page contentType="text/html; charset=EUC-KR" %>
<html><head><title>게시판</title>
<link rel="stylesheet" type="text/css" href="board.css">
<script>
function check(){
if(frm.name.value==""){
alert("이름을 입력하세요");
frm.name.focus();
}else if(frm.pass.value ==""){
alert("비밀번호를 입력하세요");
frm.pass.focus();
}else if(frm.mail.value ==""){
alert("이메일을 입력하세요");
frm.mail.focus();
}else if(frm.title.value ==""){
alert("제목을 입력하세요");
frm.title.focus();
}else if(frm.cont.value ==""){
alert("내용을 입력하세요");ㄱ
frm.cont.focus();
}else
frm.submit();
}
</script>
</head>
<body topmargin=50 leftmargin=50 onLoad=frm.name.focus()>
<center><h2>*** 글쓰기 ***</h2>
<form name=frm method=post action="boardsave.jsp">
<table border=1 style="border-collapse=collapse" width=530>
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
<tr>
<td align=center width=100>이 름</td>
<td width=430><input name=name size=15></td>
</tr>
<tr>
<td align=center >암 호</td>
<td width=430><input type=password name=pass size=15></td>
</tr>
<tr>
<td align=center>메 일</td>
<td><input name=mail size=25></td>
</tr>
<tr>
<td align=center>제 목</td>
<td><input name=title size=50></td>
</tr>
<tr>
<td align=center>내 용</td>
<td><textarea name=cont cols=50 rows=12></textarea></td>
</tr>
<tr>
<td colspan=2 align=center height=30>
<input type=button value="메 인" onClick="location.href('index.jsp')"> &
<input type=button value="작 성" onClick="check()" > &
<input type=button value="목 록" onClick="location.href('boardlist.jsp')">
</td>
</tr>
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
</table></form></center></body></html>

// index.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>
</head>
<body><h2>*** 홈페이지 ***</h2>
<a href=boardlsit.jsp>게시판</a>

</body>
</html>

// board.css

td{font-size:9pt}
a{text-decoration: none}
input{border:0;background: efefef}
TEXTAREA {
overflow-y:auto;
scrollbar-face-color:#ffffff;
scrollbar-highlight-color:navy;
scrollbar-3dlight-color:#ffffff;
scrollbar-shadow-color:navy;
scrollbar-darkshadow-color:#ffffff;
scrollbar-track-color:#ffffff;
scrollbar-arrow-color:navy;
}

// boardData.java

package pack;
import java.util.*;

public class BoardData {
String name, mail, pass, title, date, cont, bip;
int num, readcnt, gnum, onum, nested;

public String getBip() {
return bip;
}
public void setBip(String bip) {
this.bip = bip;
}
public String getCont() {
return cont;
}
public void setCont(String cont) {
this.cont = cont;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}

public void setDate(){ // 오늘 날짜를 구한다.
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1;
int day = cal.get(Calendar.DATE);
this.date = year+"/"+month+"/"+day;
}

public int getGnum() {
return gnum;
}
public void setGnum(int gnum) {
this.gnum = gnum;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNested() {
return nested;
}
public void setNested(int nested) {
this.nested = nested;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getOnum() {
return onum;
}
public void setOnum(int onum) {
this.onum = onum;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public int getReadcnt() {
return readcnt;
}
public void setReadcnt(int readcnt) {
this.readcnt = readcnt;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}


// boardmgr.java

package pack;
import java.sql.*;

public class BoardMgr {
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
DBConnectionMgr pool;

public BoardMgr(){
try {
pool = DBConnectionMgr.getInstance();
conn = pool.getConnection();
} catch (Exception e) {
System.out.println("BoardMgr DB연결 실패: "+e);
return;
}
}

public int currentGetnum() {
// 레코드 최대 번호 획득
int cnt = 0;
try{
String sql = "select max(num) as num from board";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
cnt = rs.getInt("num");
System.out.println("숫자:"+cnt);
} catch (Exception e) {
System.out.println("SQL오류 : "+e);
}finally{
try {
pool.freeConnection(conn,pstmt,rs);
} catch (Exception e) {}
}
System.out.println("ss" + cnt);
return cnt;
}

public boolean saveData(BoardData data) {
boolean cnt = false;
try {
String sql = "insert into board values(?,?,?,?,?,?,?,?,?,?,?,?)";
conn = pool.getConnection();
System.out.println(sql);
System.out.println(currentGetnum()+" "+data.getName()+" "+data.getPass()+" "+data.getMail()+ " "
+ data.getTitle()+ " " + data.getCont()+ " " +data.getBip() + " " + data.getDate() + " "
+ data.getReadcnt() + " " + data.getNum());
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, data.getNum());
pstmt.setString(2, data.getName());
pstmt.setString(3, data.getPass());
pstmt.setString(4, data.getMail());
pstmt.setString(5, data.getTitle());
pstmt.setString(6, data.getCont());
pstmt.setString(7, data.getBip());
pstmt.setString(8, data.getDate());
pstmt.setInt(9, 0);
pstmt.setInt(10, data.getGnum());
pstmt.setInt(11, 0);
pstmt.setInt(12, 0);
int re = pstmt.executeUpdate();
if(re > 0) cnt = true;
} catch (Exception e) {
System.out.println("저장실패:"+e);
}finally{
try {
pool.freeConnection(conn,pstmt);
} catch (Exception e) {}
}
return cnt;
}
}


// boardsave.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" %>
<% request.setCharacterEncoding("EUC-KR"); %>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:setProperty property="*" name="data" />
<% // 키보드를 통해 5개 항목은 채움 : name, pass, mail, title, cont %>
<jsp:useBean id="mgr" class="pack.BoardMgr" />
<% // 키보드를 통해 5개 항목은 채움 : name, pass, mail, title, cont
data.setName(request.getParameter("name"));
data.setPass(request.getParameter("pass"));
data.setMail(request.getParameter("mail"));
data.setTitle(request.getParameter("title"));
data.setCont(request.getParameter("cont"));
data.setBip(request.getRemoteAddr()); // IP를 취한다.
data.setDate(); // 작성일
int max = mgr.currentGetnum() + 1;
data.setNum(max); // 레코드 번호
data.setGnum(max); // 해당 레코드 댓글 그룹 번호

out.println("<html><body>");
if(mgr.saveData(data)) {
out.println("저장성공");
}else {
out.println("저장실패");
}
out.println("</html></body>");
%>