반응형
view.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="frontControllerPJT.vo.*" %>
<%
request.setCharacterEncoding("UTF-8");
NoticeBoardVO vo = (NoticeBoardVO)request.getAttribute("vo");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>공지사항 상세페이지</title>
</head>
<style>
button{
background-color:white;
border: 1px solid royalblue;
color: royalblue;
width:100px;
height:30px;
cursor:pointer;
}
</style>
<body>
<h2>공지사항 상세페이지</h2><hr>
<a href="<%= request.getContextPath() %>/board/main.do">
<button>글목록</button>
</a><br>
글번호 : <span><%= vo.getNno() %></span><br>
제목 : <span><%= vo.getTitle() %></span><br>
작성자 : <span><%= vo.getUid() %></span><br>
작성일 : <span><%= vo.getRdate() %></span><br>
상태 : <span><%= vo.getState() %></span><br>
조회수 : <span><%= vo.getHit() %></span><br>
내용 : <span><%= vo.getContent() %></span><br>
<button onclick="location.href='modify.do?nno=<%= vo.getNno() %>';">수정하기</button>
<button onclick="document.dFn.submit();">삭제하기</button>
<form name="dFn" action="delete.do" method="post">
<input type="hidden" name="nno" value="<%= vo.getNno() %>">
</form>
</body>
</html>
modify.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="frontControllerPJT.vo.*" %>
<%
NoticeBoardVO vo = (NoticeBoardVO)request.getAttribute("vo");
%>
<!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="<%= vo.getNno() %>">
제목 :
<span>
<input type="text" name="title" size="50" value="<%= vo.getTitle() %>">
</span><br>
글번호 : <span><%= vo.getNno() %></span><br>
작성자 : <span><%= vo.getUid() %></span><br>
상태 :
<span>
<input type="radio" name="state" value="E" id="E" <%= vo.getState().equals("E") ? "checked" : "" %>>
<label for="E">활성</label>
<input type="radio" name="state" value="D" id="D" <%= vo.getState().equals("D") ? "checked" : "" %>>
<label for="D">비활성</label>
</span><br>
작성일 : <span><%= vo.getRdate() %></span><br>
내용 :
<span>
<textarea name="content" rows="10" cols="50"><%= vo.getContent() %></textarea>
</span><br>
<button>저장</button>
</form>
</body>
</html>
NoticeController
package frontControllerPJT.controller;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import frontControllerPJT.util.DBConn;
import frontControllerPJT.vo.NoticeBoardVO;
public class NoticeController {
public NoticeController(HttpServletRequest request
, HttpServletResponse response
, String[] comments) throws ServletException, IOException {
if(comments[comments.length-1].equals("view.do")) {
view(request,response);
}else if(comments[comments.length-1].equals("modify.do")) {
if(request.getMethod().equals("GET")) {
//수정페이지를 요청할때
modify(request,response);
}else if(request.getMethod().equals("POST")) {
//수정페이지에서 데이터 수정 후 서브밋했을 때
modifyOk(request,response);
}
}else if(comments[comments.length-1].equals("delete.do")) {
delete(request,response);
}
}
public void view(HttpServletRequest request
, HttpServletResponse response) {
String nno = request.getParameter("nno");
Connection conn = null; //DB 연결
PreparedStatement psmt = null; //SQL 등록 및 실행. 보안이 더 좋음!
ResultSet rs = null; //조회 결과를 담음
PreparedStatement psmtHit = 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 vo = new NoticeBoardVO();
vo.setNno(nno);
vo.setTitle(rs.getString("title"));
vo.setUid(rs.getString("uid"));
vo.setState(rs.getString("state"));
vo.setRdate(rs.getString("rdate"));
vo.setContent(rs.getString("content"));
//조회수 증가
int hit = rs.getInt("hit");
String sqlHit = "update notice_board set hit = hit + 1 where nno = ?";
hit++;
psmtHit = conn.prepareStatement(sqlHit);
psmtHit.setString(1, nno);
psmtHit.executeUpdate();
vo.setHit(hit+"");
request.setAttribute("vo", vo);
request.getRequestDispatcher("/WEB-INF/notice/view.jsp").forward(request, response);
}else {
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
DBConn.close(psmtHit, null);
DBConn.close(rs, psmt, conn);
}catch(Exception e) {
e.printStackTrace();
}
}
}
public void modify(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 vo = new NoticeBoardVO();
vo.setNno(nno);
vo.setTitle(rs.getString("title"));
vo.setUid(rs.getString("uid"));
vo.setState(rs.getString("state"));
vo.setRdate(rs.getString("rdate"));
vo.setContent(rs.getString("content"));
request.setAttribute("vo", vo);
request.getRequestDispatcher("/WEB-INF/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();
}
}
}
public void modifyOk(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);
psmt.executeUpdate();
response.sendRedirect(request.getContextPath()+"/notice/view.do?nno="+nno);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
DBConn.close(rs, psmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void delete(HttpServletRequest request
, HttpServletResponse response) throws ServletException, IOException {
String nno = request.getParameter("nno");
Connection conn = null; //DB 연결
PreparedStatement psmt = null; //SQL 등록 및 실행. 보안이 더 좋음!
//try 영역
try{
conn = DBConn.conn();
/* String sql = "delete from notice_board where nno=?"; */
String sql = "update notice_board set state='D' where nno=?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, nno);
psmt.executeUpdate();
response.sendRedirect(request.getContextPath()+"/board/main.do");
}catch(Exception e){
e.printStackTrace();
}finally{
try {
DBConn.close(psmt, conn);
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
반응형
'Java' 카테고리의 다른 글
[JSP] EL & JSTL (0) | 2024.11.26 |
---|---|
[Servlet] FrontController 회원 로그인, 내정보조회 등 (0) | 2024.10.26 |
[Servlet] FrontController 게시글 상세 조회 (0) | 2024.10.24 |
[Servlet] FrontController 글 목록 조회 (0) | 2024.10.20 |
[Servlet] Front Controller (0) | 2024.10.19 |