- 글을 등록할 수 있다.
- 저장소는 application의 속성을 이용한다.
- include 액션태그를 사용한다.
- 작성 후 새로 고침 시 중복 등록되는 것을 막는다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판</title>
</head>
<body>
<jsp:include page="write.jsp" />
<hr>
<jsp:include page="list.jsp"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="doWrite.jsp" method="post">
작성자 <input type="text" name="writer"><br>
내용 <input type="text" name="text"><br>
<input type="submit" value="등록">
</form>
</body>
</html>
<%@page import="kr.ac.green.BoardManager"%>
<%@page import="kr.ac.green.Board"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
ArrayList<Board> list = BoardManager.loadList(application);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<table border="1">
<tr>
<th>번호</th>
<th>내용</th>
<th>작성자</th>
<th>작성일</th>
</tr>
<%
if(list.size() != 0) {
for(int i = list.size() - 1; i >= 0; i--) {
Board board = list.get(i);
%>
<tr>
<td><%= i + 1 %></td>
<td><%= board.getText() %></td>
<td><%= board.getWriter() %></td>
<td><%= board.getDate() %></td>
</tr>
<%
}
} else {
%>
<tr>
<td colspan="4">등록된 글이 없습니다.</td>
</tr>
<%
}
%>
</table>
</body>
</html>
연산 처리 jsp
<%@page import="kr.ac.green.BoardManager"%>
<%@page import="kr.ac.green.Board"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String text = request.getParameter("text");
String writer = request.getParameter("writer");
ArrayList<Board> list = BoardManager.loadList(application);
Board board = new Board(text, writer);
list.add(board);
response.sendRedirect("template.jsp");
%>
package kr.ac.green;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Board {
private String text;
private String writer;
private String date;
public Board(String text, String writer) {
setText(text);
setWriter(writer);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
date = sdf.format(new Date());
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
package kr.ac.green;
import java.util.ArrayList;
import javax.servlet.ServletContext;
public class BoardManager {
public static ArrayList<Board> loadList(ServletContext application) {
ArrayList<Board> list = (ArrayList<Board>)application.getAttribute("list");
if(list == null) {
list = new ArrayList<>();
application.setAttribute("list", list);
}
return list;
}
}