728x90
반응형
#게시판 글쓰기, 목록 보기(객체 직렬화) Model1 방식
※JSP Model 1 방식
data => file
#사전작업
-web.xml에 파일 경로를 추가합니다.
<context-param>
<param-name>bookPath</param-name>
<param-value>/data/books.dat</param-value>
</context-param>
-WebContent에 date폴더생성 + .dat파일 생성합니다.
-글쓰기 화면입니다.
-글목록입니다.
#books.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>
<%@ page import="kr.ac.green.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="addBook.jsp" method="post">
제목 : <input type="text" name="title" />
<br/>
저자 : <input type="text" name="author" />
<br/>
발행일 : <input type="text" name="date" />
<br/>
가격 : <input type="text" name="price" />
<br/>
<input type="submit" />
<input type="reset" />
</form>
<hr>
<%
String path = application.getRealPath(
application.getInitParameter("bookPath"));
Vector<Book> list = BookManager.getList(path);
%>
<table>
<tr>
<th>제목</th>
<th>저자</th>
<th>가격</th>
<th>발행일</th>
</tr>
<%
int size = list.size();
if(size == 0){
%>
<tr>
<td colspan="4">no books</td>
</tr>
<%
}else{
for(int i = size -1; i >=0; i--){
Book book = list.get(i);
%>
<tr>
<td><%= book.getTitle() %>
<td><%= book.getAuthor() %>
<td><%= book.getPrice() %>
<td><%= book.getDate() %>
</tr>
<%
}
}
%>
</table>
</body>
</html>
#addbook.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="kr.ac.green.Book" %>
<%@ page import="kr.ac.green.BookManager" %>
<%
request.setCharacterEncoding("euc_kr");
String title = request.getParameter("title");
String author = request.getParameter("author");
String date = request.getParameter("date");
int price = Integer.parseInt(request.getParameter("price"));
Book book = new Book(title, author, date, price);
String path =
application.getRealPath(application.getInitParameter("bookPath"));
Vector<Book> list = BookManager.getList(path);
list.add(book);
BookManager.addBook(path, list);
response.sendRedirect("books.jsp");
%>
#Book.java
import java.io.Serializable;
public class Book implements Serializable {
private String title;
private String author;
private String date;
private int price;
public Book(String title, String author, String date, int price) {
super();
this.title = title;
this.author = author;
this.date = date;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
#BookManager
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;
public class BookManager {
public static void addBook(String path, Vector<Book> list) {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream(path);
oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.flush();
oos.reset();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
oos.close();
} catch (Exception e) {
}
try {
fos.close();
} catch (Exception e) {
}
}
}
public static Vector<Book> getList(String path) {
Vector<Book> list = null;
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(path);
ois = new ObjectInputStream(fis);
list = (Vector<Book>) ois.readObject();
} catch (IOException e) {
// 최초실행 시 파일이 존재하지 않아 예외가 발생한다.
list = new Vector<Book>();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
ois.close();
} catch (Exception e) {
}
try {
fis.close();
} catch (Exception e) {
}
}
return list;
}
}
728x90
반응형
'IT > JSP Practice' 카테고리의 다른 글
[JSP Practice] - JSP MODEL 2 PreparedStatement (0) | 2020.08.04 |
---|---|
[JSP Practice] - JSP Model 2 Statement (0) | 2020.08.04 |
[JSP Practice] - JSP 로그인, 회원가입(CRUD) Model1 방식 (0) | 2020.07.30 |
[JSP Practice] - JSP 게시판 CRUD + 페이징(Paging)처리 Model 1 방식 (0) | 2020.07.27 |
[JSP Practice] - JSP 게시판 CRUD(글쓰기, 읽기, 수정, 삭제) Model 1 방식 (0) | 2020.07.24 |
댓글