"<td><a href='"+request.getContextPath()+"/boderView?wno="+bvo.getNum()+"'>" + bvo.getNum() + "</td>"
게시글 번호를 클릭하면 해당 게시글의 상세내용을 보기위해 링크를 걸어주었다.
게시글 테이블의 PK는 글번호이기 때문에 wno라는 변수에 글번호를 넘겨주고 boderview 서블릿으로 넘어간다.
boderview 서블릿의 doGet 메서드
private boardDAO bdao=new boardDAO() ;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String wno=request.getParameter("wno"); //jsp로부터 넘겨받은 글번호
ArrayList<boardVO> clist=new ArrayList<>(); //게시글에 달린 댓글들을 가져오기 위한 ArrayList
clist=bdao.selectC(wno); //게시글에 달린
boardVO data=bdao.selectOne(wno);
}
DAO객체에서 selectOne 메서드를 사용하여 게시글의 상세내용을 가지고 와 주었고 selectC 메서드로 해당 글의 댓글들을 List로 가지고 와 주었다.
selectOne 메서드
public boardVO selectOne(String num) {
// TODO Auto-generated method stub
ResultSet rs=null;
if(connect()) {
String sql="select * from qa where num="+num;
try {
Statement st =conn.createStatement();
rs=st.executeQuery(sql);
if(rs.next()) {
boardVO bvo=new boardVO();
bvo.setNum(rs.getInt("num"));
bvo.setName(rs.getString("name"));
bvo.setChkbox(rs.getString("chkbox"));
bvo.setIndate(rs.getString("indate"));
bvo.setTitle(rs.getString("title"));
bvo.setContent(rs.getString("content"));
return bvo;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
selectC
public ArrayList<boardVO> selectC(String wno) {
// TODO Auto-generated method stub
ArrayList<boardVO> clist=new ArrayList<>();
ResultSet rs=null;
if(connect()) {
String sql="select * from comm where num=?";
try {
PreparedStatement psmt =conn.prepareStatement(sql);
psmt.setInt(1, Integer.parseInt(wno));
rs=psmt.executeQuery();
while(rs.next()) {
boardVO bvo=new boardVO();
bvo.setId(rs.getString("id"));
bvo.setName(rs.getString("name"));
bvo.setContent(rs.getString("content"));
bvo.setIndate(rs.getString("indate"));
clist.add(bvo);
}
conn.close();
return clist;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
boderview 서블릿의 doGet 메서드
String url="bbs/view.jsp";
RequestDispatcher dispatcher=request.getRequestDispatcher(url);
request.setAttribute("board", data); //board라는 변수에 게시글 상세정보를 저장
request.setAttribute("comment", clist); comment라는 변수에 댓글목록을 저장
dispatcher.forward(request, response);
그렇게 받아온 정보들을 setAttribute로 저장해주고 view.jsp 파일로 넘어간다.
EL (Expression Language)
<%= %> , out.println()과 같은 자바코드를 더 이상 사용하지 않고 좀더 간편하게 출력을 지원하기 위한 도구.
배열이나 컬렉션에서도 사용되고, JavaBean의 프로퍼티에서도 사용됩니다.
-> ${ }
- JSP가 실행될 때 즉시 반영된다.
- 객체 프로퍼티 값을 꺼낼때 주로 사용
-> #{ }
- 시스템에서 필요하다고 판단될 때 그 값을 사용한다.
- 사용자 입력값을 객체의 프로퍼티에 담는 용도로 주로 사용
${ }를 사용하여 게시물의 상세내용을 가지고 와 주었다.
게시글 상세 내용
<table>
<tr>
<th colspan="1">글번호</th>
<td colspan="3">${board.getNum()}</td>
</tr>
<tr>
<th colspan="1">작성자</th>
<td colspan="1">${board.getName()}</td>
<th colspan="1">비밀글</th>
<td colspan="1">${board.getChkbox()}</td>
</tr>
<tr>
<th colspan="1">작성일</th>
<td colspan="3">${board.getIndate()}</td>
</tr>
<tr>
<th colspan="1">제목</th>
<td colspan="3">${board.getTitle()}</td>
</tr>
<tr>
<th colspan="1">내용</th>
<td colspan="3">${board.getContent()}</td>
</tr>
</table>
댓글 가져오기
<table>
<tr>
<th colspan="4">댓글</th>
</tr>
<tr>
<td colspan="1">ID</td>
<td colspan="1">이름</td>
<td colspan="1">댓글</td>
<td colspan="1">작성일자</td>
</tr>
<%
ArrayList<boardVO> clist=(ArrayList)request.getAttribute("comment");
if(clist.size()==0){
out.print("<tr class='record'>");
out.print("<td colspan='4'>등록된 댓글이 없습니다.</td>");
out.print("</tr>");
}else{
for(int i=0; i<clist.size(); i++){
boardVO bvo=clist.get(i);
out.print("<tr class='record'>");
out.print("<td>"+bvo.getId()+"</td>");
out.print("<td>"+bvo.getName()+"</td>");
out.print("<td>"+bvo.getContent()+"</td>");
out.print("<td>"+bvo.getIndate()+"</td>");
out.print("</tr>");
}
}
%>
</table>
게시글 상세 내용은 ${}로 가지고 와 주었고, 댓글 목록은 <%= %>로 가지고 와 주었다.
EL 표기법이 <%=%> 보다 훨씬 간편하고 보기 쉽게 코드를 작성할 수 있다.