티스토리 뷰

프로젝트 구조 




ArticleVO.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
package com.ktds.smahn.bbs.vo;
 
public class ArticleVO {
    
    private int articleNumber;
    private String articleName;
    private int hitCount;
 
    public ArticleVO(int articleNumber, String articleName, int hitCount) {
        this.articleNumber = articleNumber;
        this.articleName = articleName;
        this.hitCount = hitCount;
    }
 
    public String getArticleName() {
        return articleName;
    }
 
    public void setArticleName(String articleName) {
        this.articleName = articleName;
    }
 
    public int getArticleNumber() {
        return articleNumber;
    }
 
    public void setArticleNumber(int articleNumber) {
        this.articleNumber = articleNumber;
    }
 
    public int getHitCount() {
        return hitCount;
    }
 
    public void setHitCount(int hitCount) {
        this.hitCount = hitCount;
    }
}
 
 
cs


BBSServlet.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
 
package com.ktds.smahn.bbs.web;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.ktds.smahn.bbs.vo.ArticleVO;
 
/**
 * Servlet implementation class BBSServlet
 */
public class BBSServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BBSServlet() {
        super();
        
    }
 
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        this.doPost(request, response);
    }
 
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        List<ArticleVO> articleList = new ArrayList<ArticleVO>();
        ArticleVO articleVO = null;
        
        for ( int i = 0; i < 10; i++) {
            articleVO = new ArticleVO( (i + 1), "제목" + (i + 1), new Random().nextInt(100) );
            articleList.add(articleVO);
 
        }
        
        request.setAttribute("articleList", articleList);
        
        // 결과 보여줄 view(.jsp) 선택
        RequestDispatcher rd = request.getRequestDispatcher("/view/jsp/bbs/list.jsp");
        rd.forward(request, response);
        
        
    }
 
}
 
cs


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
 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    안녕하세요<br/>
    
    총 ${ articleList.size() } 개의 게시글이 등록되어 있습니다.<br/>
    
    <table    border="1">
        <tr>
            <th> 글 번호 </th>
            <th> 제목 </th>
            <th> 조회수 </th>
        </tr>    
        
        <c:if test="${ not empty articleList }">
            <c:forEach var="article" items="${ articleList }">
                <tr>            
                <!-- 필드명만 잘 적으면 알아서 Getter 이용해서 데이터를 가져온다. -->    
                    <td>${ article.articleNumber }</td>                 
                    <td>${ article.articleName }</td>
                    <td>${ article.hitCount }</td>
                </tr>
            </c:forEach>
        </c:if>
        <c:if test='${ empty articleList }'>        
            <tr>
                <td colspan="3">데이터가 없습니다.</td>            
            </tr>
        </c:if>
        
    </table>
    
</body>
</html>
cs


web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>AD_SampleBBS</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>BBSServlet</display-name>
    <servlet-name>BBSServlet</servlet-name>
    <servlet-class>com.ktds.smahn.bbs.web.BBSServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>BBSServlet</servlet-name>
    <url-pattern>/list</url-pattern>
  </servlet-mapping>
</web-app>
cs



결과


'프로그래밍 > JSP' 카테고리의 다른 글

CSS 기초  (0) 2016.03.03
JSTL 다운로드 및 세팅  (0) 2016.02.05
Servlet  (0) 2016.02.03
코드 리뷰하는 방법, jsp 규칙, 색상찾는 사이트  (0) 2016.02.03
[4] jsp를 이용해서 심플 게시판 만들기  (0) 2016.02.02
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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
글 보관함