GuestbookListServlet.java
out.println("<hr width='600' align='left' color='red'/>");
총글수 : 8
페이지 수 = (총글수 + 2) / 3
[1] [2] [3]
총글수 : 13
페이지 수 [1] [2] [3] [4] [5]
총글수 : 9
페이지 수 [1] [2] [3]
GuestbookListServlet.java
int totalA = guestbookDAO.getTotal(); //총글수
int totalP = (totalA + 2) / 3;
GuestbookDAO.java
총글수 구하기위한 getTotal 함수 생성하기
★ totalA = rs.getInt(1); 1번 칼럼에 있는 값을 가져와라
public int getTotal() {
int totalA = 0;
getConnection();
String sql = "select count(*) from guestbook";
try {
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()) {
totalA = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return totalA;
}
rs.next() -- 이거 반드시 있어야 한다 !!!!
현재 포지션의 위치를 맞춰서 레코드가 있는지 없는지 확인하고 그 다음에 컬럼의 값을 꺼내올 수 있다.
if(rs.next()) {
totalA = rs.getInt(1);
}
밑에처럼만 적어줘도 된다. 하지만 반드시 있어야한다는 거 확인 !!!!
rs.next();
totalA = rs.getInt(1);
int totalA = guestbookDAO.getTotal(); //총글수
int totalP = (totalA + 2) / 3;
System.out.println(totalA + ", " + totalP);
if(list != null) {
for(int i=1; i<totalP; i++) {
out.println("[ " + i + " ]");
}
out.println("<br><br>");
이제 숫자누르면 이동하게 링크를 걸어줘야한다.
if(list != null) {
for(int i=1; i<totalP; i++) {
out.println(" ");
out.println("[ <a href=''> " + i + "</a> ]");
}
out.println("<br><br>");
http://localhost:8080/guestbookServlet/list?pg=1
/guestbookServlet/list?pg=1
http://localhost:8080/guestbookServlet/list?pg=2
/guestbookServlet/list?pg=2
if(list != null) {
for(int i=1; i<totalP; i++) {
out.println("[ <a href='/guestbookServlet/list?pg=" + i + "'>" + i + "</a> ]");
}
out.println("<br><br>");
현재페이지와 번호가 같을 때 style을 걸어서 빨간색 글씨와 밑줄을 쳐주는 작업을한다.
즉, 클릭한 페이지가 빨간글씨에 밑줄이 쳐지게 하고
클릭한 페이지가 아니면 검정글씨에 밑줄 없어지게 한다.
//응답
response.setContentType("text/html; charset=UTF-8");;
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<style>");
out.println("#currentPaging { color: red; text-decoration: underline;}");
out.println("#paging { color: black; text-decoration: none; }");
out.println("</style>");
out.println("<body>");
if(list != null) {
out.println(" ");
for(int i=1; i<totalP; i++) {
if(i == pg)
out.println("[ <a id='currentPaging' href='/guestbookServlet/list?pg=" + i + "'>" + i + "</a> ]");
else
out.println("[ <a id='paging' href='/guestbookServlet/list?pg=" + i + "'>" + i + "</a> ]");
}
out.println("<br><br>");
GuestbookListServlet.java 전체코드
package guestbook.service;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import guestbook.bean.GuestbookDTO;
import guestbook.dao.GuestbookDAO;
@WebServlet("/list")
public class GuestbookListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//데이터
int pg =Integer.parseInt(request.getParameter("pg"));
//1페이지당 3개씩
int endNum = pg * 3;
int startNum = endNum - 2;
// DB에서 데이터 가져오기
GuestbookDAO guestbookDAO = GuestbookDAO.getInstance(); // 싱글톤
List<GuestbookDTO> list = guestbookDAO.guestbookList(startNum, endNum); //부모 = 자식
int totalA = guestbookDAO.getTotal(); //총글수
int totalP = (totalA + 2) / 3;
//System.out.println(totalA + ", " + totalP);
//응답
response.setContentType("text/html; charset=UTF-8");;
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<style>");
out.println("#pagingContainer { text-align: center; margin-top: 20px; }");
out.println("hr { width: 610px; align: left; color: red; margin: 20px auto; }");
out.println("#currentPaging { text-align: center; color: red; text-decoration: underline;}");
out.println("#paging { color: black; text-decoration: none; }");
out.println("</style>");
out.println("<body>");
if(list != null) {
out.println("<div id='pagingContainer'>");
for(int i=1; i<totalP; i++) {
if(i == pg)
out.println("[ <a id='currentPaging' href='/guestbookServlet/list?pg=" + i + "'>" + i + "</a> ]");
else
out.println("[ <a id='paging' href='/guestbookServlet/list?pg=" + i + "'>" + i + "</a> ]");
}
out.println("</div>");
for(GuestbookDTO guestbookDTO : list) {
out.println("<table border='1' style='border-collapse: collapse; margin: 20px auto;'>");
out.println("<tr>");
out.println("<th width='100'>작성자</th>");
out.println("<td width='150' align='center'>" + guestbookDTO.getName() + "</td>");
out.println("<th width='100'>작성일</th>");
out.println("<td width='250' align='center'>" + guestbookDTO.getLogtime() + "</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<th>이메일</th>");
out.println("<td colspan='3'>" + guestbookDTO.getEmail() + "</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<th>홈페이지</th>");
out.println("<td colspan='3'>" + guestbookDTO.getHomepage() + "</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<th>제목</th>");
out.println("<td colspan='3'>" + guestbookDTO.getSubject() + "</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td colspan='4' style='white-space: pre-wrap; word-wrap: break-word;'>" + guestbookDTO.getContent() + "</td>");
out.println("</tr>");
out.println("</table>");
out.println("<hr color=red />");
}
}
out.println("</body>");
out.println("</html>");
}
}
'JSP & Servlet' 카테고리의 다른 글
DAY 46 JSP - Connection Pool / 로그인 / 쿠키(2024.09.05) (1) | 2024.09.06 |
---|---|
DAY 47 - 미니프로젝트(member) - 세션 / 회원정보 수정 / 글 작성 (2024.09.06) (2) | 2024.09.06 |
DAY 45 JSP - 주석 / 미니프로젝트(member) - 중복체크 ( 2024.09.04 ) (4) | 2024.09.04 |
DAY 44 - Servlet - Guestbook ( 2024.09.03 ) (0) | 2024.09.04 |
DAY 43 - Servlet (2024.09.02) (0) | 2024.09.02 |