Web Programming/Project

[게시판 만들기] 1-5. JSP - Oracle 을 이용한 Model1 입니다.

유가엘 2020. 3. 26. 12:00

lesson13_JSP_Board_MVC.zip
0.01MB

상세 페이지를 만들기 위해서 content.jsp를 생성합니다.

list.jsp에서 content.jsp 로 이동하기 위한 <a href="></a> 태그를 생성합니다. 해당 content.jsp 페이지에는 선택한 값의 의 상세화면을 출력해야 하므로, 키값을 가져가야합니다.

 

GET방식은 URL 창에 localhost:Port?param=Value1&param=Value2 로 값이 전달됩니다.

list.jsp

<body>
	<h4>MVC1 게시판</h4>
	
	<table border="1">
		<thead>
			<tr>
				<th>번호</th>
				<th>제목</th>
				<th>작성자</th>
				<th>날짜</th>
				<th>조회수</th>
			</tr>
		</thead>
		<tbody>
<%
			while (result.next()) {
				out.print("<tr>");
				out.print("<td>"+result.getString("BOARD_IDX")+"</td>");
				out.print("<td><a href='content.jsp?BOARD_IDX="+result.getString("BOARD_IDX")+"'>"+result.getString("BOARD_TITLE")+"</a></td>");
				out.print("<td>"+result.getString("BOARD_WRITER")+"</td>");
				out.print("<td>"+result.getString("BOARD_WRITEDATE")+"</td>");
				out.print("<td>"+result.getString("BOARD_COUNT")+"</td>");
				out.print("</tr>");
			}
%>
		</tbody>
	</table>
    </body>

전달받은 BOARD_IDX 를 Select 조건에 넣어 content.jsp 에 출력합니다.

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<%
		request.setCharacterEncoding("EUC-KR"); 
		String BOARD_IDX = request.getParameter("BOARD_IDX");

		String driver="oracle.jdbc.driver.OracleDriver";
		String url="jdbc:oracle:thin:@localhost:1521:orcl";
		String id = "tutorial";
		String pw = "12341234";
		
		Connection con = null;
		Statement stmt = null;
		ResultSet result = null;
		
		try {
			Class.forName(driver);
			con=DriverManager.getConnection(url,id,pw);
			stmt=con.createStatement();
			String sql = "SELECT * FROM BOARD WHERE BOARD_IDX ="+BOARD_IDX;
			result = stmt.executeQuery(sql);
			
			while (result.next()) {
%>
<body>
	<h4>컨텐츠 페이지입니다.</h4>
	
	<table border="1">
		<thead>
			<tr>
				<th>번호</th>
				<th>제목</th>
				<th>작성자</th>
				<th>날짜</th>
				<th>조회수</th>
			</tr>
		</thead>
		<tbody>
<%
				out.print("<tr>");
				out.print("<td>"+result.getString("BOARD_IDX")+"</td>");
				out.print("<td>"+result.getString("BOARD_TITLE")+"</td>");
				out.print("<td>"+result.getString("BOARD_WRITER")+"</td>");
				out.print("<td>"+result.getString("BOARD_WRITEDATE")+"</td>");
				out.print("<td>"+result.getString("BOARD_COUNT")+"</td>");
				out.print("</tr>");
				out.print("<tr>");
				out.print("<td colspan='5'>"+result.getString("BOARD_CONTENT")+"</td>");
				out.print("</tr>");
 %>		
		</tbody>
	</table>
	
	<a href="../index.jsp">목록으로 이동</a>
	<a href="../delete.jsp?BOARD_IDX=+<%=result.getString("BOARD_IDX") %>">삭제</a>
<%
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

%>
</body>
</html>

delete.jsp 페이지를 생성하여 BOARD_IDX 기준 삭제 후, index.jsp로 반환합니다.

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<%
		request.setCharacterEncoding("EUC-KR"); 
		String BOARD_IDX = request.getParameter("BOARD_IDX");

		String driver="oracle.jdbc.driver.OracleDriver";
		String url="jdbc:oracle:thin:@localhost:1521:orcl";
		String id = "tutorial";
		String pw = "12341234";
		
		Connection con = null;
		Statement stmt = null;
		
		try {
			Class.forName(driver);
			con=DriverManager.getConnection(url,id,pw);
			stmt=con.createStatement();
			String sql = "DELETE FROM BOARD WHERE BOARD_IDX ="+BOARD_IDX;
			int result = stmt.executeUpdate(sql);
			
			if (result==1) {
				out.print("DELETE SUCCESS");
			}else {
				out.print("DELETE FAIL");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.println(e.getMessage());
		}finally {
			if (stmt!=null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					out.println(e.getMessage());
				}
			}
			if (con!=null) {
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					out.println(e.getMessage());
				}
			}
			out.print("<script>location.href='index.jsp';</script>");
		}
%>
<body>

</body>
</html>

MVC1은 화면을 보여주는 코드와 비즈니스 로직 소스가 섞여 있어 재 사용과 가독성이 떨어집니다.

이를 해결하기 위해 MVC2 모델이 도입되었습니다.