게시판 글 목록을 보여주는 list.jsp

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="board.BoardDataBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ include file="setting.jsp" %>
<link type="text/css" rel="stylesheet" href="<%=project%>/board/style_board.css">
<script src="<%=project%>/board/script.js"></script>

<%
	int pageSize = 10;  	//페이지 크기
	int pageBlock = 3;  	//페이지 블락(나눔)

	int count = 0;			//개수 
	String pageNum = null;	//시작 페이지번호
	int currentPage = 0;	//현재 페이지
	int start = 0;			//시작번호
	int end = 0;			//끝번호
	int number = 0;			//페이지번호
	
	int pageCount = 0;		//페이지개수
	int startPage = 0;		//시작페이지
	int endPage = 0;		//끝페이지
%>
<%
	pageNum = request.getParameter( "pageNum" );
	if( pageNum == null ) {	//페이지가 존재하지 않으면 페이지숫자는  1
		pageNum = "1";
	}
	currentPage = Integer.parseInt( pageNum );	
	start = ( currentPage -1 ) * pageSize + 1;			// ( 5 - 1 ) * 10 + 1		41
	end = start + pageSize - 1;							// 41 + 10 -1				50
	
	BoardDBBean dao = BoardDBBean.getInstance();
	count = dao.getCount();
	
	if( end > count ) end = count;
	
	number = count - ( currentPage - 1 ) * pageSize;	// 50 - ( 5 - 1 ) * 10		10 
	
	pageCount = ( count / pageSize ) + ( count % pageSize > 0 ? 1 : 0 );
	startPage = ( currentPage / pageBlock ) * pageBlock + 1;	// ( 19 / 10 ) * 10 + 1		11
	if( currentPage % pageBlock == 0 ) startPage -= pageBlock;
	endPage = startPage + pageBlock - 1;						// 11 + 10 -1 				20
	if( endPage > pageCount ) endPage = pageCount;
	
%>
<h2> <%=page_list%> ( <%=str_count%> : <%=count%> ) </h2>    

<table>
	<tr>
		<td colspan="6" style="text-align:right">
			<a href="inputForm.jsp"><%=str_write%></a>&nbsp;&nbsp;&nbsp;
		</td>
	</tr>
	<tr>
		<th style="width:7%"> <%=str_num%> </th>
		<th style="width:35%"> <%=str_subject%> </th>
		<th style="width:10%"> <%=str_writer%> </th>
		<th style="width:15%"> <%=str_reg_date%> </th>
		<th style="width:7%"> <%=str_readcount%> </th>
		<th style="width:10%"> <%=str_ip%> </th>
	</tr>
	<%
		if( count == 0 ) {
			// 글이 없는 경우
			%>
			<tr>
				<td colspan="6" style="text-align:center"> <%=msg_list%> </td>
			</tr>
			<%
		} else {
			// 글이 있는 경우			
			ArrayList <BoardDataBean> dtos = dao.getArticles( start, end );
			for( BoardDataBean dto : dtos ) {
				%>
				<tr>
					<td style="text-align:center">
						<%=number--%>
					</td>
					<td>
						<%
							int level = dto.getRe_level();
							int wid = ( level -1 ) * 10;
							if( level > 1 ) {
								%>
								<img src="../images/level.gif" width="<%=wid%>" height="15">
								<%
							}
							if( level > 0 ) {
								%>
								<img src="../images/re.gif" width="20" height="15">
								<%
							}	
							
							if( dto.getReadcount() == -1 ) {
								%>
									<%=dto.getSubject()%>	
								<%		
							} else {
								%>
								<a href="content.jsp?num=<%=dto.getNum()%>&pageNum=<%=pageNum%>&number=<%=number+1%>">
									<%=dto.getSubject()%>
								</a>
								<%	
							}
							%>
					</td>
					<td style="text-align:center">
						<%=dto.getWriter()%>
					</td>
					<td style="text-align:center">
						<%
						SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );						
						%>
						<%=sdf.format( dto.getReg_date() )%>
					</td>
					<td style="text-align:center">
						<%
						if( dto.getReadcount() == -1 ) {
							%>
							&nbsp;
							<%
						} else {
							%>						
							<%=dto.getReadcount()%>
							<%
						}
						%>	
					</td>
					<td style="text-align:center">
						<%=dto.getIp()%>
					</td>
				</tr>
				<%
			}			
		}
	%>
