[필기정리]Day70 - jsp 게시판 만들기 예제

Web/JSP

2020. 10. 5. 23:38

- list.jsp

<%@ page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="HTML Study">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="coderbear">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>게시판 목록</title>
</head>
<body>
<h3>목록</h3>
<table>
	<tr>
		<td>번호</td><td>제목</td><td>작성자</td><td>작성일</td><td>조회수</td>
	</tr>
<%
	String driver = "com.mysql.cj.jdbc.Driver";
	String url = "jdbc:mysql://localhost:3306/project?useSSL=false&serverTimezone=UTC";
	String mysqlId = "root";
	String mysqlPw = "1234";
	String sql = "Select no, title, name, wTime, rCnt, email from BOARD ORDER BY no DESC";
	Connection conn = null;
	ResultSet rs = null;
	PreparedStatement pstmt = null;
	try{
		Class.forName(driver);
		conn = DriverManager.getConnection(url, mysqlId, mysqlPw);
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();
		while(rs.next())
		{
%>
			<tr>
				<td><%=rs.getInt("no") %></td>
				<td><a href="view.jsp?no=<%=rs.getInt("no")%>"><%=rs.getString("title") %></a></td>
				<td><a href="mailto:<%=rs.getString("email")%>"><%=rs.getString("name")%></a></td>
				<td><%=rs.getString("wTime")%></td>
				<td><%=rs.getString("rCnt")%></td>
			</tr>
<%			
		}
	} catch(ClassNotFoundException e){
		System.out.println("드라이버 로드 실패");
	} catch(SQLException e){
		e.printStackTrace();
	} finally{
		try{
			if(rs != null) rs.close();
			if(pstmt != null) pstmt.close();
			if(conn != null) conn.close();
		} catch(SQLException e){
			e.printStackTrace();
		}		
	}	
%>
</table>
<div><a href="write.jsp">글쓰기</a></div>
</body>
</html>

 

- write.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="HTML Study">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="coderbear">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>게시판 글쓰기</title>
</head>
<body>
	<form method="post" action="writeOK.jsp">
		<table>
			<caption>글 쓰 기</caption>
			<tr>
				<td><label for="title">제목</label></td><td><input type="text" name="title" id="title" size="40" required></td>
			</tr>
			<tr>
				<td><label for="name">이름</label></td><td><input type="text" name="name" id="name" size="40"  required></td>
			</tr>
			<tr>
				<td><label for="pw">비밀번호</label></td><td><input type="password" name="pw" id="pw" size="40"  required></td>
			</tr>
			<tr>
				<td><label for="email">E-mail</label></td><td><input type="email" name="email" id="email" size="40"  required></td>
			</tr>
			<tr>
				<td colspan="2"><textarea cols="50" rows="10" name="contents"  required></textarea></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="등록"> <a href="list.jsp"><input type="button" value="목록"></a></td>
			</tr>
		</table>
	</form>
</body>
</html>

 

- writeOK.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
	request.setCharacterEncoding("UTF-8");
	String title = request.getParameter("title");
	String name = request.getParameter("name");
	String pw = request.getParameter("pw");
	String email = request.getParameter("email");
	String contents = request.getParameter("contents");

	String driver = "com.mysql.cj.jdbc.Driver";
	String url = "jdbc:mysql://localhost:3306/project?useSSL=false&serverTimezone=UTC";
	String mysqlId = "root";
	String mysqlPw = "1234";
	Connection conn = null;
	PreparedStatement pstmt = null;
	String sql = "INSERT INTO board(title, name, password, email, contents) values(?, ?, ?, ?, ?)";
	int result = 0;
	
	try{
		Class.forName(driver);
		conn = DriverManager.getConnection(url, mysqlId, mysqlPw);
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, title);
		pstmt.setString(2, name);
		pstmt.setString(3, pw);
		pstmt.setString(4, email);
		pstmt.setString(5, contents);
		result = pstmt.executeUpdate(); // INSERT, DELETE, UPDATE는 executeUpdate() 사용 , 해당 결과의 갯수 반환
		if(result > 0) response.sendRedirect("list.jsp");
		else response.sendRedirect("error.jsp");
	} catch(ClassNotFoundException e){
		System.out.println("드라이버 로드 실패");
	} catch(SQLException e){
		e.printStackTrace();
	} finally{
		try{
			if(pstmt != null) pstmt.close();
			if(conn != null) conn.close();
		} catch(SQLException e){
			e.printStackTrace();
		}		
	}	
%>

 

