JSP 페이징 처리 알고리즘

JSP 페이징 처리 알고리즘


Bean 코드
BoardDBBean.java의 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package ch12.board;

import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.util.*;

public class BoardDBBean {
private static BoardDBBean instance = new BoardDBBean();

// .jsp페이지에서 DB연동빈인 BoardDBBean클래스의 메소드에 접근시 필요
public static BoardDBBean getInstance() {
return instance;
}

private BoardDBBean() {
}

// 커넥션풀로부터 Connection객체를 얻어냄 : DB연동빈의 쿼리문을 수행하는 메소드에서 사용
private Connection getConnection() throws Exception {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/jsptest");
return ds.getConnection();
}

// board테이블에 저장된 전체글의 수를 얻어냄(select문)<=list.jsp에서 사용
public int getArticleCount() throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int x = 0;
try {
conn = getConnection();
pstmt = conn.prepareStatement("select count(*) from board");
rs = pstmt.executeQuery();
if (rs.next()) {
x = rs.getInt(1);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException ex) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException ex) {
}
if (conn != null)
try {
conn.close();
} catch (SQLException ex) {
}
}
return x;
}

// 글의 목록(복수개의 글)을 가져옴(select문)<=list.jsp
public List getArticles(int start, int end) throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List articleList = null;// 글목록을 저장하는 객체
try {
conn = getConnection();
pstmt = conn
.prepareStatement("select * from board order by ref desc, re_step asc limit ?,? ");
pstmt.setInt(1, start - 1);
// mysql에서 첫번째레코드의 시작번호가 0부터 시작함, 따라서 화면표시할 글번호보다 하나 작다
pstmt.setInt(2, end);// 추출할 레코드의 개수
rs = pstmt.executeQuery();// ResultSet을 반환
if (rs.next()) {// ResultSet이 레코드를 가짐
articleList = new ArrayList(end);
// 몇개의 객체를 저장할지 크기를 지정해서 객체를 생성 =>객체저장소
do {// 134~150라인: ResultSet에 있는 레코드수 만큼 반복수행
BoardDataBean article = new BoardDataBean();// 레코드 하나당
article.setNum(rs.getInt("num"));// ResultSet으로부터 하나의
article.setWriter(rs.getString("writer"));// 레코드의 각각의
article.setEmail(rs.getString("email"));// 필드값을 얻어내서
article.setSubject(rs.getString("subject"));// JSP페이지에서
article.setPasswd(rs.getString("passwd"));// 사용하기위해서
article.setReg_date(rs.getTimestamp("reg_date"));// 자바빈객체에
// 값을 저장
article.setReadcount(rs.getInt("readcount"));
article.setRef(rs.getInt("ref"));
article.setRe_step(rs.getInt("re_step"));
article.setRe_level(rs.getInt("re_level"));
article.setContent(rs.getString("content"));
article.setIp(rs.getString("ip"));
// List객체에 데이터저장빈인 BoardDataBean객체를 저장
articleList.add(article);
} while (rs.next());
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (SQLException ex) {
}
if (pstmt != null)
try {
pstmt.close();
} catch (SQLException ex) {
}
if (conn != null)
try {
conn.close();
} catch (SQLException ex) {
}
}
return articleList;// List객체의 레퍼런스를 리턴
}
}

list.jsp 페이지

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//선언부
<%@ page contentType = "text/html; charset=euc-kr" %><%@ page import = "ch12.board.BoardDBBean" %>
<%@ page import = "ch12.board.BoardDataBean" %>
<%@ page import = "java.util.List" %>
<%@ page import = "java.text.SimpleDateFormat" %>
<%@ include file="/view/color.jsp"%>
<%!
int pageSize = 10;//한페이지에 표시할 글의 수
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); //날짜데이터 표시형식지정
%>
<%
String pageNum = request.getParameter("pageNum");//화면에 표시할 페이지번호
int count = 0;//전체글의 수
int number=0;//현재페이지에 표시할 레코드수
int currentPage = Integer.parseInt(pageNum);//pageNum변수값을 숫자로 파싱
if (pageNum == null) {//페이지번호가 없으면
pageNum = "1";//1페이지의 내용이 화면에 표시
}
List articleList = null;//글목록을 저장
BoardDBBean dbPro = BoardDBBean.getInstance();
count = dbPro.getArticleCount();//전체글수 얻어냄
if(count == (currentPage-1)*pageSize){
currentPage -=1;
String listPage= "list.jsp?pageNum="+currentPage;
response.sendRedirect(listPage);
}
int startRow = (currentPage - 1) * pageSize + 1;//현재 페이지에서의 시작글번호
int endRow = currentPage * pageSize;//현재 페이지에서의 마지막글번호
if (count>0) {//테이블에 저장된 글이 있으면
articleList = dbPro.getArticles(startRow, pageSize);//테이블에 글목록을 가져옴
}
if(articleList.isEmpty()){
out.println("a");
currentPage-=1;
response.sendRedirect("list.jsp?pageNum=currentPage");
}
number=count-(currentPage-1)*pageSize;
%>

//페이징 처리부
<%
if (count>0) { //count가 0보다 크다면 실행 글이 하나라도 저장되 있다면
int pageCount = count / pageSize + ( count % pageSize == 0 ? 0 : 1);
int startPage =1;
if(currentPage % 10 != 0){
startPage = (int)(currentPage/10)*10 + 1;
}else{
startPage = ((int)(currentPage/10)-1)*10 + 1;
}
int pageBlock=10;
int endPage = startPage + pageBlock-1;
if (endPage>pageCount) endPage = pageCount;
if (startPage>10) { %><a href="list.jsp?pageNum=<%= startPage - 10 %>">[이전]</a><% }
for (int i = startPage ; i<= endPage ; i++) { %><a href="list.jsp?pageNum=<%= i %>">[<%= i %>]</a><%
}
if (endPage<pageCount) { %><a href="list.jsp?pageNum=<%= startPage + 10 %>">[다음]</a><%
}
}
%>

Share