공부/JSP
[JSP] 쉽게 배우는 JSP 웹 프로그래밍 7장 연습문제
thegreatjy
2020. 10. 25. 15:45
728x90
3번
(1) fileupload01.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="fileForm" method="post" enctype="multipart/form-data" action="fileupload1_process.jsp">
<p> 파일 : <input type="file" name="filename">
<p> <input type="submit" value="파일올리기">
</form>
</body>
</html>
|
cs |
(2) fileupload01_process.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
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.oreilly.servlet.*" %>
<%@ page import="com.oreilly.servlet.multipart.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
MultipartRequest multi=new MultipartRequest(request, "C:\\upload", 5*1024*1024, "utf-8", new DefaultFileRenamePolicy());
Enumeration files=multi.getFileNames();
while(files.hasMoreElements()){
String name=(String)files.nextElement();
String filename=multi.getFilesystemName(name);
String original=multi.getOriginalFileName(name);
String type=multi.getContentType(name);
File file=multi.getFile(name);
out.println("요청 파라미터 이름 : "+name+"<br>");
out.println("실제 파일 이름 : "+original+"<br>");
out.println("저장 파일 이름 : "+filename+"<br>");
out.println("파일 컨텐츠 유형 : "+type+"<br>");
if(file!=null){
out.println("파일 크기 : "+file.length());
out.println("<br>");
}
}
out.println("<hr>");
%>
</body>
</html>
|
cs |
4번
(1) fileupload02.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="fileupload2_process.jsp">
<p> 파일 : <input type="file" name="filename">
<p> <input type="submit" value="파일올리기">
</form>
</body>
</html>
|
cs |
(2) fileupload02_process.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"
pageEncoding="UTF-8"%>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String path="C:\\upload";
DiskFileUpload upload=new DiskFileUpload();
upload.setSizeMax(1000000);
upload.setSizeThreshold(4096);
upload.setRepositoryPath(path);
List items=upload.parseRequest(request);
Iterator params= items.iterator();
while(params.hasNext()){
FileItem item=(FileItem)params.next();
String fileFieldName=item.getFieldName();
String fileName=item.getName();
//System.out.println(fileName+"...."+fileName.lastIndexOf("\\"));
String contentType=item.getContentType();
fileName=fileName.substring(fileName.lastIndexOf("\\")+1);
long fileSize=item.getSize();
File file=new File(path+"/"+fileName);
item.write(file);
out.println("요청 파라미터 이름 : "+fileFieldName+"<br>");
out.println("저장 파일 이름 : "+fileName+"<br>");
out.println("파일 컨텐츠 유형 : "+contentType+"<br>");
out.println("파일 크기 : "+fileSize);
}
%>
</body>
</html>
|
cs |
5번
(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
117
118
119
120
121
122
|
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;
private String filename;
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;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename=filename;
}
}
|
cs |
(2) 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
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
|
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");
b1.setFilename("1.jpg");
Book b2=new Book("2", "[IT 모바일] 쉽게 배우는 자바 프로그래밍", 27000);
b2.setDescription("객체 지향의 핵심과 자바의 현대적 기능을 충실히 다루면서도 초보자가 쉽게 학습할 수 있게 구성한 교재이다. 시각화 도구를 활용한 개념 설명과 군더더기 없는 핵심 코드를 통해 개념과 구현을 한 흐름으로 학습할 수 있다.");
b2.setAuthor("우종중");
b2.setPublisher("한빛미디어");
b2.setUnitsInStock(1000);
b2.setTotalPages(692);
b2.setReleaseDate("2017/08/02");
b2.setFilename("2.jpg");
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");
b3.setFilename("3.jpg");
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 |
(3) WebContent/resources/css/bootstrap.mis.css
WebContent/resources/images/1.jpg, 2.jpg, 3.jpg (알아서 소스코드 수정)
C드라이브에 upload파일 생성
WebContent/WEB-INF/lib/cos.jar
(4) 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
47
48
49
50
51
52
|
<%@ 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="./resources/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-md-3">
<img src="c:/upload/<%=book.getFilename()%>" style="width:100%"/>
</div>
<div class="col-md-6">
<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"> 상세 정보 »></a>
</div>
</div>
<hr style="border:grey 1px dashed">
<%
}
%>
</div>
<%@ include file="footer.jsp" %>
</body>
</html>
|
cs |
(5) book.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="dto.Book" %>
<%@ page import="dao.BookRepository" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./resources/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-5">
<img src="c:/upload/<%=book.getFilename()%>" style="width:100%"/>
</div>
<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">도서 주문 »</a>
<a href="./books.jsp" class="btn btn-secondary">도서 목록 »</a>
</div>
</div>
<hr>
</body>
</html>
|
cs |
(6) 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
96
97
98
99
100
101
102
|
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<link rel="stylesheet" href="./resources/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"
enctype="multipart/form-data">
<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">
<label class="col-sm-2">이미지</label>
<div class="col-sm-5">
<input type="file" name="productImage" class="form-control">
</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 |
(7) 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="com.oreilly.servlet.*" %>
<%@ page import="com.oreilly.servlet.multipart.*" %>
<%@ page import="java.util.*" %>
<%@ page import="dto.Book" %>
<%@ page import="dao.BookRepository" %>
<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String filename="";
String realFolder="C:\\upload";
int maxSize=5*1024*1024;
String encType="utf-8";
MultipartRequest multi=new MultipartRequest(request, realFolder, 5*1024*1024, encType, new DefaultFileRenamePolicy());
String bookId=multi.getParameter("bookId");
String name=multi.getParameter("name");
String unitPrice=multi.getParameter("unitPrice");
String author=multi.getParameter("author");
String description=multi.getParameter("description");
String publisher=multi.getParameter("publisher");
String category=multi.getParameter("category");
String unitsInStock=multi.getParameter("unitsInStock");
String totalPages=multi.getParameter("totalPages");
String releaseDate=multi.getParameter("releaseDate");
String condition=multi.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);
Enumeration files=multi.getFileNames();
String fname=(String)files.nextElement();
String fileName=multi.getFilesystemName(fname);
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);
newProduct.setFilename(fileName);
dao.addBook(newProduct);
response.sendRedirect("books.jsp");
%>
</body>
</html>
|
cs |
728x90