본문 바로가기
공부/JSP

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

by thegreatjy 2020. 10. 13.
728x90

4번

(1) form01.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
 
<title>Insert title here</title>
</head>
<body>
    <form action="form1_process.jsp" name="form1" method="post">
        <p>이름 : <input type="text" name="name">
        <p>주소 : <input type="text" name="address">
        <p>이메일 : <input type="text" name="email">
        <p><input type="submit" value="전송" >
    </form>
</body>
</html>
cs

 

(2) form01_process.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
 
<title>Insert title here</title>
</head>
<body>
    <%
        request.setCharacterEncoding("UTF-8");
        StringBuffer name = new StringBuffer(request.getParameter("name"));
        StringBuffer address = new StringBuffer(request.getParameter("address"));
        StringBuffer email = new StringBuffer(request.getParameter("email"));
        
        out.println("이름 : "+name+"<br>");
        out.println("주소 : "+address+"<br>");
        out.println("이메일 : "+email+"<br>");
    %>
</body>
</html>
cs

 

5번

(1) form02.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
 
<title>Insert title here</title>
</head>
<body>
    <form action="form2_process.jsp" name="form1" method="post">
        <p>이름 : <input type="text" name="name">
        <p>주소 : <input type="text" name="address">
        <p>이메일 : <input type="text" name="email">
        <p><input type="submit" value="전송" >
    </form>
</body>
</html>
cs

 

(2) form02_process.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@page import="java.io.*, java.util.*"%>
<!DOCTYPE html>
<html>
<head>
 
<title>Insert title here</title>
</head>
<body>
    <%
        request.setCharacterEncoding("UTF-8");
    
        Enumeration param=request.getParameterNames();
        
        while(param.hasMoreElements()){
            StringBuffer name=new StringBuffer((String)param.nextElement());
            StringBuffer value=new StringBuffer(request.getParameter((name.toString())));
            out.println(name+" : "+value+"<br>");
        }
    %>
</body>
</html>
cs

 

6번

(1) form03.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
 
<title>Insert title here</title>
</head>
<body>
    <form action="form3_process.jsp" name="form3" method="post">
        오렌지<input type="checkbox" name="fruits" value="Orange" >
        사과<input type="checkbox" name="fruits" value="Apple">
        바나나<input type="checkbox" name="fruits" value="Banana">
        <input type="submit" value="전송" >
    </form>
</body>
</html>
cs

 

(2) form03_process.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=utf-8"%>
<!DOCTYPE html>
<html>
<head>
 
<title>Insert title here</title>
</head>
<body>
    <h1>선택한 과일</h1>
    <%
        request.setCharacterEncoding("UTF-8");
        String[] fruit=request.getParameterValues("fruits");
        for(int i=0;i<fruit.length;i++){
            out.print(fruit[i]+" ");
        }
    %>
</body>
</html>
cs

 

7번

(1) addBook.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
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
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
 
<title>Insert title here</title>
</head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="./resources/js/validation.js"></script>
<body>
    <jsp:include page="menu.jsp"/>
    <div class="jumbotron">
          <div class="container">
                  <h1 class = "display-3">도서 등록</h1>
          </div>
    </div>
    <div class="container">
        <form name="newBook" action="./processAddBook.jsp" class="form-horiontal" method="post">
            <div class="form-group row">
                <label class="col-sm-2">도서코드</label>
                <div class="col-sm-3">
                    <input type="text" name="bookId" class="form-control">
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2">도서명</label>
                    <div class="col-sm-3">
                        <input type="text" name="name" class="form-control">
                    </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2">가격</label>
                    <div class="col-sm-3">
                        <input type="text" name="unitPrice" class="form-control">
                    </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2">저자</label>
                    <div class="col-sm-3">
                        <input type="text" name="author" class="form-control">
                    </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2">출판사</label>
                    <div class="col-sm-3">
                        <input type="text" name="publisher" class="form-control">
                    </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2">출판일</label>
                    <div class="col-sm-3">
                        <input type="text" name="releaseDate" class="form-control">
                    </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2">총 페이지 수</label>
                    <div class="col-sm-3">
                        <input type="text" name="totalPages" class="form-control">
                    </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2">상세정보</label>
                    <div class="col-sm-5">
                        <textarea name="description" cols="50" rows="2" class="form-control"></textarea>
                    </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2">분류</label>
                    <div class="col-sm-3">
                        <input type="text" name="category" class="form-control">
                    </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2">재고 수</label>
                    <div class="col-sm-3">
                        <input type="text" name="unitsInStock" class="form-control">
                    </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2">상태</label>
                    <div class="col-sm-5">
                        <input type="radio" name="condition" value="New"> 신규 도서
                        <input type="radio" name="condition" value="Old"> 중고 도서
                        <input type="radio" name="condition" value="E-Book"> E-book
                    </div>
            </div>
            <div class="form-group row">
                <div class="col-sm-offset-2 col-sm-10">
                        <input type="submit" value="등록" class="btn btn-primary">
                </div>
            </div>
        </form>
    </div>
