개발/JSP

[jsp] 게시판 만들기

윤_ve 2021. 4. 19. 16:35

기본적인 게시판 만들기 

 

BoardBean.java

import java.sql.Timestamp;

public class BoardBean {
	private int b_id;
	private String b_name;
	private String b_email;
	private String b_title;
	private String b_content;
	private Timestamp b_date;
	private int b_hit;
	private String b_pwd;

	public String getB_pwd() {
		return b_pwd;
	}
	public void setB_pwd(String b_pwd) {
		this.b_pwd = b_pwd;
	}
	public int getB_hit() {
		return b_hit;
	}
	public void setB_hit(int b_hit) {
		this.b_hit = b_hit;
	}
	public Timestamp getB_date() {
		return b_date;
	}
	public void setB_date(Timestamp b_date) {
		this.b_date = b_date;
	}
	public int getB_id() {
		return b_id;
	}
	public void setB_id(int b_id) {
		this.b_id = b_id;
	}
	public String getB_name() {
		return b_name;
	}
	public void setB_name(String b_name) {
		this.b_name = b_name;
	}
	public String getB_email() {
		return b_email;
	}
	public void setB_email(String b_email) {
		this.b_email = b_email;
	}
	public String getB_title() {
		return b_title;
	}
	public void setB_title(String b_title) {
		this.b_title = b_title;
	}
	public String getB_content() {
		return b_content;
	}
	public void setB_content(String b_content) {
		this.b_content = b_content;
	}
	
}

 

 

BoardDBBean.java

package magic.board;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import myUtil.HanConv;

public class BoardDBBean {
	
	private static BoardDBBean instance = new BoardDBBean();
	
	public static BoardDBBean getInstance() {
		return instance;
	}
	
	public Connection getConnection() throws Exception {
		Context ctx = new InitialContext();
		DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/oracle");

		return ds.getConnection();
	}
	
	public int insertBoard(BoardBean board) throws Exception {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";
		
		int re = -1;
		int number;
		try {
			con = getConnection();
			sql = "SELECT MAX(b_id) FROM BOARDT"; // 글번호 추가
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			if(rs.next()) {
				number = rs.getInt(1) + 1;
			} else {
				number = 1;
			}
			
			sql = "insert into boardT values(?, ?, ?, ?, ?, ?, ?, ?)";
			pstmt = con.prepareStatement(sql);

			pstmt.setInt(1, number); // 글번호
			pstmt.setString(2, HanConv.toKor(board.getB_name()));
			pstmt.setString(3, board.getB_email());
			pstmt.setString(4, HanConv.toKor(board.getB_title()));
			pstmt.setString(5, HanConv.toKor(board.getB_content()));
			pstmt.setTimestamp(6, board.getB_date());
			pstmt.setInt(7, board.getB_hit());
			pstmt.setString(8, board.getB_pwd());
			
			pstmt.executeUpdate();
			re = 1;
			
			con.close();
			pstmt.close();
			System.out.println("추가성공");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("추가실패");
		} 
		return re;
	}
	
