반응형
servlet --> data 가공 --> request.setAttribute
(controller 생성. 가상경로 매핑) (model)
--> jsp foward --> request.getAttribute --> HTML 출력
(view)
NoticeBoardVO.java
package jspservlet.vo;
public class NoticeBoardVO /* extends UserVO */ {
private String nno;
private String uno;
private String title;
private String content;
private String rdate;
private String hit;
private String state;
private String top_yn;
private String filename;
private String uid;
public NoticeBoardVO() {}
public NoticeBoardVO(String nno, String uno, String title, String content, String rdate, String hit, String state,
String top_yn, String filename, String uid) {
this.nno = nno;
this.uno = uno;
this.title = title;
this.content = content;
this.rdate = rdate;
this.hit = hit;
this.state = state;
this.top_yn = top_yn;
this.filename = filename;
this.uid = uid;
}
public String getNno() { return nno; }
public String getUno() { return uno; }
public String getTitle() { return title; }
public String getContent() { return content; }
public String getRdate() { return rdate; }
public String getHit() { return hit; }
public String getState() { return state; }
public String getTop_yn() { return top_yn; }
public String getFilename() { return filename; }
public String getUid() { return uid; }
public void setNno(String nno) { this.nno = nno; }
public void setUno(String uno) { this.uno = uno; }
public void setTitle(String title) { this.title = title; }
public void setContent(String content) { this.content = content; }
public void setRdate(String rdate) { this.rdate = rdate; }
public void setHit(String hit) { this.hit = hit; }
public void setState(String state) { this.state = state; }
public void setTop_yn(String top_yn) { this.top_yn = top_yn; }
public void setFilename(String filename) { this.filename = filename; }
public void setUid(String uid) { this.uid = uid; }
}
BoardRealListController.java
package jspservlet.controller;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
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 jspservlet.util.DBConn;
import jspservlet.vo.*;
/**
* Servlet implementation class BoardRealListController
*/
@WebServlet("/board/realList.do")
public class BoardRealListController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BoardRealListController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = null; //DB 연결
PreparedStatement psmt = null; //SQL 등록 및 실행. 보안이 더 좋음!
ResultSet rs = null; //조회 결과를 담음
List<NoticeBoardVO> boardList = new ArrayList<>();
//try 영역
try{
conn = DBConn.conn();
String sql = "select nno,title,uid,n.uno,content,filename, "
+ "date_format(n.rdate,'%Y-%m-%d') as rdate,n.state,hit,top_yn "
+ "from notice_board n "
+ "inner join user u "
+ "on n.uno = u.uno ";
sql += " where n.state='E' ";
sql += "order by top_yn desc, nno desc ";
psmt = conn.prepareStatement(sql);
rs = psmt.executeQuery();
while(rs.next()){
NoticeBoardVO board = new NoticeBoardVO(
rs.getString("nno"),
rs.getString("uno"),
rs.getString("title"),
rs.getString("content"),
rs.getString("rdate"),
rs.getString("hit"),
rs.getString("state"),
rs.getString("top_yn"),
rs.getString("filename"),
rs.getString("uid")
);
boardList.add(board);
}
request.setAttribute("boardList", boardList);
request.getRequestDispatcher("/board/realList.jsp").forward(request, response);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
DBConn.close(rs, psmt, conn);
}catch(Exception e) {
e.printStackTrace();
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
realList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="jspservlet.vo.*" %>
<%
List<NoticeBoardVO> boardList = (List<NoticeBoardVO>)request.getAttribute("boardList");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시글 목록</title>
</head>
<style>
td{
text-align:center;
}
</style>
<body>
<h2>게시글 목록 페이지</h2><hr>
<table>
<thead>
<tr>
<th>글번호</th>
<th>제목</th>
<th>작성자</th>
<th>작성일</th>
<th>조회수</th>
</tr>
</thead>
<tbody>
<!-- controller에서 가져온 게시글 목록을 출력하세요
힌트! controller에서 가져온 DB notice_board 테이블 데이터를
List로 만들어 model에 담아 전달
-->
<%
int num = boardList.size();
for(NoticeBoardVO n : boardList){
%>
<tr>
<td width="60px;" style="color:<%= n.getTop_yn().equals("Y") ? "red" : "black" %>";>
<%
if(n.getTop_yn().equals("Y")){
%>★<%
num--;
}else{
%>
<%= num-- %>
<%
}
%>
</td>
<td style="color:<%= n.getTop_yn().equals("Y") ? "red" : "black" %>";>
<%= n.getTitle() %>
</td>
<td>
<%= n.getUid() %>
</td>
<td>
<%= n.getRdate() %>
</td>
<td width="60px;">
<%= n.getHit() %>
</td>
</tr>
<%
}
%>
</tbody>
</table>
</body>
</html>
반응형
'Java' 카테고리의 다른 글
[Servlet] 게시글 수정 (0) | 2024.10.13 |
---|---|
[Servlet] 공지게시글 상세페이지 조회 (0) | 2024.10.12 |
[Servlet] 로그인한 회원의 회원 정보 조회 (0) | 2024.10.10 |
[Servlet] 로그인 처리 (0) | 2024.10.09 |
[JSP] 회원정보 조회하고 수정하기 (0) | 2024.10.08 |