</body>
</html>
cs

 

(2) processAddBook.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="dto.Book" %>
<%@ page import="dao.BookRepository" %>
<!DOCTYPE html>
<html>
<head>
 
<title>Insert title here</title>
</head>
<body>
    <%
    request.setCharacterEncoding("UTF-8");
    
    String bookId=request.getParameter("bookId");
    String name=request.getParameter("name");
    String unitPrice=request.getParameter("unitPrice");
    String author=request.getParameter("author");
    String description=request.getParameter("description");
    String publisher=request.getParameter("publisher");
    String category=request.getParameter("category");
    String unitsInStock=request.getParameter("unitsInStock");
    String totalPages=request.getParameter("totalPages");
    String releaseDate=request.getParameter("releaseDate");
    String condition=request.getParameter("condition");
    
    Integer price;
    
    if(unitPrice.isEmpty())
            price=0;
    else    price=Integer.valueOf(unitPrice);
    
    long stock;
    
    if(unitsInStock.isEmpty())
            stock=0;
    else    stock=Long.valueOf(unitsInStock);
    
    long pages;
    
    if(totalPages.isEmpty())
            pages=0;
    else    pages=Long.valueOf(totalPages);
    
    BookRepository dao=BookRepository.getInstance();
    
    Book newProduct=new Book();
    
    newProduct.setBookId(bookId);
    newProduct.setName(name);
    newProduct.setUnitPrice(price);
    newProduct.setAuthor(author);
    newProduct.setDescription(description);
    newProduct.setPublisher(publisher);
    newProduct.setCategory(category);
    newProduct.setUnitsInStock(stock);
    newProduct.setTotalPages(pages);
    newProduct.setReleaseDate(releaseDate);
    newProduct.setCondition(condition);
    
    
    dao.addBook(newProduct);
    response.sendRedirect("books.jsp");
    %>
</body>
</html>
cs

 

(3) BookRepository 클래스에 addBook()메소드 생성

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

 

(4) products.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
47
48
49
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import ="java.util.ArrayList" %>
<%@ page import="dto.Book" %>
<%@ page import="dao.BookRepository"  %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>도서 목록</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>
    
    <%
    BookRepository dao=BookRepository.getInstance();
    ArrayList<Book> listOfBooks=dao.getAllBooks();
    %>
    
    <div class="container">
        <%
            for(int i=0;i<listOfBooks.size();i++){
                Book book=listOfBooks.get(i);
        %>
      <div class="row" align="left">
        <div class="col">
            <h3><%=book.getName()%></h3>
            <p><%=book.getDescription()%>
            <p><%=book.getAuthor()+" | "+book.getPublisher()+" | "+book.getUnitPrice()%></p>
            <a href="./book.jsp?id=<%=book.getBookId()%>"
            class="btn btn-secondary" role="button"> 상세 정보 &raquo;></a>
        </div>
      </div>
      <hr style="border:grey 1px dashed">
          <%
            }
        %>
    </div>
 
<%@ include file="footer.jsp" %>
 
</body>
</html>
cs

 

(5) product.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
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="dto.Book" %>
<%@ page import="dao.BookRepository" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<title>도서 상세 정보</title>
</head>
<body>
    <jsp:include page="menu.jsp" />
    <div class="jumbotron">
          <div class="container">
                  <h1 class = "display-3">상품 정보</h1>
          </div>
    </div>
    <%
        String id=request.getParameter("id");
        BookRepository dao=BookRepository.getInstance();
        Book book=dao.getBookById(id);
    %>
    <div class="container">
      <div class="row" align="left">
        <div class="col-md-6">
            <h3><%=book.getName()%></h3>
            <p><%=book.getDescription()%></p>
            <p> <b>도서 코드 : </b><span class="badge badge-danger">
            <%=book.getBookId()%>
            </span>
            <p> <b>출판사</b> : <%=book.getPublisher() %>
            <p> <b>저자</b> : <%=book.getAuthor() %>
            <p> <b>재고수</b> : <%=book.getUnitsInStock()%>
            <p> <b>총 페이지 수</b> : <%=book.getTotalPages() %>
            <p> <b>출판일</b> : <%=book.getReleaseDate() %>
 
            <h4><%=book.getUnitPrice() %></h4>
            <p> <a href="#" class="btn btn-info">도서 주문 &raquo;</a>
            <a href="./books.jsp"    class="btn btn-secondary">도서 목록  &raquo;</a>
        </div>
        
      </div>
      <hr>
</body>
</html>
cs

쉽게 배우는 jsp 웹 프로그래밍 6장 연습문제 정답 솔루션 답

728x90