본문 바로가기

Study/JSP

[jsp] 게시판

게시판의 세부분류
1. 글 쓰기
2. 글 리스트 보기 - 답변 글, 페이징 처리, 검색
3. 글 세부보기 - 댓글 보기(DOM)
4. 글 수정하기
5. 글 삭제하기
6. 기타 - 파일 업로드

b_ref : 글 그룹번호(원본 글 번호)
b_step : 그룹 안 글 순서(정렬)
b_level : 답변 글들의 들여쓰기  깊이

 

 

server.xml의 추가

<Context docBase="board_exam" 
	path="/board_exam" 
    reloadable="true" 
    source="org.eclipse.jst.jee.server:board_exam">
  <Resource auth="Container"
  	driverClassName="oracle.jdbc.driver.OracleDriver" 
  	maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/oracle" 
  	password="tiger" type="javax.sql.DataSource" 
  	url="jdbc:oracle:thin:@localhost:1521:ORCL" username="scott"/>
</Context>

 

write.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="magic.board.*"%>
<%
 //답변 글인지 판단한다(b_id를 검사)
 int b_id = 0;
 int b_ref = 0;
 int b_step = 0;
 int b_level = 0;

 String b_title = "";

 //답변 글일 때만 수행된다
 if (request.getParameter("b_id") != null) {
  b_id = Integer.parseInt(request.getParameter("b_id"));
 }

 BoardDBBean db = BoardDBBean.getInstance();
 
 //글 번호에 해당되는 글 내용만 반환한다
 BoardBean board = db.getBoard(b_id, false);

 //글 번호에 해당되는 글 내용을 불러오지 못할 때 예외처리
 if (board != null) {
  b_title = board.getB_title();
  b_ref = board.getB_ref();
  b_step = board.getB_step();
  b_level =board.getB_level();
 }
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>글 올리기</h1>

<!-- enctype="multipart/form-data" 지정해주지 않으면
 파일 업로드도 되지 않고 request객체를 사용할 수없다 -->
<form action="write_ok.jsp" method="post" enctype="multipart/form-data">
 <input type="hidden" name="b_id" value="<%= b_id %>">
 <input type="hidden" name="b_ref" value="<%= b_ref %>">
 <input type="hidden" name="b_step" value="<%= b_step %>">
 <input type="hidden" name="b_level" value="<%= b_level %>">

 <table border="0" cellpadding="0" cellspacing="0">
  <tr height=30>
   <td width=80>작성자</td>
   <td width=140>
    <input type="text" name="b_name" maxlength="15" size=10></td>
   <td width=80>이메일</td>
   <td width=240>
    <input type="text" name="b_email" maxlength="30" size=24></td>
  </tr>

  <tr height=30>
   <td width=80>글제목</td>
   <td colspan=3 width=460>
    <%
     if(b_id == 0) {
    %>
      <input type="text" name="b_title" maxlength="60" size=55> 
    <%    
     } else {
    %>
      <input type="text" name="b_title" 
      maxlength="60" size=55 value="[답변] :<%= b_title %>">
    <%} %> 
   </td>
  </tr>

  <tr height=30>
   <td width=80>파일</td>
   <td colspan=3 width=140>
    <input type="file" name="b_filename" maxlength=60 size=40>
   </td>
  </tr>

  <tr>
   <td colspan=4>
    <textarea wrap=hard rows=10 cols=65 name="b_content">
    </textarea>
   </td>
  </tr>
  <tr>
   <td width="80">암  호</td>
   <td>
    <input type=password maxLength=12 size=12 name=b_pwd>
   </td>
  </tr>
  <tr align="center" height="50">
   <td width="480" colspan="4">
    <input onclick=check_ok() type=submit value=글쓰기>
    <input type=reset value=" 다시작성">
    <input onclick="location.href='list.jsp'" type=button value=글목록>
   </td>
  </tr>
 </table>
</form>
<center></center>


write_ok.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
 pageEncoding="EUC-KR"%>
<%@ page import="java.sql.Timestamp"%>
<%@ page import="magic.board.*" %>
<%@ page import="com.oreilly.servlet.*" %>
<%@ page import="com.oreilly.servlet.multipart.*" %>
<%@ page import="java.io.*" %>

