[JSP] 게시판 ③ - 게시글 목록


bbs/List.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.Vector"%>
<%@ page import="pack_BBS.BoardBean" %>
<jsp:useBean id="boardMgr" class="pack_BBS.BoardMgr" scope="page" />

<% 
	request.setCharacterEncoding("UTF-8");
	
	int listSize = 0; // 출력할 데이터의 개수 (데이터 1개 = 기로줄 1)
	
	Vector<BoardBean> vList = null;
%>
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JSP Board - List</title>
    <link rel="stylesheet" href="/Proj_BBS/style/Style.css">
</head>
<body>
	<div id="wrap">
	<h1>List 페이지</h1>
	<header id="header">
		<nav>
			<ul>
				<li><a href="/Proj_BBS/Index.jsp">메인</a></li>
				<li><a href="#">로그인</a></li>
				<li><a href="/Proj_BBS/bbs/Post.jsp">글쓰기</a></li>
				<li><a href="/Proj_BBS/bbs/List.jsp">자유게시판</a></li>
			</ul>
		</nav>
	</header>
	<!-- header#header -->
		
	<main id="main" class="list">
	
		<table id="boardList">
			<thead>
				<tr>
					<th>번호</th>
					<th>제목</th>
					<th>이름</th>
					<th>날짜</th>
					<th>조회수</th>
				</tr>		
			</thead>
			<tbody>
		<%				
			vList = boardMgr.getBoardList(); // DB에서 데이터 불러오기 
			listSize = vList.size();
		
			if (vList.isEmpty()) {
		%>
				<tr>
					<td colspan="5"><%="등록된 게시물이 없습니다." %></td>
				</tr>
		<%
			} else {
	
				for (int i=0; i<listSize; i++) {
					
					if (i == listSize) break;
					
					BoardBean bean = vList.get(i);
					/* 제네릭 없다면 형변환 필요 : (BoardBean)vList.get(i) */
					
					int num = bean.getNum();
					String subject = bean.getSubject();
					String uName = bean.getuName();
					String regDate = bean.getRegDate();
					int depth = bean.getDepth();
					int count = bean.getCount();
		%>
			
				<tr class="trHover" onclick="read('<%=num %>')">
					<td><%=num %></td>
					<td><%=subject %></td>
					<td><%=uName %></td>
					<td><%=regDate %></td>
					<td><%=count %></td>
				</tr>	
							
		<%
				}
			}
		%>			

			</tbody>
		</table>
	</main>
	<!-- main#main -->
	</div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="/Proj_BBS/script/Script.js"></script>
</body>
</html>



pack_BBS/BoardMgr.java


package pack_BBS;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

public class BoardMgr {

	private DBConnectionMgr pool;
	private static final String SAVEFOLDER = "D:/ezen/silsp/jsp_Model1/Proj_BBS/src/main/webapp/fileupload";
	private static String encType = "UTF-8";
	private static int maxSize = 5 * 1024 * 1024;
	
	Connection			conn	=	null;
	PreparedStatement	pstmt	=	null;
	ResultSet			rs		=	null;
	String				sql		=	null;
	
	public BoardMgr() {
		try {
			pool = DBConnectionMgr.getInstance();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	// 글 입력 (PostProc.jsp)
	public void insertBoard(HttpServletRequest req) {
		
		MultipartRequest	multi		=	null;
		int					fileSize	=	0;
		String				fileName	=	null;
		
		try {
			conn = pool.getConnection();
			
			sql = "select max(num) from tblBoard";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			int ref = 1; 
			if (rs.next()) ref = rs.getInt(1) + 1;
			
			File file = new File(SAVEFOLDER);
			if (!file.exists()) file.mkdirs();
			
			multi = new MultipartRequest(req, SAVEFOLDER, maxSize, encType, new DefaultFileRenamePolicy());
			
			if (multi.getFilesystemName("fileName") != null) {
				fileName = multi.getFilesystemName("fileName");
				fileSize = (int)multi.getFile("fileName").length();
			}
			
			String content = multi.getParameter("content");
			
			if (multi.getParameter("contentType").equalsIgnoreCase("TEXT")) {
				content = UtilMgr.replace(content, "<", "&lt;");
			}
			
			sql = "insert into tblBoard (";
			sql += "uName, subject, content, ref, pos, depth, ";
			sql += "regDate, uPw, ip, count, fileName, fileSize) values(";
			sql += "?, ?, ?, ?, 0, 0, now(), ?, ?, 0, ?, ?)";

			pstmt = conn.prepareStatement(sql);		
			pstmt.setString(1, multi.getParameter("uName"));
			pstmt.setString(2, multi.getParameter("subject"));
			pstmt.setString(3, content);
			pstmt.setInt(4, ref);
			pstmt.setString(5, multi.getParameter("uPw"));
			pstmt.setString(6, multi.getParameter("ip"));
			pstmt.setString(7, fileName);
			pstmt.setInt(8, fileSize);
			pstmt.executeUpdate();
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pool.freeConnection(conn, pstmt, rs);
		}
	}
	
	// 게시판 리스트 출력 (List.jsp)
    public Vector<BoardBean> getBoardList(){
        
        Vector<BoardBean> vList = new Vector<>();
        
        try {
            conn = pool.getConnection();

            sql = "select * from tblBoard order by num desc limit ?, ?";
                                                            
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, 0);
            pstmt.setInt(2, 10);
            rs = pstmt.executeQuery();
            
            while (rs.next()) {
                BoardBean bean = new BoardBean();
                bean.setNum(rs.getInt("num"));
                bean.setuName(rs.getString("uName"));
                bean.setSubject(rs.getString("subject"));
                bean.setPos(rs.getInt("pos"));
                bean.setRef(rs.getInt("ref"));
                bean.setDepth(rs.getInt("depth"));
                bean.setRegDate(rs.getString("regDate"));
                bean.setCount(rs.getInt("count"));

                vList.add(bean);
            }
            
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            pool.freeConnection(conn, pstmt, rs);
        }
        
        return vList;
    }
}



style/Style.css


@charset "UTF-8";

* {
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	box-sizing: border-box;
}

.flex-container { display: flex; }

div#wrap {
	width: 800px;
	border: 1px solid #000;
	margin: 10px auto;
	padding: 20px;
}

#header, #main { margin: 10px 0; }

#main { padding: 40px; }

h1 { text-align: center; }

ul { 
	list-style: none;
	display: flex;
}

li {
	width: 170px; 
	border: 1px solid #000;
	text-align: center;
}

li:hover{ background-color: #ddd; }

a {
	font-size: 20px;
	text-decoration: none;
	color: #000;
}

tfoot>tr>td {
	text-align: center;	
}


/* Post.jsp */
::-webkit-file-upload-button { 
   cursor:pointer; 
}

.postTbl td, .postTbl th {
	border-bottom: 1px solid #eee;
	padding: 8px 10px;
}
.postTbl th {
	background-color: #eee;
	width: 100px;
}

.postTbl input, .postTbl textarea {
	border: 1px solid #ccc;
}

/* List.jsp */
main.list table#boardList {
	border: 1px solid #ccc;
	border-collapse: collapse;
	width: 680px;
	border-left: none;
	border-right: none;
}
table#boardList th, table#boardList td {
	border: 1px solid #ccc;
	padding: 6px;
	border-left: none;
	border-right: none;
	text-align: center;
}
table#boardList tr.trHover:hover {
	cursor: pointer;
	background-color: #eee;
} 


Categories:

JSP/Servlet