본문 바로가기
공부/JSP

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

by thegreatjy 2020. 10. 8.
728x90

4번

(1) request.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>Insert title here</title>
</head>
<body>
    <form action="requst_process.jsp" method="get">
        <p> 아이디  : <input type="text" name="id">
        <p> 비밀번호 : <input type="text" name="passwd">
        <p> <input type="submit" value="전송"/> 
    </form>
 
</body>
</html>
cs

 

(2) request_process.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>Insert title here</title>
</head>
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String userId=request.getParameter("id");
        String password=request.getParameter("passwd");
    %>
    <p> 전송된 요청 파라미터 : id=<%=userId%>&passwd=<%=password%>
</body>
</html>
cs

 

 

5번

(1) response.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
<%@ page language="java" contentType="text/html; charset=utf-8"%>
    <%@ page import="java.util.Calendar"  %>
    <%@ page import="java.text.SimpleDateFormat" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
    <%
        response.setIntHeader("Refresh", 5);
    %>
    <p>현재 시간은 
    <%
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a");
    Calendar cal=Calendar.getInstance();
    String datestr = sdf.format(cal.getTime());
 
    out.print(datestr);
    %>
    <p><a href="./response_data.jsp">google 홈페이지로 이동하기</a>
</body>
</html>
cs

 

(2) response_data.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ 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>
    <%
        response.sendRedirect("https://www.google.com/");
    %>
</body>
</html>
cs

 

 

6번

(1) bookRepository 클래스에 getBookById() 메소드 생성

1
2
3
4
5
6
7
8
9
10
11
12
    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;
    }
cs

 

(2) 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
<%@ page language="java" contentType="text/html; charset=utf-8"%>
    <%@ 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>도서 목록</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> listOfBooks=bookDAO.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

 

(3) 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
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="dto.Book" %>
<jsp:useBean id="bookDAO" class="dao.BookRepository" scope="session"/>
<!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");
        Book book=bookDAO.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="./product.jsp"    class="btn btn-secondary">도서 목록  &raquo;</a>
        </div>
        
      </div>
      <hr>
</body>
</html>
cs

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

728x90