<%
 request.setCharacterEncoding("euc-kr");

 BoardBean board = new BoardBean();

 //upload폴더의 경로를 가져온다
 String uploadPath = request.getRealPath("upload");
 int size = 30*1024*1024;

 //multi를 생성하자마자 파일 업로드가 발생된다
 //인자값(request, 파일경로, 파일크기, 인코딩 방식, 파일이름 중첩방지);
 MultipartRequest multi = 
 new MultipartRequest(request, uploadPath, size, "euc-kr", new DefaultFileRenamePolicy());

 String b_filename = multi.getFilesystemName("b_filename");

 //b_filename을 바로 넣어주면, 경로 지정이 되지 않아서 null값이 들어가게된다
 String path = uploadPath + "\\" + b_filename;

 //파일을 만들어서 경로를 넣어줌
 File file = new File(path);

 //파일 크기 정하기
 int b_fsize = (int)file.length();

 board.setB_name(multi.getParameter("b_name"));
 board.setB_email(multi.getParameter("b_email"));
 board.setB_title(multi.getParameter("b_title"));
 board.setB_content(multi.getParameter("b_content"));
 board.setB_pwd(multi.getParameter("b_pwd"));
 board.setB_id(Integer.parseInt(multi.getParameter("b_id")));
 board.setB_ref(Integer.parseInt(multi.getParameter("b_ref")));
 board.setB_step(Integer.parseInt(multi.getParameter("b_step")));
 board.setB_level(Integer.parseInt(multi.getParameter("b_level")));
 board.setB_ip(request.getRemoteAddr());
 board.setB_date(new Timestamp(System.currentTimeMillis()));
 board.setB_fname(b_filename);
 board.setB_fsize(b_fsize);
 
 BoardDBBean db = BoardDBBean.getInstance();
 int re = db.insertBoard(board);

 if(re == 1){
  response.sendRedirect("list.jsp");
 }else{
  response.sendRedirect("write.jsp");
 }
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
</body>
</html>


list.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import = "magic.board.*" %>
<%@ page import = "java.util.*" %>
<%@page import="java.sql.Timestamp"%>

<%
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
 String pageNUM = request.getParameter("pageNUM");
 
 if(pageNUM == null) {
  pageNUM = "1";
 }

 BoardDBBean db = BoardDBBean.getInstance();
 ArrayList<BoardBean> boardList = db.listBoard(pageNUM);
 
 String b_name, b_email, b_title, b_content, b_fname;
 int b_id, b_hit, b_level = 0, b_fsize;
 Timestamp b_date;
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.text.SimpleDateFormat"%><html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
 <h1>게시판 등록된 글 목록 보기</h1>
 <table width=600>
  <tr>
   <td align=right>
    <a href="write.jsp">글쓰기</a>
   </td>
  </tr>
 </table>

 <table width=800 border=1 cellpadding="0" cellspacing="0">
  <tr height=25>
   <td width=40 align=center>번호</td>
   <td width=60 align=center>첨부<br />파일</td>
   <td width=450 align=center>글 제목</td>
   <td width=120 align=center>작성자</td>
   <td width=130 align=center>작성일</td>
   <td width=60 align=center>조회수</td> 
  </tr>

  <%
   for(int i=0; i<boardList.size(); i++) {
    BoardBean board = boardList.get(i);
    b_id = board.getB_id();
    b_level = board.getB_level();
    b_title = board.getB_title();
    b_name = board.getB_name();
    b_email = board.getB_email();
    b_date = board.getB_date();
    b_hit = board.getB_hit();
    b_fname = board.getB_fname();
    b_fsize = board.getB_fsize();
  %>

  <tr height=25>
   <td align=center>
    <%= b_id %>
   </td>
   
   <td>
    <!-- 파일의 크기가 있으면 출력 -->
    <% if(b_fsize > 0) { %>
     <img src="../images/zip.gif" 
     alt="<% out.println(b_fname+"("+b_fsize/1024+")"); %>KByte">
    <% } %>
   </td>
   
   <td align=left>
    <%
     if(b_level > 0) {
      for(int k=0; k<b_level; k++) {
      %>
      <%
      }
      %>
       <img src="../images/AnswerLine.gif" width=15 height=15 border=0>
      <%
     }
    %>
    <a href="show.jsp?b_id=<%= b_id %>&pageNUM=<%= pageNUM %>"><%= b_title %></a>
   </td>

   <td align=center>
    <a href="mailto:<%= b_email %>"><%= b_name %></a>
   </td>

   <td align=center>
    <%= sdf.format(b_date) %>
   </td>
   
   <td align=center>
    <%= b_hit %>
   </td>
  </tr>
  <%
  }
  %>
 </table>
 <br />
 <%= BoardBean.pageNumber(5) %>
</center>
</body>
</html>


show.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import = "magic.board.*" %>
<%@ page import = "java.util.*" %>
<%@page import="java.sql.Timestamp"%>