	public ArrayList<BoardBean> listBoard(){
		Connection con=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		ArrayList<BoardBean> boardList = new ArrayList<BoardBean>();
		
		try {
			con = getConnection();
			String sql="select * from boardt order by b_id";
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			while (rs.next()) {
				BoardBean board=new BoardBean();
				board.setB_id(rs.getInt(1));
				board.setB_name(rs.getString(2));
				board.setB_email(rs.getString(3));
				board.setB_title(rs.getString(4));
				board.setB_content(rs.getString(5));
				board.setB_date(rs.getTimestamp(6));
				board.setB_hit(rs.getInt(7));
				board.setB_pwd(rs.getString(8));
				
				boardList.add(board);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (rs != null) rs.close();
				if (pstmt != null) pstmt.close();
				if (con != null) con.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return boardList;
	}
	
	public BoardBean getBoard(int num, boolean countSet) throws Exception{
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";
		BoardBean board = new BoardBean();
		
		try {
			con = getConnection();
			if(countSet == true) { // updateSet
				sql = "update boardT set b_hit = b_hit + 1 where b_id=?";
				pstmt  = con.prepareStatement(sql);
				pstmt.setInt(1, num);
				pstmt.executeUpdate();
				pstmt.close();				
				sql = "select * from boardT where b_id=?";
				pstmt  = con.prepareStatement(sql);
				pstmt.setInt(1, num);
				rs = pstmt.executeQuery();
			} else {
				sql = "select * from boardT where b_id=?";
				pstmt  = con.prepareStatement(sql);
				pstmt.setInt(1, num);
				rs = pstmt.executeQuery();
			}
			
			if (rs.next()) {
				board.setB_id(rs.getInt("b_id"));
				board.setB_name(rs.getString("b_name"));
				board.setB_email(rs.getString("b_email"));
				board.setB_title(rs.getString("b_title"));
				board.setB_content(rs.getString("b_content"));
				board.setB_date(rs.getTimestamp("b_date"));
				board.setB_hit(rs.getInt("b_hit"));
				board.setB_pwd(rs.getString("b_pwd"));
			}
			con.close();
			pstmt.close();
			rs.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return board;
	}
	
	public int deleteBoard(int num, String b_pwd) throws Exception {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";
		String pwd = "";
		int re = -1;
		try {
			con = getConnection();
			sql = "select b_pwd from boardt where b_id=?";
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, num);
			rs = pstmt.executeQuery();
			
			if(rs.next()) {
				pwd = rs.getString(1);
				// 비밀번호를 가져온다.
				if(!pwd.equals(b_pwd)) {
					re=0;
				} else {
					sql = "delete from boardt where b_id=?";
					pstmt = con.prepareStatement(sql);
					pstmt.setInt(1, num);
					pstmt.executeUpdate();
					re = 1;
				}
			}
			
			pstmt.close();
			con.close();
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return re;
	} 
	
	public int editBoard(BoardBean board) throws Exception {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";
		String pwd = "";
		int re = -1;
		
		try {
			con = getConnection();	
			sql = "select b_pwd from boardt where b_id=?";
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, board.getB_id());
			rs = pstmt.executeQuery();
			
			if(rs.next()) {
				pwd = rs.getString(1);
				// 비밀번호를 가져온다.
				if(!pwd.equals(board.getB_pwd())) {
					re=0;
				} else {
					sql = "update boardT set b_name=?,b_email=?,b_title=?,b_content=? where b_id=? ";
					pstmt = con.prepareStatement(sql);

					pstmt.setString(1, HanConv.toKor(board.getB_name()));
					pstmt.setString(2, board.getB_email());
					pstmt.setString(3, HanConv.toKor(board.getB_title()));
					pstmt.setString(4, HanConv.toKor(board.getB_content()));
					pstmt.setInt(5, board.getB_id()); // 글번호
					
					pstmt.executeUpdate();
					re = 1;
				}
			}
			con.close();
			pstmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return re;
	}
}

 

list.jsp

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="java.util.ArrayList"%>
<%@page import="magic.board.BoardDBBean"%>
<%@page import="magic.board.BoardBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<h1>게시판에 등록된 글 목록</h1>
		<form>
			<table border="1" width="800" cellspacing="0">
					<tr>
						<td width="50" align="center">번호</td>
						<td align="center">제목</td>
						<td width="150" align="center">작성자</td>
						<td width="200" align="center">작성일</td>			
						<td width="80" align="center">조회수</td>			
					</tr>
					<%
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
						BoardDBBean db = BoardDBBean.getInstance();
						ArrayList<BoardBean> list = db.listBoard();
						
						for(int i = 0; i < list.size(); i++) {
							BoardBean data = list.get(i);
							
							int b_id = data.getB_id();
							String name = data.getB_name();
							String title = data.getB_title();
							Timestamp b_date = data.getB_date();
							int count = data.getB_hit();
							
					%>
							<tr bgcolor="#f7f7f7"
								onmouseover="this.style.backgroundColor='#eeeeef'"
								onmouseout="this.style.backgroundColor='#f7f7f7'"
							>
								<td align="center"><%= b_id %></td>
								<td>
									<a href="show.jsp?b_id=<%=b_id %>"><%= title %></a>
								</td>
								<td align="center">
									<a href="mailto:<%=data.getB_email()%>"><%= name %></a>
									<!-- mailto로 이메일프로그램 실행 -->
								</td>
								<td align="center"><%= sdf.format(b_date) %></td>
								<td align="center"><%= count %></td>
							</tr>
					<%
						}
					%>
			</table>
		</form>	
	<a href="write.jsp">글쓰기</a>
</body>
</html>

 

write.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
 <title>게시판 글 등록</title>
 <script type="text/javascript" src="board.js" charset="utf-8"></script>
 </head>
 <body>
 <center>
<table>
	<form name="write_frm" action="write_ok.jsp" method="post">
	     <tr >
	      <td colspan="4" align="center" width="400">
	      	<h1 align="center">글올리기</h1>	
	      </td>
	     </tr>
	     <tr>
	      <td>&nbsp;</td>
	      <td align="center">작성자</td>
	      <td><input name="b_name" size="50" maxlength="100"></td>
	      <td>&nbsp;</td>
	     </tr>
	     <tr height="1" bgcolor="#82B5DF"><td colspan="4"></td></tr>
	    <tr>
	      <td>&nbsp;</td>
	      <td align="center">이메일</td>
	      <td><input name="b_email" size="50" maxlength="50"></td>
	      <td>&nbsp;</td>
	     </tr>
	      <tr height="1" bgcolor="#82B5DF"><td colspan="4"></td></tr>
	    <tr>
	      <td>&nbsp;</td>
	      <td align="center">글제목</td>
	      <td><input name="b_title" size="50" maxlength="50"></td>
	      <td>&nbsp;</td>
	     </tr>
	     <tr height="1" bgcolor="#82B5DF"><td colspan="4"></td></tr>
	     <tr>
	      <td>&nbsp;</td>
	      <td align="center">내용</td>
	      <td><textarea name="b_content" cols="50" rows="13"></textarea></td>
	      <td>&nbsp;</td>
	     </tr>
	     <tr>
	      <td>&nbsp;</td>
	      <td align="center">비밀번호</td>
	      <td><input name="b_pwd" size="20" maxlength="12"></td>
	      <td>&nbsp;</td>
	     </tr>
	     <tr height="1" bgcolor="#dddddd"><td colspan="4"></td></tr>
	     <tr height="1" bgcolor="#82B5DF"><td colspan="4"></td></tr>
	     <tr align="center">
	      <td>&nbsp;</td>
	      <td colspan="3">
	       <input type="button" value="글쓰기" onclick="check_write()">
	       &nbsp;&nbsp;
	       <input type="reset" value="다시작성">
	       &nbsp;&nbsp;
	       <input type="button" value="글목록" onclick="location.href='list.jsp'">
	      <td>&nbsp;</td>
	     </tr>
  	</form>
 </table>
 </center>
</body> 

 

write_ok.jsp

<%@page import="java.sql.Timestamp"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<jsp:useBean id="bd" class="magic.board.BoardBean"/>
<jsp:setProperty property="*" name="bd"/>
<%
	bd.setB_date(new Timestamp(System.currentTimeMillis()));
	BoardDBBean manager = BoardDBBean.getInstance();

	int re= manager.insertBoard(bd);
	System.out.println("@@@###getMem_uid ===>"+ re);
		
	if(re == 1) {
%>
	<script>
		alert("글을 등록 하셨습니다.");
	</script>
<%
		response.sendRedirect("list.jsp");
	}else {
%>
	<script>
		alert("글 등록에 실패했습니다.");
	</script>
<%
		response.sendRedirect("write.jsp");		
	}

%>

 

show.jsp

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
	int b_id = Integer.parseInt(request.getParameter("b_id"));
	BoardDBBean manager = BoardDBBean.getInstance();
	BoardBean bb = manager.getBoard(b_id,true);
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<style type="text/css">
</style>
</head>
<body>
	<div class="body">	
		<h1>글 내용</h1>
		<table border="1" cellspacing="0" width="800">
			<tr align="center">
				<td>글 번호</td>
				<td align="center" width="250"><%= bb.getB_id() %></td>
				<td>조회수</td>
				<td align="center" width="250"><%= bb.getB_hit() %></td>
			</tr>
			<tr align="center">
				<td>작성자</td>
				<td align="center"><%= bb.getB_name() %></td>
				<td>작성일자</td>
				<td align="center"><%= sdf.format(bb.getB_date()) %></td>
			</tr>
			<tr align="center">
				<td>글 제목</td>
				<td colspan="3" align="left"><%= bb.getB_title() %></td>
			</tr>
			<tr align="center">
				<td>글 내용</td>
				<td colspan="3" align="left"><%= bb.getB_content() %></td>
			</tr>
			<tr>
				<td colspan="4" align="right">
					<input type="button" value="글목록" onclick="location.href = 'list.jsp'">
					<input type="button" value="글수정" onclick="location.href = 'edit.jsp?b_id=<%=b_id %>'">
					<input type="button" value="글삭제" onclick="location.href = 'delete.jsp?b_id=<%=b_id %>'">
				</td>
			</tr>
		</table>
	</div>
</body>
</html>

 

delete.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
	int b_id = Integer.parseInt(request.getParameter("b_id"));
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<script type="text/javascript" src="board.js" charset="utf-8"></script>
</head>
<body>
	<h1> >>암호를 입력하세요<< </h1>
	<table>
		<form action="delete_ok.jsp?b_id=<%= b_id %>" method="post" name="delete_frm">
			<tr>
				<td>암 호 : <input type="password" name="b_pwd"></td>
			<tr>
			<tr>
				<td>
					<input type="button" value="글삭제" onclick="delete_ok()">
					<input type="reset" value="다시작성">
					<input type="button" value="글 목록" onclick="location.href = 'list.jsp'">
				</td>
			</tr>
		</form>
	</table>
</body>
</html>

 

delete_ok.jsp

<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
	int id = Integer.parseInt(request.getParameter("b_id"));
	String pwd = request.getParameter("b_pwd");
	
	BoardDBBean manager = BoardDBBean.getInstance();
	int re = manager.deleteBoard(id, pwd);

	if(re == 1) {
		response.sendRedirect("list.jsp");
	}else if (re == 0){
%>
		<script language="JavaScript">
			alert("비밀번호가 틀렸습니다.");
			delete_frm.b_pwd.onfocus;
		</script>
<%		
	} else {
%>
		<script language="JavaScript">
			alert("삭제에 실패했습니다.");
			history.go(-1);
		</script>
<%				
	}

%>


edit.jsp

<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
	int b_id = Integer.parseInt(request.getParameter("b_id"));
	BoardDBBean manager = BoardDBBean.getInstance();
	BoardBean bb = manager.getBoard(b_id,false);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<script type="text/javascript" src="board.js" charset="utf-8"></script>
</head>
<body>
	<table>
	<form name="edit_frm" action="edit_ok.jsp?b_id=<%= b_id %>" method="post">
	     <tr >
	      <td colspan="4" align="center" width="400">
	      	<h1 align="center">글 수정하기</h1>	
	      </td>
	     </tr>
	     <tr>
	      <td>&nbsp;</td>
	      <td align="center">작성자</td>
	      <td><input name="b_name" size="50" value="<%= bb.getB_name()%>"></td>
	      <td>&nbsp;</td>
	     </tr>
	     <tr height="1" bgcolor="#82B5DF"><td colspan="4"></td></tr>
	    <tr>
	      <td>&nbsp;</td>
	      <td align="center">이메일</td>
	      <td><input name="b_email" size="50" value="<%= bb.getB_email()%>"></td>
	      <td>&nbsp;</td>
	     </tr>
	      <tr height="1" bgcolor="#82B5DF"><td colspan="4"></td></tr>
	    <tr>
	      <td>&nbsp;</td>
	      <td align="center">글제목</td>
	      <td><input name="b_title" size="50" value="<%= bb.getB_title() %>"></td>
	      <td>&nbsp;</td>
	     </tr>
	     <tr height="1" bgcolor="#82B5DF"><td colspan="4"></td></tr>
	     <tr>
	      <td>&nbsp;</td>
	      <td align="center">내용</td>
	      <td><textarea name="b_content" cols="50" rows="13" ><%= bb.getB_content() %></textarea></td>
	      <td>&nbsp;</td>
	     </tr>
	     <tr>
	      <td>&nbsp;</td>
	      <td align="center">비밀번호</td>
	      <td><input type="password" name="b_pwd" size="20" maxlength="12"></td>
	      <td>&nbsp;</td>
	     </tr>
	     <tr height="1" bgcolor="#dddddd"><td colspan="4"></td></tr>
	     <tr height="1" bgcolor="#82B5DF"><td colspan="4"></td></tr>
	     <tr align="center">
	      <td>&nbsp;</td>
	      <td colspan="3">
	       <input type="button" value="글수정" onclick="check_ok()">
	       &nbsp;&nbsp;
	       <input type="reset" value="다시작성">
	       &nbsp;&nbsp;
	       <input type="button" value="글목록" onclick="location.href='list.jsp'">
	      <td>&nbsp;</td>
	     </tr>
  	</form>
 </table>	
</body>
</html>

 

edit_ok.jsp

<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<jsp:useBean id="bd" class="magic.board.BoardBean"/>
<jsp:setProperty property="*" name="bd"/>
<%     
	BoardDBBean manager = BoardDBBean.getInstance();
	int re = manager.editBoard(bd);

	if(re == 1) {
		response.sendRedirect("list.jsp");
	}else if (re == 0){
%>
		<script language="JavaScript">
			alert("비밀번호가 틀렸습니다.");
			history.go(-1);
		</script>
<%		
	} else {
%>
		<script language="JavaScript">
			alert("수정에 실패했습니다.");
			history.go(-1);
		</script>
<%				
	}

%>

 

board.js

function check_write() {
	
	if(document.write_frm.b_name.value.length == 0) {
		alert("이름을 써주세요.");
		write_frm.b_name.focus();
		return;
	}
	
	if(document.write_frm.b_email.value.length == 0) {
		alert("이메일을 써주세요.");
		write_frm.b_email.focus();
		return;
	}
	
	if(document.write_frm.b_title.value.length == 0) {
		alert("제목을 써주세요.");
		write_frm.b_title.focus();
		return;
	}
	
	if(document.write_frm.b_content.value.length == 0) {
		alert("내용을 써주세요.");
		write_frm.b_content.focus();
		return;
	}
	
	if(document.write_frm.b_pwd.value.length == 0) {
		alert("비밀번호를 써주세요.");
		write_frm.b_pwd.focus();
		return;
	}
	
	
	document.write_frm.submit();
}

function check_ok() {
	
	if(document.edit_frm.b_name.value.length == 0) {
		alert("이름을 써주세요.");
		edit_frm.b_name.focus();
		return;
	}
	
	if(document.edit_frm.b_email.value.length == 0) {
		alert("이메일을 써주세요.");
		edit_frm.b_email.focus();
		return;
	}
	
	if(document.edit_frm.b_title.value.length == 0) {
		alert("제목을 써주세요.");
		edit_frm.b_title.focus();
		return;
	}
	
	if(document.edit_frm.b_content.value.length == 0) {
		alert("내용을 써주세요.");
		edit_frm.b_content.focus();
		return;
	}
	
	document.edit_frm.submit();
}

function delete_ok() {
	if(document.delete_frm.b_pwd.value.length == 0 ) {
		alert("비밀번호를 입력해 주세요.");
		delete_frm.b_pwd.focus();
		return;
	}
	
	document.delete_frm.submit();
}