modify.jsp와 modify_ok.jsp 모두 수정을 해야합니다.
우선
기존의 modify.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <%@ page import="java.sql.*"%> <script language = "javascript"> // 자바 스크립트 시작 function modifyCheck() { var form = document.modifyform; if( !form.password.value ) { alert( "비밀번호를 적어주세요" ); form.password.focus(); return; } if( !form.title.value ) { alert( "제목을 적어주세요" ); form.title.focus(); return; } if( !form.memo.value ) { alert( "내용을 적어주세요" ); form.memo.focus(); return; } form.submit(); } </script> <% request.setCharacterEncoding("euc-kr"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:board2"; String id = ""; String pass = ""; String name = ""; String password = ""; String title = ""; String memo = ""; int idx = Integer.parseInt(request.getParameter("idx")); int pg = Integer.parseInt(request.getParameter("pg")); try { Connection conn = DriverManager.getConnection(url,id,pass); Statement stmt = conn.createStatement(); String sql = "SELECT USERNAME, PASSWORD, TITLE, MEMO FROM board1 WHERE NUM=" + idx; ResultSet rs = stmt.executeQuery(sql); if(rs.next()){ name = rs.getString(1); password = rs.getString(2); title = rs.getString(3); memo = rs.getString(4); } rs.close(); stmt.close(); conn.close(); }catch(SQLException e) { out.println( e.toString() ); } %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>게시판</title> </head> <body> <table> <form name=modifyform method=post action="modify_ok.jsp?idx=<%=idx%>&pg=<%=pg%>"> <tr> <td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr style="background:url('img/table_mid.gif') repeat-x; text-align:center;"> <td width="5"><img src="img/table_left.gif" width="5" height="30" /></td> <td>수정</td> <td width="5"><img src="img/table_right.gif" width="5" height="30" /></td> </tr> </table> <table> <tr> <td> </td> <td align="center">제목</td> <td><input type=text name=title size=50 maxlength=50 value="<%=title%>"></td> <td> </td> </tr> <tr height="1" bgcolor="#dddddd"><td colspan="4"></td></tr> <tr> <td> </td> <td align="center">이름</td> <td><%=name%><input type=hidden name=name size=50 maxlength=50 value="<%=name%>"></td> <td> </td> </tr> <tr height="1" bgcolor="#dddddd"><td colspan="4"></td></tr> <tr> <td> </td> <td align="center">비밀번호</td> <td><input type=password name="password" id="pass" size=50 maxlength=50 ></td> <td> </td> </tr> <tr height="1" bgcolor="#dddddd"><td colspan="4"></td></tr> <tr> <td> </td> <td align="center">내용</td> <td><textarea name=memo cols=50 rows=13><%=memo%></textarea></td> <td> </td> </tr> <tr height="1" bgcolor="#dddddd"><td colspan="4"></td></tr> <tr height="1" bgcolor="#82B5DF"><td colspan="4"></td></tr> <tr align="center"> <td> </td> <td colspan="2"><input type="button" value="수정" OnClick="javascript:modifyCheck();"> <input type=button value="취소" OnClick="javascript:history.back(-1)"> <td> </td> </tr> </table> </td> </tr> </form> </table> </body>
</html>
새로운 modify.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <%@ page import="board.*" %> <jsp:useBean id="dao" class="board.DAO"/> <jsp:useBean id="vo1" class="board.VO"/> <% int idx = Integer.parseInt(request.getParameter("idx")); int pg = Integer.parseInt(request.getParameter("pg")); VO vo = dao.getView(idx); %> <script language = "javascript"> // 자바 스크립트 시작 function modifyCheck() { var form = document.modifyform; if( !form.password.value ) { alert( "비밀번호를 적어주세요" ); form.password.focus(); return; } if( !form.title.value ) { alert( "제목을 적어주세요" ); form.title.focus(); return; } if( !form.memo.value ) { alert( "내용을 적어주세요" ); form.memo.focus(); return; } form.submit(); } </script> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>게시판</title> </head> <body> <table> <form name=modifyform method=post action="modify_ok.jsp?idx=<%=idx%>&pg=<%=pg%>"> <tr> <td> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr style="background:url('img/table_mid.gif') repeat-x; text-align:center;"> <td width="5"><img src="img/table_left.gif" width="5" height="30" /></td> <td>수정</td> <td width="5"><img src="img/table_right.gif" width="5" height="30" /></td> </tr> </table> <table> <tr> <td> </td> <td align="center">제목</td> <td><input type=text name=title size=50 maxlength=50 value="<%=vo.getTitle()%>"></td> <td> </td> </tr> <tr height="1" bgcolor="#dddddd"><td colspan="4"></td></tr> <tr> <td> </td> <td align="center">이름</td> <td><%=vo.getName()%><input type=hidden name=name size=50 maxlength=50 value="<%=vo.getName()%>"></td> <td> </td> </tr> <tr height="1" bgcolor="#dddddd"><td colspan="4"></td></tr> <tr> <td> </td> <td align="center">비밀번호</td> <td><input type=password name="password" id="pass" size=50 maxlength=50 ></td> <td> </td> </tr> <tr height="1" bgcolor="#dddddd"><td colspan="4"></td></tr> <tr> <td> </td> <td align="center">내용</td> <td><textarea name=memo cols=50 rows=13><%=vo.getMemo()%></textarea></td> <td> </td> </tr> <tr height="1" bgcolor="#dddddd"><td colspan="4"></td></tr> <tr height="1" bgcolor="#82B5DF"><td colspan="4"></td></tr> <tr align="center"> <td> </td> <td colspan="2"><input type="button" value="수정" OnClick="javascript:modifyCheck();"> <input type=button value="취소" OnClick="javascript:history.back(-1)"> <td> </td> </tr> </table> </td> </tr> </form> </table> </body>
</html>
그리고 이번엔 modify_ok.jsp
기존의 modify_ok.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <%@ page import="java.sql.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:board2"; String id = ""; String pass = ""; String password = ""; try{ request.setCharacterEncoding("euc-kr"); int idx = Integer.parseInt(request.getParameter("idx")); int pg = Integer.parseInt(request.getParameter("pg")); String title = request.getParameter("title"); String memo = request.getParameter("memo"); String passw = request.getParameter("password"); Connection conn = DriverManager.getConnection(url,id,pass); Statement stmt = conn.createStatement(); String sql = "SELECT PASSWORD FROM board1 WHERE NUM=" + idx; ResultSet rs = stmt.executeQuery(sql); if(rs.next()){ password = rs.getString(1); } if(password.equals(passw)) { sql = "UPDATE board1 SET TITLE='" + title+ "' ,MEMO='"+ memo +"' WHERE NUM=" + idx; stmt.executeUpdate(sql); %> <script language=javascript> self.window.alert("글이 수정되었습니다."); location.href="view.jsp?idx=<%=idx%>&pg=<%=pg%>"; </script> <% rs.close(); stmt.close(); conn.close(); } else { %> <script language=javascript> self.window.alert("비밀번호를 틀렸습니다."); location.href="javascript:history.back()"; </script> <% } } catch(SQLException e) { out.println( e.toString() ); }
%>
새로운 modify_ok.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <jsp:useBean id="dao" class="board.DAO"/> <jsp:useBean id="vo" class="board.VO"/> <jsp:setProperty name="vo" property="*" /> <% int idx = Integer.parseInt(request.getParameter("idx")); int pg = Integer.parseInt(request.getParameter("pg")); boolean ch = dao.checkPassword(vo, idx); if(ch) { dao.modify(vo, idx); %> <script language=javascript> self.window.alert("글이 수정되었습니다."); location.href="view.jsp?idx=<%=idx%>&pg=<%=pg%>"; </script> <% } else { %> <script language=javascript> self.window.alert("비밀번호를 틀렸습니다."); location.href="javascript:history.back()"; </script> <% }
%>
역시나 짧아졌습니다.
이미지출처 : 판다의 이상한 블로그(http://ssppmm.tistory.com/)
'Programming > JSP - 게시판만들기' 카테고리의 다른 글
[JSP] JSP로 게시판 만들기[MODEL 1] - 13(최종파일) (34) | 2013.01.25 |
---|---|
[JSP] JSP로 게시판 만들기[MODEL 1] - 12(답글달기) (2) | 2013.01.25 |
[JSP] JSP로 게시판 만들기[MODEL 1] - 10(글삭제) (3) | 2013.01.25 |
[JSP] JSP로 게시판 만들기[MODEL 1] - 8(내용보기) (0) | 2013.01.25 |
[JSP] JSP로 게시판 만들기[MODEL 1] - 7(글쓰기) (8) | 2013.01.25 |