<%
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
 String pageNUM = request.getParameter("pageNUM");

 if(pageNUM == null) {
  pageNUM = "1";
 }

 BoardDBBean db = BoardDBBean.getInstance();
 ArrayList<BoardBean> boardList = db.listBoard(pageNUM);

 String b_name, b_email, b_title, b_content, b_fname;
 int b_id, b_hit, b_level = 0, b_fsize;

 Timestamp b_date;
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.text.SimpleDateFormat"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
 <h1>게시판 등록된 글 목록 보기</h1>
 <table width=600>
  <tr>
   <td align=right>
    <a href="write.jsp">글쓰기</a>
   </td>
  </tr>
 </table>

 <table width=800 border=1 cellpadding="0" cellspacing="0">
  <tr height=25>
   <td width=40 align=center>번호</td>
   <td width=60 align=center>첨부<br />파일</td>
   <td width=450 align=center>글 제목</td>
   <td width=120 align=center>작성자</td>
   <td width=130 align=center>작성일</td>
   <td width=60 align=center>조회수</td> 
  </tr>

  <%
   for(int i=0; i<boardList.size(); i++) {
    BoardBean board = boardList.get(i);
    b_id = board.getB_id();
    b_level = board.getB_level();
    b_title = board.getB_title();
    b_name = board.getB_name();
    b_email = board.getB_email();
    b_date = board.getB_date();
    b_hit = board.getB_hit();
    b_fname = board.getB_fname();
    b_fsize = board.getB_fsize();
  %>

  <tr height=25>
   <td align=center>
    <%= b_id %>
   </td>

   <td>
    <!-- 파일의 크기가 있으면 출력 -->
    <% if(b_fsize > 0) { %>
     <img src="../images/zip.gif" 
     alt="<% out.println(b_fname+"("+b_fsize/1024+")"); %>KByte">
    <% } %>
   </td>

   <td align=left>
    <%
     if(b_level > 0) {
      for(int k=0; k<b_level; k++) {
      %>
      <%
      }
      %>
       <img src="../images/AnswerLine.gif" width=15 height=15 border=0>
      <%
     }
    %>
    <a href="show.jsp?b_id=<%= b_id %>&pageNUM=<%= pageNUM %>"><%= b_title %></a>
   </td>

   <td align=center>
    <a href="mailto:<%= b_email %>"><%= b_name %></a>
   </td>

   <td align=center>
    <%= sdf.format(b_date) %>
   </td>

   <td align=center>
    <%= b_hit %>
   </td>
  </tr>
  <%
  }
  %>
 </table><br />

 <%= BoardBean.pageNumber(5) %>

</center>
</body>
</html>


edit.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import = "magic.board.*" %>
<%@ page import = "java.util.*" %>

<%
 int b_id = Integer.parseInt(request.getParameter("b_id"));
 String pageNUM = request.getParameter("pageNUM");

 BoardDBBean db = BoardDBBean.getInstance();
 BoardBean board = db.getBoard(b_id, false); 
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Insert title here</title>
<script src="board.js">
</script>
</head>
<body>
<center>
<h1>글 수 정 하 기</h1>
<form name="form" method="post" action="edit_ok.jsp?b_id=<%= b_id %>&pageNUM=<%= pageNUM %>">

<table border=0 cellpadding="0" cellspacing="0">
 <tr height=30>
  <td width=80>작성자</td>
  
  <td width=140>
  <input type="text" name="b_name" maxlength=15 size=10 value="<%= board.getB_name() %>">
  </td>

  <td width=80>이메일</td>
  
  <td width=240><input type="text" name="b_email" maxlength=30 size=24 
  value="<%= board.getB_email() %>">
  </td>
 </tr>

 <tr height=30>
  <td width=80>글제목</td>
  <td colspan=3 width=460>
   <input type="text" name="b_title" maxlength=60 size=55 value="<%= board.getB_title() %>">
   </td>
 </tr>

 <tr>
  <td colspan=4 width=480>
   <textarea wrap=hard name="b_content" rows=10 cols=65>
    <%= board.getB_content() %>
   </textarea> 
  </td>
 </tr>
 
 <tr>
  <td width="80">암  호</TD> <TD width="460" colspan="3">
   <input type=password maxLength=12 size=12 name=b_pwd>
  </td>
 </tr>
 <tr align="center" height="50">
  <td width="480" colspan="4">
   <input onclick=check_ok() type=button value=글수정>
   <input type=reset value=다시작성>
   <input onclick="location.href='list.jsp?pageNUM=" type=button value=글목록>
  </td>
 </tr>
</table>
</body>
</html>