관리 메뉴

nkdk의 세상

26장 전체적인 약간의 수정 딜리트, 댓글삭제, 에디트, 관리자로 본문

My Programing/JSP and Servlet

26장 전체적인 약간의 수정 딜리트, 댓글삭제, 에디트, 관리자로

nkdk 2008. 3. 9. 01:09
전체적으로 약간의 수정이 있을 듯 하네요.
그리고

// delete

<%@ page contentType="text/html; charset=EUC-KR" %>
<%!
String spage;
String snum;
%>
<%
request.setCharacterEncoding("EUC-KR");
snum = request.getParameter("num");
spage = request.getParameter("page");
%>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:useBean id="mgr" class="pack.BoardDelMgr" />
<%!


%>
<html><head><title>글고침</title>
<link rel="stylesheet" type="text/css" href="board.css">
<% mgr.checkPass(snum, spage); %>
<script>
function check(){
frm.submit();
}
</script>
</head>
<body topmargin=50 leftmargin=50 onLoad=frm.name.focus()>
<center><h2>*** 글 삭제 ***</h2>

<form name=frm method=post action="deletesave.jsp">
<center>비밀번호를 입력하여 주세요<p>
<input type=hidden name=num value="<%=snum %>">
<input type=hidden name=page value="<%=spage %>">
<input type=password name=pass size=15%>
<b>
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="메 인" onClick="location.href('index.jsp')"> &
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="삭 제" onClick="check()" > &
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="목 록" onClick="location.href('boardlist.jsp?page=<%=spage %>')">
</table></form>
</center></body></html>

//boarddelmgr.java

package pack;
import java.sql.*;

public class BoardDelMgr {
Connection conn, conn2;
PreparedStatement pstmt;
PreparedStatement pstmt2;
ResultSet rs, rs2;
DBConnectionMgr pool;

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

public void delFile(int snum) { // 실제 snum으로 받아오는 화일을 삭제하는 메소드
boolean cnt = false;
try{
String sql = "delete from board where num=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, snum);
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) {}
}
}

public boolean delData(String snum) {
boolean flag = false; // 값에 대한 수정이 이루어 졌는가 아닌가를 확인할 때 사용
int nest = 0; // 해당 값에 대한 nest 값을 받아
String gnum = ""; // 그룹 번호를 저장할 장소

try {
String sql2 = "select * from board where num = "+snum; // gnum그룹을 받아 오기 위해 실행
pstmt2 = conn.prepareStatement(sql2);
rs2 = pstmt2.executeQuery();
if(rs2.next()) {
gnum = rs2.getString("gnum");
}
// gnum으로 찾고, onum 으로 정렬 한다. 그러면 게시판 순서대로 나오게 될것이다.
sql2 = "select * from board where gnum = "+gnum+" order by onum";
pstmt2 = conn.prepareStatement(sql2);
rs2 = pstmt2.executeQuery();
// 1번을 통과 하면 flag가 true 가 되고 2번 작업이 수행되게 된다.
while(rs2.next()) {
/* 2번 */ if (flag) { // 값에 대해 적용이 되면 적용된 값 부터 지워가기 시작한다.
// 해당 값이 현재의 nest >= nested 하게된다면 return flag 하여 종료시킨다.
if(nest >= rs2.getInt("nested")) {
return flag;
}
delFile(rs2.getInt("num")); // 값을 만족 하기 전까지는 계속 지운다.
}
/* 1번 */ if(rs2.getString("num").equals(snum)) { // 값을 만족하면 nest에 값을넣고 flag를 true시킨다.
nest = rs2.getInt("nested");
delFile(rs2.getInt("num")); // 자신의 파일을 먼저 삭제 시킨다.
flag = true;
}
}
} catch (Exception e) {
System.out.println("저장실패:"+e);
}finally{
try {
pool.freeConnection(conn,pstmt);
} catch (Exception e) {}
}
return flag;
}

public boolean checkPass(String num, String spass) {
boolean b= false;
System.out.println(num+spass);
String sql = "select pass from board where num = "+ num;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()) {
if(spass.equals(rs.getString(1))) b = true;
}
} catch (Exception e) {
System.out.println("checkPass Error:" +e);
}finally{
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
} catch (Exception ex) {}
}
return b;
}
}

