공부/JSP

[JSP] 쉽게 배우는 JSP 웹 프로그래밍 4장 연습문제

thegreatjy 2020. 9. 29. 00:00
728x90

4번

(1) forward.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>forward</title>
</head>
<body>
    <h4>구구단 출력하기</h4>
    <jsp:forward page="forward_data.jsp">
        <jsp:param name="num" value="5" />
    </jsp:forward>
</body>
</html>
cs

(2) forward_data.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>forward_data</title>
</head>
<body>
    <%
    int num=Integer.parseInt(request.getParameter("num"));
    for(int i=1;i<10;i++){
        out.println(num+"*"+i+"="+num*i+"<br>");
    }
    %>
</body>
</html>
cs

 

5번

(1) include.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <h4>구구단 출력하기</h4>
    <jsp:include page="include_data.jsp">
        <jsp:param name="num" value="5" />
    </jsp:include>
 
</body>
</html>
cs

(2) include_data.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <%
    int num=Integer.parseInt(request.getParameter("num"));
    for(int i=1;i<10;i++){
        out.println(num+"*"+i+"="+num*i+"<br>");
    }
    %>
    
</body>
</html>
cs

 

6번

(1) src폴더에 ch04.com.dao 패키지에 GuGuDan 클래스, process() 메소드 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package ch04.com.dao;
 
public class GuGuDan {
    //private String result="";
    
    public GuGuDan() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    //n단 문자열을 리턴한다.
    public String process(int n) {    
        StringBuffer result=new StringBuffer();
        for(int i=1;i<10;i++) {
            result.append(n+"*"+i+"="+n*i+"<br>");
        }
        return result.toString();
    }
}
 
cs

(2) useBean.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <jsp:useBean id="mult" class="ch04.com.dao.GuGuDan"/>
    <h4>구구단 출력하기</h4>
    <%
        String str=mult.process(5);
        out.println(str);
    %>
 
</body>
</html>
cs

 

7번

(1) src 폴더에 dto 패키지, Book 클래스 생성

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
package dto;
 
public class Book {
    private String bookId;
    private String name;
    private Integer UnitPrice;
    private String author;
    private String description;
    private String publisher;
    private String category;
    private long unitsInStock;
    private long totalPages;
    private String releaseDate;
    private String condition;
    
    public Book() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public Book(String bookId, String name, Integer unitPrice) {
        this.bookId=bookId;
        this.name=name;
        this.UnitPrice=unitPrice;
    }
 
    public String getAuthor() {
        return author;
    }
 
    public void setAuthor(String author) {
        this.author = author;
    }
 
    public String getBookId() {
        return bookId;
    }
 
    public void setBookId(String bookId) {
        this.bookId = bookId;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Integer getUnitPrice() {
        return UnitPrice;
    }
 
    public void setUnitPrice(Integer unitPrice) {
        UnitPrice = unitPrice;
    }
 
    public String getDescription() {
        return description;
    }
 
    public void setDescription(String desciption) {
        this.description = desciption;
    }
 
    public String getPublisher() {
        return publisher;
    }
 
    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }
 
    public String getCategory() {
        return category;
    }
 
    public void setCategory(String category) {
        this.category = category;
    }
 
    public long getUnitsInStock() {
        return unitsInStock;
    }
 
    public void setUnitsInStock(long unitsInStock) {
        this.unitsInStock = unitsInStock;
    }
 
    public long getTotalPages() {
        return totalPages;
    }
 
    public void setTotalPages(long totalPages) {
        this.totalPages = totalPages;
    }
 
    public String getReleaseDate() {
        return releaseDate;
    }
 
    public void setReleaseDate(String releaseDate) {
        this.releaseDate = releaseDate;
    }
 
    public String getCondition() {
        return condition;
    }
 
    public void setCondition(String condition) {
        this.condition = condition;
    }
    
    
}
cs

참고로 이 부분은 이클립스 윈도우에 Source>Generate Getters and Setters & Source>Generate Constructors from superclass 를 통해 소스 작성이 가능하다. (처음 앎 ㅎ)

 

(2) src 폴더에 dao 패키지, BookRepository 클래스 생성

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
package dao;
import java.util.ArrayList;
import dto.Book;
import dto.Product;
 
public class BookRepository {
    ArrayList<Book> listOfBooks=new ArrayList<Book>();
 
    public BookRepository() {
        Book b1=new Book("1""[Hello Coding] HTML5+CSS3"15000);
        b1.setDescription("문서 작성만큼, 아니 그보다 더 쉬운 웹페이지 만들기, 초보자를 위한 맞춤형 입문서. 지금 당장 컴퓨터가 없어도 괜찮다. 코드와 실행 화면이 바로 보여서 눈으로만 읽어도 어떻게 작동하는지 쉽게 파악할 수 있는 것은 기본이고, 중간중간 퀴즈를 추가하여 재미있게 게임하듯 복습할 수 있다.");
        b1.setAuthor("황재호");
        b1.setPublisher("한빛미디어");
        
        Book b2=new Book("2""[IT 모바일] 쉽게 배우는 자바 프로그래밍"27000);
        b2.setDescription("객체 지향의 핵심과 자바의 현대적 기능을 충실히 다루면서도 초보자가 쉽게 학습할 수 있게 구성한 교재이다. 시각화 도구를 활용한 개념 설명과 군더더기 없는 핵심 코드를 통해 개념과 구현을 한 흐름으로 학습할 수 있다.");
        b2.setAuthor("우종중");
        b2.setPublisher("한빛미디어");
        
        Book b3=new Book("3""[IT 모바일] 스프링4 입문"27000);
        b3.setDescription("그림과 표로 쉽게 배우는 스프링 4 입문서. 스프링은 단순히 사용 방법만 익히는 것보다 아키텍처를 어떻게 이해하고 설계하는지가 더 중요하다.");
        b3.setAuthor("하세가와 유이치, 오오노 와타루, 토키 코헤이(권은철, 전민수)");
        b3.setPublisher("한빛미디어");
        
        listOfBooks.add(b1);
        listOfBooks.add(b2);
        listOfBooks.add(b3);
    }
    
    
    public ArrayList<Book> getAllProducts(){
        return listOfBooks;
    }
}
 
cs

(3) books.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
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="EUC-KR"%>
    <%@ page import ="java.util.ArrayList" %>
<%@ page import="dto.Book" %>
<jsp:useBean id="bookDAO" class="dao.BookRepository" scope="session"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<body>
<%@ include file="menu.jsp" %>
    <div class="jumbotron">
      <div class="container">
      <h1 class = "display-3">도서 목록</h1>
      </div>
    </div>
    
    <%
        ArrayList<Book> listOfProducts=bookDAO.getAllProducts();
    %>
    
    <div class="container">
        <%
            for(int i=0;i<listOfProducts.size();i++){
                Book book=listOfProducts.get(i);
        %>
      <div class="row" align="left">
        <div class="col">
            <h3><%=book.getName()%></h3>
            <p><%=book.getDescription()%></p>
            <p><%=book.getAuthor()+" | "+book.getPublisher()+" | "+book.getUnitPrice()%></p>
        </div>
      </div>
      <hr style="border:grey 1px dashed">
          <%
            }
        %>
    </div>
 
<%@ include file="footer.jsp" %>
 
</body>
</html>
cs

 

잘못된 부분은 댓글로 알려주세요!

쉽게 배우는 jsp 웹 프로그래밍 4장 연습문제

728x90