</table>
<br>
<center>
<%
	if( count > 0 ) {
		if( startPage > pageBlock ) {
			%>
			<a href="list.jsp?pageNum=<%=startPage - pageBlock%>">[◀]</a>
			<%	
		}		
		for( int i=startPage; i<=endPage; i++ ) {
			if( i == currentPage ) {
				%>
				<b>[<%=i%>]</b>
				<%
			} else {
				%>
				<a href="list.jsp?pageNum=<%=i%>">[<%=i%>]</a>
				<%
			}
		}
		if( pageCount > endPage ) {
			%>
			<a href="list.jsp?pageNum=<%=startPage + pageBlock%>">[▶]</a>
			<%
		}
		
	}	
%>
</center>

글 작성을 할 수 있는 inputForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<%@ include file="setting.jsp" %>
<link type="text/css" rel="stylesheet" href="<%=project%>/board/style_board.css">
<script src="<%=project%>/board/script.js"></script>

<h2> <%=page_write%> </h2>

<%
	// 제목글
	int num = 0;
	int ref = 0;
	int re_step = 0;
	int re_level = 0;
	if( request.getParameter( "num" ) != null ) {
		// 답변글
		num = Integer.parseInt( request.getParameter( "num" ) );
		ref = Integer.parseInt( request.getParameter( "ref" ) );
		re_step = Integer.parseInt( request.getParameter( "re_step" ) );
		re_level = Integer.parseInt( request.getParameter( "re_level" ) );
	}
%>

<form name="writeform" method="post" action="inputPro.jsp" onsubmit="return writecheck()">
	<input type="hidden" name="num" value="<%=num%>">
	<input type="hidden" name="ref" value="<%=ref%>">
	<input type="hidden" name="re_step" value="<%=re_step%>">
	<input type="hidden" name="re_level" value="<%=re_level%>">
	
	<table>
		<tr>
			<th colspan="2" style="text-align:right">
				<a href="list.jsp"><%=str_list%></a>&nbsp;&nbsp;&nbsp;
			</th>
		</tr>
		<tr>
			<th> <%=str_writer%> </th>
			<td>
				<input class="input" type="text" name="writer" maxlength="30" autofocus>
			</td>
		</tr>
		<tr>
			<th> <%=str_email%> </th>
			<td>
				<input class="input" type="text" name="email" maxlength="40">
			</td>
		</tr>
		<tr>
			<th> <%=str_subject%> </th>
			<td>
				<input class="input" type="text" name="subject" maxlength="100">
			</td>
		</tr>
		<tr>
			<th> <%=str_content%> </th>
			<td>	
				<textarea name="content" rows="10" cols="40"></textarea>				
			</td>
		</tr>
		<tr>
			<th> <%=str_passwd%> </th>
			<td>	
				<input class="input" type="password" name="passwd" maxlength="20">
			</td>
		</tr>
		<tr>
			<th colspan="2">
				<input class="inputbutton" type="submit" value="<%=btn_write%>">
				<input class="inputbutton" type="reset" value="<%=btn_cancel%>">
				<input class="inputbutton" type="button" value="<%=btn_list%>"
					onclick="location='list.jsp'">				
			</th>
		</tr>
	</table>
</form>

글 작성 로직을 구현할 inputPro.jsp

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

<%@ include file="setting.jsp" %>
<script src="<%=project%>/board/script.js"></script>    

<h2> <%=page_write%> </h2>

<%
	request.setCharacterEncoding( "utf-8" );
%>

<jsp:useBean id="dto" class="board.BoardDataBean"/>
	<jsp:setProperty name="dto" property="*"/>
	<!-- writer email subject content passwd -->
	<!-- num ref re_step re_level -->
<%
	dto.setReg_date( new Timestamp( System.currentTimeMillis() ) );
%>	
<%
	dto.setIp( request.getRemoteAddr() );
%>
<%
	BoardDBBean dao = BoardDBBean.getInstance();
	int result = dao.insertArticle( dto );
	if( result == 0 ) {
		%>
		<script type="text/javascript">
			<!--
			alert( inserterror );
			//-->
		</script>
		<meta http-equiv="refresh" content="0; url=list.jsp">
		<%		
	} else {
		response.sendRedirect( "list.jsp" );		
	}
%>