- error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="HTML Study">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="coderbear">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
</head>
<body>
에러
</body>
</html>

 

- view.jsp

<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="HTML Study">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="coderbear">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
<link rel="stylesheet" href="css/view.css">
</head>
<body>
<table>
	<caption>내용보기</caption>
<%
	String no = request.getParameter("no");
	Connection conn = null;
	Statement stmt = null;
	String driver = "com.mysql.cj.jdbc.Driver";
	String url = "jdbc:mysql://localhost:3306/project?useSSL=false&serverTimezone=UTC";
	String mysqlId = "root";
	String mysqlPw = "1234";
	String pw = null;
	String sql = "SELECT title, name, wTime, contents, rCnt, password FROM board where no =" + no;
	ResultSet rs = null;
	int rCnt = 0;
	
	try{
		Class.forName(driver);
		conn = DriverManager.getConnection(url, mysqlId, mysqlPw);
		stmt = conn.createStatement(); // DB 연결 정보를 가지고 Statement 객체 생성
		rs = stmt.executeQuery(sql);
		while(rs.next()){
			rCnt = rs.getInt("rCnt");
			pw = rs.getString("password");
%>
	<tr>
		<td>제목</td><td><%=rs.getString("title") %></td>
	</tr>
	<tr>
		<td>이름</td><td><%=rs.getString("name") %></td>
	</tr>
	<tr>
		<td>작성일</td><td><%=rs.getString("wTime") %></td>
	</tr>
	<tr>
		<td colspan="2"><textarea name="contents" cols="50" rows="10" style="resize: none;" readonly><%=rs.getString("contents") %></textarea></td>
	</tr>
	
<%	
		}
		sql = "UPDATE board SET rCnt = " + (rCnt + 1) + " WHERE no = " + no;
		stmt.executeUpdate(sql);
	} catch(ClassNotFoundException e){
		System.out.println("드라이버 로드 실패");
	} catch(SQLException e){
		e.printStackTrace();
	} finally{
		try{
			if(rs != null) rs.close();
			if(stmt != null) stmt.close();
			if(conn != null) conn.close();
		} catch(SQLException e){
			e.printStackTrace();
		}		
	}		
%>		
</table>
<a href="list.jsp"><input type="button" value="목록" class="formbtn"></a> 
<form method="post" action="inputPassword.jsp">
<input type="hidden" name="no" value="<%=no%>">
<input type="hidden" name="pw" value="<%=pw%>">
<input type="hidden" name="mode" value="2">
<input type="submit" value="수정"  class="formbtn">
</form>
<form method="post" action="inputPassword.jsp">
<input type="hidden" name="no" value="<%=no%>">
<input type="hidden" name="pw" value="<%=pw%>">
<input type="hidden" name="mode" value="1">
<input type="submit" value="삭제" class="formbtn">
</form>
<%-- <a href="inputPassword.jsp?no=<%=no%>&pw=<%=pw%>&mode=1">삭제</a> --%>
</body>
</html>

 

- modify.jsp

<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="HTML Study">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="coderbear">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
<link rel="stylesheet" href="css/view.css">
</head>
<body>
<form method="post" action="modifyOK.jsp">
<table>
	<caption>수정하기</caption>
<%
	String no = request.getParameter("no");
	Connection conn = null;
	Statement stmt = null;
	PreparedStatement pstmt = null;
	String driver = "com.mysql.cj.jdbc.Driver";
	String url = "jdbc:mysql://localhost:3306/project?useSSL=false&serverTimezone=UTC";
	String mysqlId = "root";
	String mysqlPw = "1234";
	String pw = null;
	String sql = "SELECT title, name, wTime, contents, rCnt, password FROM board where no =" + no;
	ResultSet rs = null;
	int rCnt = 0;
	
	try{
		Class.forName(driver);
		conn = DriverManager.getConnection(url, mysqlId, mysqlPw);
		stmt = conn.createStatement();
		rs = stmt.executeQuery(sql);
		if(rs.next()){
			rCnt = rs.getInt("rCnt");
			pw = rs.getString("password");
%>
	<tr>
		<td>제목</td><td><input type="text" name="title" value="<%=rs.getString("title") %>"></td>
	</tr>
	<tr>
		<td>이름</td><td><%=rs.getString("name") %></td>
	</tr>
	<tr>
		<td>작성일</td><td><%=rs.getString("wTime") %></td>
	</tr>
	<tr>
		<td colspan="2"><textarea name="contents" cols="50" rows="10" style="resize: none;"><%=rs.getString("contents") %></textarea></td>
	</tr>
	
<%	
		}
	} catch(ClassNotFoundException e){
		System.out.println("드라이버 로드 실패");
	} catch(SQLException e){
		e.printStackTrace();
	} finally{
		try{
			if(rs != null) rs.close();
			if(stmt != null) stmt.close();
			if(conn != null) conn.close();
		} catch(SQLException e){
			e.printStackTrace();
		}		
	}		