// deletesave.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" />

<jsp:useBean id="mgr" class="pack.BoardDelMgr" />
<% // 키보드를 통해 5개 항목은 채움 : name, pass, mail, title, cont
String snum =request.getParameter("num");
String spage = request.getParameter("page");

boolean b = mgr.checkPass(snum, data.getPass());
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를 취한다.

out.println("<html><body>");

if(b) {
if(mgr.delData(snum)) {
out.println("삭제성공");
%>
<script>
alert("삭제 되었습니다.");
</script>
<% response.sendRedirect("boardlist.jsp?page="+spage);
}else {
out.println("삭제실패");
response.sendRedirect("boardlist.jsp?snum="+request.getParameter("num")+"&page="+request.getParameter("spage"));
return;
}
} else {
%>
<script>
alert("비밀번호 불일치로 삭제가 되지 않습니다.");
<% response.sendRedirect("boardcontent.jsp?snum="+request.getParameter("num")+"&page="+request.getParameter("spage")); %>
</script>
<% }
out.println("</html></body>");
%>


//edit.jsp

<%@ page contentType="text/html; charset=EUC-KR" %>
<jsp:useBean id="data" class="pack.BoardData" />
<jsp:useBean id="mgr" class="pack.editMgr" />
<%!
int snum;
String spage;
%>
<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.cont.value ==""){
alert("내용을 입력하세요");
frm.cont.focus();
}else
frm.submit();
}
</script>
</head>
<body topmargin=50 leftmargin=50 onLoad=frm.name.focus()>
<center><h2>*** 글 수정 ***</h2>
<%
request.setCharacterEncoding("EUC-KR");
snum = Integer.parseInt(request.getParameter("num"));
spage = request.getParameter("page");

//원글의 gnum, onum, nested, title 가져오기
data = mgr.getData(snum);

int gnum = data.getGnum();
int onum = data.getOnum();
int nested = data.getNested();
String title = data.getTitle();
String cont = data.getCont();
String mail = data.getMail();
String name = data.getName();
String pass = data.getPass();

%>
<form name=frm method=post action="editsave.jsp">
<table border=4 bordercolor="#AAAAAA" style="border-collapse=collapse" width=530 cellspacing="1" cellpadding="0">
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
<tr bgcolor="#BBEEBB">
<input type="hidden" name="num" value="<%=snum %>">
<input type="hidden" name="page" value="<%=spage %>">
<input type="hidden" name="gnum" value="<%=gnum %>">
<input type="hidden" name="onum" value="<%=onum %>">
<input type="hidden" name="nested" value="<%=nested %>">
<td align=center width=100>이 름</td>
<td width=430><input name=name size=15 value="<%=name %>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center >암 호</td>
<td width=430><input type=password name=pass size=15%>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>메 일</td>
<td><input name=mail size=25 value="<%=mail %>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>제 목</td>
<td><input name=title size=50 value="<%=title %>"></td>
</tr>
<tr bgcolor="#BBEEBB">
<td align=center>내 용</td>
<td><textarea name=cont cols=50 rows=12><%= cont %></textarea></td>
</tr>
<tr bgcolor="#BBEEBB">
<td colspan=2 align=center height=30>
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="메 인" onClick="location.href('index.jsp')"> &
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="작 성" onClick="check()" > &
<input style='border:1px solid #AA0000;background-color:white;font-family:Tahoma;font-size:12px' type=button value="목 록" onClick="location.href('boardlist.jsp?page=<%=spage %>')">
</td>
</tr>
<tr><td colspan=2 height=50 align=center><img src="images/bar.gif"></td></tr>
</table></form>
</center></body></html>

// editsave.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" />

<jsp:useBean id="mgr" class="pack.editMgr" />
<% // 키보드를 통해 5개 항목은 채움 : name, pass, mail, title, cont
String snum =request.getParameter("num");
String spage = request.getParameter("page");

boolean b = mgr.checkPass(snum, data.getPass());
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를 취한다.

