JSP

[ KH 정보교육원 ] 자바 프레임워크 개발자 양성과정 - 게시판 만들기(2)

코드사냥꾼 2019. 11. 27. 20:54

** KH정보교육원에서 실제로 수강한 내용을 개인적으로 정리한 것입니다. **

서블릿을 사용하지 않고 데이터베이스와 jsp 파일만을 이용하여 만들어 보았다. 이 단계에서는 조회(select) 기능과 삽입(insert) 기능을 구현하는 방법을 알아본다.

1. selectOne.jsp : 조회기능

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
%>
<%
	response.setContentType("text/html; charset=UTF-8");
%>
<%@ page import="com.dao.MyBoardDao"%>
<%@ page import="com.vo.MyBoard"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		int myno = Integer.parseInt(request.getParameter("myno"));
		MyBoardDao dao = new MyBoardDao();
		MyBoard board = dao.selectOne(myno);
	%>

	<table>
		<tr>
			<th>이름 :</th>
			<td><%=board.getMyname()%></td>
		</tr>
		<tr>
			<th>제목 :</th>
			<td><%=board.getMytitle()%></td>
		</tr>
		<tr>
			<th>내용</th>
			<td><textarea rows="10" cols="60" readonly="readonly"><%=board.getMycontent()%></textarea></td>
		</tr>
		<tr>
			<td colspan="2">
			    <button onclick="location.href='myupdate.jsp?myno=<%=board.getMyno()%>'">수정</button>
			    <button onclick="location.href='mydelete.jsp?myno=<%=board.getMyno()%>'">삭제</button>
				<button onclick="location.href='mylist.jsp'">목록</button>&nbsp;
			</td>
		</tr>
	</table>
</body>
</html>

2. myinsert.jsp : 삽입 할 데이터를 입력할 수 있는 페이지 구현 파일(form)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
%>
<%
	response.setContentType("text/html; charset=UTF-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>글쓰기</h1>
	<form action="myinsert_res.jsp" method="post">
		<table>
			<tr>
				<th>이름 :</th>
				<td><input type="text" name="myname"></td>
			</tr>
			<tr>
				<th>제목 :</th>
				<td><input type="text" name="mytitle"></td>
			</tr>
			<tr>
				<th>내용 :</th>
				<td><textarea rows="10" cols="40" name="mycontent"></textarea></td>
			</tr>
			<tr>
				<td colspan="2" align="right">
					<input type="submit" value="입력">
					<input type="button" value="취소" onclick="location.href='mylist.jsp'">
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

3. myinsert_res.jsp : 삽입 시 데이터 처리 후 값을 나타내는 기능

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
%>
<%
	response.setContentType("text/html; charset=UTF-8");
%>
<%@ page import="com.dao.MyBoardDao"%>
<%@ page import="com.vo.MyBoard"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		String myname = request.getParameter("myname");
		String mytitle = request.getParameter("mytitle");
		String mycontent = request.getParameter("mycontent");

		MyBoard vo = new MyBoard(myname, mytitle, mycontent);
		MyBoardDao dao = new MyBoardDao();

		// int res 담는 이유 : 결과값이 적용된 row의 수만큼 증가하기때문에 일단 int로 받아주는 것임
		int res = dao.insert(vo);

		if (res > 0) { // 추가 된 사항이 있다면
	%>

	<script type="text/javascript">
		alert("글 등록이 성공했습니다.");
		location.href = "mylist.jsp";
	</script>

	<%
		} else {
	%>

	<script type="text/javascript">
		alert("글 등록에 실패했습니다. 다시 입력해주세요.");
		location.href = "myinsert.jsp";
	</script>

	<%
		}
	%>

	<table>
		<tr>
			<th>이름</th>
			<td><%=myname%></td>
		</tr>
		<tr>
			<th>제목</th>
			<td><%=mytitle%></td>
		</tr>
		<tr>
			<th>내용 :</th>
			<td><textarea rows="10" cols="40" name="mycontent"><%=mycontent%></textarea></td>
		</tr>
	</table>
</body>
</html>