본문 바로가기
개발 공부 Today I Learned

[국비 36일차 TIL] 게시판 만들기 (Delete, Util. update)

by 개발자신입 2024. 1. 11.
반응형

 

Delete.java

package com.coffee.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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 com.coffee.dao.BoardDAO;
import com.coffee.util.Util;

@WebServlet("/delete")
public class Delete extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Delete() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 20240111 글 삭제하기

		// no가 숫자인지 if문으로 확인 (숫자인 경우: 삭제 - board로 이동 / 숫자 아닌 경우: 에러 표시) 
	      if(Util.intCheck(request.getParameter("no"))) {
//	          System.out.println("숫자입니다.");
	    	  
	    	  // 파라미터로 오는 번호 잡기 -> 자바 변수로 저장 (여러개 보낼때는 &를 씀)
	    	  int no = Util.str2Int(request.getParameter("no"));
	    	  
	    	  // DAO에게 일 시키기
	    	  BoardDAO dao = new BoardDAO();
	    	  int result = dao.delete(no);
	    	  
	    	  // 잘 삭제되었는지 값 받기
	    	  System.out.println("삭제 여부 : " + result);
	    	  if(result == 1) {
	    		  // 값이 1이면 여기로
	    		  response.sendRedirect("./board");
	    	  } else {
	    		  // 값이 0이면 여기로
	    		  response.sendRedirect("./error.jsp");
	    	  }
	       } else {
//	          System.out.println("문자입니다.");
	    	  response.sendRedirect("./error.jsp");
	       }
	      
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}

 

Util.java

package com.coffee.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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 com.coffee.dao.BoardDAO;
import com.coffee.util.Util;

@WebServlet("/delete")
public class Delete extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Delete() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 20240111 글 삭제하기

		// no가 숫자인지 if문으로 확인 (숫자인 경우: 삭제 - board로 이동 / 숫자 아닌 경우: 에러 표시) 
	      if(Util.intCheck(request.getParameter("no"))) {
//	          System.out.println("숫자입니다.");
	    	  
	    	  // 파라미터로 오는 번호 잡기 -> 자바 변수로 저장 (여러개 보낼때는 &를 씀)
	    	  int no = Util.str2Int(request.getParameter("no"));
	    	  
	    	  // DAO에게 일 시키기
	    	  BoardDAO dao = new BoardDAO();
	    	  int result = dao.delete(no);
	    	  
	    	  // 잘 삭제되었는지 값 받기
	    	  System.out.println("삭제 여부 : " + result);
	    	  if(result == 1) {
	    		  // 값이 1이면 여기로
	    		  response.sendRedirect("./board");
	    	  } else {
	    		  // 값이 0이면 여기로
	    		  response.sendRedirect("./error.jsp");
	    	  }
	       } else {
//	          System.out.println("문자입니다.");
	    	  response.sendRedirect("./error.jsp");
	       }
	      
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}

 

Update.java

 

package com.coffee.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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 com.coffee.dao.BoardDAO;
import com.coffee.dto.BoardDTO;
import com.coffee.util.Util;


@WebServlet("/update")
public class Update extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Update() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// no 잡기
		int no = Util.str2Int(request.getParameter("no")); // 정수로 바꾸기
				
		// DAO 질의하기
		BoardDAO dao = new BoardDAO();
		BoardDTO dto = dao.detail(no);
		
		// jsp로 보내기
		request.setAttribute("update", dto);
		
		RequestDispatcher rd = request.getRequestDispatcher("update.jsp");
		rd.forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		if(request.getParameter("title") != null && request.getParameter("content") != null && Util.intCheck(request.getParameter("no"))){
			// 진짜 수정 (수정 후에 보드로 갈지, 수정한 글을 다시 확인할지)
			BoardDTO dto = new BoardDTO();
			dto.setContent(request.getParameter("title"));
			dto.setTitle(request.getParameter("content"));
			dto.setNo(Util.str2Int(request.getParameter("no")));
			
			BoardDAO dao = new BoardDAO();
			int result = dao.update(dto);
			
			response.sendRedirect("./detail?no=" + request.getParameter("no"));
		} else {
			response.sendRedirect("./error.jsp");
		}
		
//		String title = request.getParameter("title");
//		String content = request.getParameter("content");
//		String no = request.getParameter("no");
		
//		System.out.println("title");
//		System.out.println("content");
//		System.out.println("no");
	}

}

 

 

update.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글 수정 페이지입니다.</title>
<link href="./css/menu.css" rel="stylesheet">
<script type="text/javascript" src="./js/menu.js"></script>

<!-- include libraries(jQuery, bootstrap) -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<!-- include summernote css/js -->
<link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote.min.js"></script>

<style type="text/css">
#title{
	width: 100%;
	height: 30px;
	margin-bottom: 10px;
}

</style>

<script type="text/javascript"> // 제목,내용 없이 입력했을 때 경고창 팝업
	function check(){
		var title = document.getElementById("title");
		if (title.value.length < 5) {
			alert("제목은 5글자 이상이어야 합니다.");
			title.focus();
			return false;
		}
		

		var content = document.getElementsByClassName("content");
		if(content[0].value.length < 3) {
			alert("내용은 3글자 이상 작성해주세요.");
			content[0].focus();
			return false;
		}
	}
</script>


</head>
<body>
	<div class="container1">
		<header>
			<jsp:include page="menu.jsp"></jsp:include>
		</header>
		<div class="main">
			<div class="mainStyle">
				<article><br>
					<h1>글 수정하기</h1>
					<div class="writeFORM">
						<form action="./update" method="post">
							<input type="text" id="title" name="title" value="${update.title }"><br>
							<textarea id="summernote" name="content">${update.content }</textarea>
							<button type="submit">수정하기</button>
							<input type="hidden" name="no" value="${update.no }">
						</form>
					</div>
				</article>
        </div>
    </div>
</div>

<div id="summernote"></div>
<script>
	$(document).ready(function() {
		$('#summernote').summernote({
			height : 400
		});
});
  </script>
</body>
</html>
반응형

댓글