out.println("<html><body>");

if(b) {
if(mgr.updateData(data)) {
out.println("저장성공");
}else {
out.println("저장실패");
}
response.sendRedirect("boardlist.jsp?snum="+request.getParameter("num")+"&page="+request.getParameter("spage"));
} else {
%>
<<script>
alert("비밀번호 불일치");
history.back();
</script>
<% }
out.println("</html></body>");
%>

// editMgr.java

package pack;
import java.sql.*;

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

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

public BoardData getData(int num) {
BoardData bda = new BoardData();
String sql = "select * from board where num = " + num;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString(4));
System.out.println(rs.getString(5));
System.out.println(rs.getString(6));
System.out.println(rs.getString(7));
System.out.println(rs.getString(8));

bda.setTitle(rs.getString("title"));
bda.setGnum(rs.getInt("gnum"));
bda.setOnum(rs.getInt("onum"));
bda.setNested(rs.getInt("nested"));
bda.setCont(rs.getString("cont"));
bda.setMail(rs.getString("mail"));
bda.setPass(rs.getString("pass"));
bda.setName(rs.getString("name"));

} catch (Exception e) {
System.out.println("getData 오류 : " + e);
} finally {
pool.freeConnection(conn, pstmt, rs);
}
return bda;
}

public boolean updateData(BoardData data) {
boolean cnt = false;
try {
String sql = "update board set name='"+data.getName()+"', mail='"+data.getMail()+
"', title='"+data.getTitle()+"', cont='"+ data.getCont()+"', bip='"+
data.getBip() + "' where num="+data.getNum();
conn = pool.getConnection();
pstmt = conn.prepareStatement(sql);
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;
}

public boolean checkPass(String num, String spass) {
boolean b= false;
String sql = "select pass from board where num = "+ num;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()) {
if(spass.equals(rs.getString(1))) b = true;
}
} catch (Exception e) {
System.out.println("checkPass Error:" +e);
}finally{
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
} catch (Exception ex) {}
}
return b;
}
}


//admin.jsp


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

<link rel="stylesheet" type="text/css" href="board.css">
<script>

function check() {
if(frm.id.value=="") {
alert("아이디입력");
frm.id.focus();
return;
}
if(frm.pass.value==""){
alert("비밀번호 입력");
frm.pass.focus();
return;
}
frm.submit();
}
</script>
<head>
</head>
<body>
<%
String val = (String)session.getAttribute("adminOk");
if(val != null) {
%>
이미 로그인 했습니다.<br><br>
[<a href="adminlogout.jsp">로그아웃</a>]&[<a href="javascript:window.close()">닫기</a>]
<%
} else {%>
<form name=frm method=post action="adminlogin.jsp">
<table width=80% height=80%>
<tr>
<td align=center>Admin id</td>
<td><input type=text name=id size=15></td>
</tr>
<tr>
<td align=center>password</td>
<td><input type=password name=pass size=15></td>
</tr>
</table><center>
[<a href="#" onclick="check()">로그인</a>]&
[<a href="javascript:window.close()">닫기</a>]
</center>
</form>
<% }%>
</body>
</html>

// adminlogin.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<html><body><center><p>
<%
String id = request.getParameter("id");
String pass = request.getParameter("pass");
if(id.equals("korea") && pass.equals("korea")) {
session.setAttribute("adminOk", "admin");
%>
<table>
<tr>
<td bgcolor="#efefef">로그인 성공</td>
</tr>
<tr>
<td><a href="#" onclick="window.close()">닫기</a></td>
</tr></table>
<%
} else {
%>
<table>
<tr><td bgcolor="#efefef">로그인 실패</td>
<tr>
<td><a href="#" onclick="window.close()">닫기</a></td>
</tr>
</table>
<% } %>
</body>
</html>

// adminlogout.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
session.removeAttribute("adminOk");
%>
<html>
<body><center>
로그 아웃 성공<p>
[<a href="#" onClick="window.close()">닫기</a>
</p></center>
</body>
</html>

// 이제 게시판 부분은 끝난것 같네요 오츠까레 :)