%>		
</table>
<input type="hidden" name="no" value="<%=no %>">
<a href="list.jsp"><input type="button" value="목록"></a> 
<input type="submit" value="등록">
</form>
</body>
</html>

 

- modifyOK.jsp

<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
	request.setCharacterEncoding("UTF-8");
	String no = request.getParameter("no");
	String title = request.getParameter("title");
	String contents = request.getParameter("contents");
	String driver = "com.mysql.cj.jdbc.Driver";
	String url = "jdbc:mysql://localhost:3306/project?useSSL=false&serverTimezone=UTC";
	String mysqlId = "root";
	String mysqlPw = "1234";
	String sql = "UPDATE board SET title=?, contents=? WHERE no = ?";
	Connection conn = null;
	ResultSet rs = null;
	PreparedStatement pstmt = null;
	int result = 0;
	
	try{
		Class.forName(driver);
		conn = DriverManager.getConnection(url, mysqlId, mysqlPw);
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, title);
		pstmt.setString(2, contents);
		pstmt.setInt(3, Integer.parseInt(no));
		result = pstmt.executeUpdate();
		if(result > 0) response.sendRedirect("list.jsp");
		else response.sendRedirect("error.jsp");
	} catch(ClassNotFoundException e){
		e.printStackTrace();
	} catch(SQLException e){
		e.printStackTrace();
	} finally{
		try{
			if(pstmt != null) pstmt.close();
			if(conn != null) conn.close();			
		} catch(SQLException e){
			e.printStackTrace();
		}
	}
%>

 

- inputPassword.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="HTML Study">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="coderbear">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
</head>
<body>
	<form method="post" action="inputPasswordOK.jsp">
		<table>
			<tr>
				<td>비밀번호</td><td><input type="password" name="password"></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="확인"></td>
			</tr>			
		</table>
		<input type = "hidden" name="no" value="<%=request.getParameter("no") %>">
		<input type = "hidden" name="pw" value="<%=request.getParameter("pw") %>">
		<input type = "hidden" name="mode" value="<%=request.getParameter("mode") %>">
	</form>
</body>
</html>

 

- inputPasswordOK.jsp

<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
	String pw = request.getParameter("pw");
	String password = request.getParameter("password");
	String no = request.getParameter("no");
	String mode = request.getParameter("mode");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="HTML Study">
<meta name="keywords" content="HTML,CSS,XML,JavaScript">
<meta name="author" content="coderbear">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
<script>
	function goToLocation(no)
	{
		if(no==1){
			alert("데이터가 삭제 되었습니다");
			location.href = "list.jsp";			
		}
		else if(no==2){
			location.href = "modify.jsp?no=<%=no%>"; // no이 primary key이기 때문에 사용하는 것
		}
		else if(no==3){
			alert("비밀번호가 일치하지 않습니다.");
			history.go(-2);
		}
	}
</script>
</head>
<body>
<%	
	String driver = "com.mysql.cj.jdbc.Driver";
	String url = "jdbc:mysql://localhost:3306/project?useSSL=false&serverTimezone=UTC";
	String mysqlId = "root";
	String mysqlPw = "1234";
	Connection conn = null;
	Statement stmt = null;
	String sql = null;
	try{
		Class.forName(driver);
		conn = DriverManager.getConnection(url, mysqlId, mysqlPw);
		stmt = conn.createStatement();
		
		if(pw.matches(password))
		{
			if(mode.matches("1"))
			{
				sql = "DELETE FROM board where no = " + no;
				stmt.executeUpdate(sql);
%>
				<script>goToLocation(1)</script>
<%		
		
			}
			else if(mode.matches("2"))
			{
%>
				<script>goToLocation(2)</script>
<%
			}
		}
		else
		{
%>
				<script>goToLocation(3)</script>		
<%
		}
	
	} catch(ClassNotFoundException e){
		System.out.println("드라이버 로드 실패");
	} catch(SQLException e){
		e.printStackTrace();
	} finally{
		try{
			if(stmt != null) stmt.close();
			if(conn != null) stmt.close();			
		} catch(SQLException e){
			e.printStackTrace();
		}
	}
%>
</body>
</html>

 

728x90