JSP day15
JSP로 게시판 만들기(5)
이번 시간에는 글 상세보기 페이지의 데이터를 수정하는 기능을 추가해보자.
view.jsp 일부
<%
if(b_userid.equals(userid)){
%>
<input type="button" value="수정" onclick="location.href='./edit.jsp?b_idx=<%=b_idx%>'">
<input type="button" value="삭제" onclick="location.href='./delete.jsp?b_idx=<%=b_idx%>'">
<%
}
%>
수정버튼을 클릭 시 글 번호를 URL에 담아 edit.jsp로 보내진다.
edit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.koreait.db.Dbconn"%>
<%
request.setCharacterEncoding("UTF-8");
String userid = (String)session.getAttribute("userid");
if(userid == null){
%>
<script>
alert('로그인 후 이용하세요');
location.href='../login.jsp';
</script>
<%
}else{
String b_idx = request.getParameter("b_idx");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String b_title = "";
String b_content = "";
String sql = "";
try{
conn = Dbconn.getConnection();
if(conn != null){
sql = "select b_title, b_content from tb_board where b_idx=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, b_idx);
rs = pstmt.executeQuery();
if(rs.next()){
b_title = rs.getString("b_title");
b_content = rs.getString("b_content");
}
}
}catch (Exception e) {
e.printStackTrace();
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>커뮤니티 - 글수정</title>
</head>
<body>
<h2>커뮤니티 - 글수정</h2>
<form method="post" action="./edit_ok.jsp">
<input type="hidden" name="b_idx" value="<%=b_idx%>">
<p>작성자 : <%=userid%></p>
<p>제목<input type="text" name="b_title" value="<%=b_title%>"></p>
<p>내용</p>
<p><textarea name="b_content" rows="5" column="40"><%=b_content%></textarea></p>
<p><input type="submit" value="수정"> <input type="reset" value="다시작성"> <input type="button" value="리스트" onclick="location.href='list.jsp'"></p>
</form>
</body>
</html>
<%
}
%>
이 페이지에서는 수정 페이지로 기존의 데이터를 출력해주고 그 위에 수정하는 방식으로 진행 할 것이다. 글번호를 request.getParameter로 받아와서 글번호와 맞는 데이터를 받아온다. 데이터를 먼저 html에 출력해준다.
이런 식으로 수정할 글에 대해 보여주고 수정을 완료하기 위해 수정 버튼을 클릭하면 form 태그에 POST방식으로 edit_ok.jsp로 데이터를 보내게된다.
edit_ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.koreait.db.Dbconn"%>
<%
request.setCharacterEncoding("UTF-8");
String userid = (String)session.getAttribute("userid");
if(userid == null){
%>
<script>
alert('로그인 후 이용하세요');
location.href='../login.jsp';
</script>
<%
}else{
String b_idx = request.getParameter("b_idx");
String b_title = request.getParameter("b_title");
String b_content = request.getParameter("b_content");
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "";
try{
conn = Dbconn.getConnection();
if(conn != null){
sql = "update tb_board set b_title = ?, b_content = ? where b_idx=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, b_title);
pstmt.setString(2, b_content);
pstmt.setString(3, b_idx);
pstmt.executeUpdate();
}
}catch (Exception e) {
e.printStackTrace();
}
%>
<script>
alert('게시글이 수정되었습니다.');
location.href='./view.jsp?b_idx=<%=b_idx%>';
</script>
<%
}
%>
edit.jsp에서 받은 데이터들은 update SQL문을 통해 갱신되고 다시 글 상세보기 페이지로 이동하게된다. 이때 글 수정이 완료된 것을 확인 할 수 있다.
이번 시간을 마지막으로 순수 JSP를 활용한 회원가입, 로그인, 리스트 만드는 기능구현이 마무리되었다.