반응형
noticeView.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="jspservlet.vo.*" %>
<%
request.setCharacterEncoding("UTF-8");
NoticeBoardVO board = (NoticeBoardVO)request.getAttribute("board");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>공지사항 상세페이지</title>
</head>
<body>
<h2>공지사항 상세페이지</h2><hr>
제목 : <span><%= board.getTitle() %></span><br>
글번호 : <span><%= board.getNno() %></span><br>
작성자 : <span><%= board.getUid() %></span><br>
상태 : <span><%= board.getState() %></span><br>
작성일 : <span><%= board.getRdate() %></span><br>
내용 : <span><%= board.getContent() %></span><br>
<!-- 수정하기 버튼 클릭시 수정화면 출력 후 수정화면에서
제목,내용,상태 값 변경 후 저장 버튼 클릭시 실제 notice_board 테이블의 데이터가
저장될 수 있도록 기능을 완성
수정페이지 가상 경로 : /notice/modify.do
수정처리페이지 가상 경로 : /notice/modify.do
수정처리 후 다시 상세페이지로 이동합니다
-->
<button onclick="location.href='<%= request.getContextPath() %>/notice/modify.do?nno=<%= board.getNno() %>';">수정하기</button>
</body>
</html>
modify.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="jspservlet.vo.*" %>
<%
NoticeBoardVO board = (NoticeBoardVO)request.getAttribute("board");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>공지사항 수정페이지</title>
</head>
<body>
<h2>공지사항 수정페이지</h2><hr>
<form action="modify.do" method="post">
<input type="hidden" name="nno" value="<%= board.getNno() %>">
제목 :
<span>
<input type="text" name="title" size="50" value="<%= board.getTitle() %>">
</span><br>
글번호 : <span><%= board.getNno() %></span><br>
작성자 : <span><%= board.getUid() %></span><br>
상태 :
<span>
<input type="radio" name="state" value="E" id="E" <%= board.getState().equals("E") ? "checked" : "" %>>
<label for="E">활성</label>
<input type="radio" name="state" value="D" id="D" <%= board.getState().equals("D") ? "checked" : "" %>>
<label for="D">비활성</label>
</span><br>
작성일 : <span><%= board.getRdate() %></span><br>
내용 :
<span>
<textarea name="content" rows="10" cols="50"><%= board.getContent() %></textarea>
</span><br>
<button>저장</button>
</form>
</body>
</html>
NoticeModifyController.java
package jspservlet.controller;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jspservlet.util.DBConn;
import jspservlet.vo.NoticeBoardVO;
/**
* Servlet implementation class NoticeModifyController
*/
@WebServlet("/notice/modify.do")
public class NoticeModifyController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public NoticeModifyController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String nno = request.getParameter("nno");
Connection conn = null; //DB 연결
PreparedStatement psmt = null; //SQL 등록 및 실행. 보안이 더 좋음!
ResultSet rs = null; //조회 결과를 담음
//try 영역
try{
conn = DBConn.conn();
String sql = "select n.*,uid from notice_board n "
+ " inner join user u "
+ " on n.uno = u.uno "
+ " where nno=?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, nno);
rs = psmt.executeQuery();
if(rs.next()){
NoticeBoardVO board = new NoticeBoardVO();
board.setNno(nno);
board.setTitle(rs.getString("title"));
board.setUid(rs.getString("uid"));
board.setState(rs.getString("state"));
board.setRdate(rs.getString("rdate"));
board.setContent(rs.getString("content"));
request.setAttribute("board", board);
request.getRequestDispatcher("/notice/modify.jsp").forward(request, response);
}else {
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
DBConn.close(rs, psmt, conn);
}catch(Exception e) {
e.printStackTrace();
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
request.setCharacterEncoding("UTF-8");
String nno = request.getParameter("nno");
String title = request.getParameter("title");
String content = request.getParameter("content");
String state = request.getParameter("state");
try{
conn = DBConn.conn();
String sql = "update notice_board "
+ " set "
+ " title=?, "
+ " content=?, "
+ " state=? "
+ " where nno=?";
psmt = conn.prepareStatement(sql); //사용할 쿼리 등록
psmt.setString(1, title);
psmt.setString(2, content);
psmt.setString(3, state);
psmt.setString(4, nno);
int result = psmt.executeUpdate();
response.sendRedirect(request.getContextPath()+"/board/view.do?nno="+nno);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
DBConn.close(rs, psmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
반응형
'Java' 카테고리의 다른 글
[Servlet] Front Controller (0) | 2024.10.19 |
---|---|
[Servlet] 게시글 삭제 (0) | 2024.10.14 |
[Servlet] 공지게시글 상세페이지 조회 (0) | 2024.10.12 |
[Servlet] 공지 게시글 목록 조회하기 (0) | 2024.10.11 |
[Servlet] 로그인한 회원의 회원 정보 조회 (0) | 2024.10.10 |