<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>04.24</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>dataPath</param-name>
<param-value>/data/list.dat</param-value>
</context-param>
</web-app>
package kr.ac.green;
import java.io.Serializable;
public class Student implements Serializable{ //학생정보 담을 거. Stream쓰려고 시리얼라이즈함.
private String name;
private String nick;
private int age;
public Student() {
}
public Student(String name, String nick, int age) {
super();
this.name = name;
this.nick = nick;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", nick=" + nick + ", age=" + age + "]";
}
}
package kr.ac.green;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;
import javax.servlet.ServletContext;
public class StudentManager {
public static Vector<Student> loadList(ServletContext application, String paramName) {
String dataPath = application.getInitParameter(paramName);
String path = application.getRealPath(dataPath);
FileInputStream fis = null;
ObjectInputStream ois= null;
Vector<Student> list = null;
try {
fis = new FileInputStream(path);
ois = new ObjectInputStream(fis);
list = (Vector<Student>)ois.readObject();
}catch(IOException e) {
list = new Vector<Student>();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
try {
ois.close();
}catch(Exception e) {}
try {
fis.close();
}catch(Exception e) {}
}
return list;
}
public static void storeStudent(ServletContext application, Vector<Student> list, String paramName) {
String dataPath = application.getInitParameter(paramName);
String path = application.getRealPath(dataPath);
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) {
}finally {
try {
oos.close();
}catch(Exception e) {}
try {
fos.close();
}catch(Exception e) {}
}
}
}
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>
<%@ page import="kr.ac.green.*" %>
<%
Vector<Student> list = StudentManager.loadList(application, "dataPath");
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset="EUC-KR">
<title>main.jsp</title>
</head>
<body>
<form action="save.jsp" method ="post">
이름 : <input type="text" name="studentName" />
<br>
별명 : <input type="text" name="studentNick"/>
<br>
나이 : <input type="text" name="studentAge"/>
<br>
<input type="submit" value="등록" />
<input type="reset" value="초기화" />
</form>
<hr>
<table>
<caption>학생목록</caption>
<thead>
<tr>
<th>번호</th>
<th>이름</th>
<th>별명</th>
<th>나이</th>
</tr>
</thead>
<tfoot>
<tr>
<th colspan="4"><%=list.size() %> row(s) </th>
</tr>
</tfoot>
<tbody>
<%
for(int i = 0; i<list.size(); i++) {
Student temp = list.get(i);
%>
<tr>
<td><%= i+1 %></td>
<td><%= temp.getName() %></td>
<td><%= temp.getNick() %></td>
<td><%= temp.getAge() %></td>
</tr>
<%
}
%>
</tbody>
</table>
</body>
</html>
<%@page import="kr.ac.green.StudentManager"%>
<%@page import="kr.ac.green.Student"%>
<%@ 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.*" %>
<%
request.setCharacterEncoding("euc-kr");
String name = request.getParameter("studentName");
String nick = request.getParameter("studentNick");
int age = Integer.parseInt(request.getParameter("studentAge"));
Student s = new Student(name, nick, age);
Vector<Student> list = StudentManager.loadList(application, "dataPath");
list.add(s);
StudentManager.storeStudent(application, list, "dataPath");
response.sendRedirect("main.jsp");
%>
<결과>