✔️ 객체와 객체의 기능을 java파일로 만들어준다.
package kr.ac.green;
public class Comment {
private String writer;
private String value;
public Comment() {}
public Comment(String writer, String value) {
super();
this.writer = writer;
this.value = value;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
package kr.ac.green;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;
import javax.servlet.ServletContext;
public class DataManager {
private static ServletContext application;
// nameList 생성
public static void setApplication(ServletContext app) {
if(application == null) {
application = app;
}
Map<String, String> nameList = (Map<String,String>)application.getAttribute("nameList");
if(nameList == null) {
nameList = new Hashtable<String, String>();
Enumeration<String> names = application.getInitParameterNames();
while(names.hasMoreElements()) {
String name = names.nextElement(); // f1, f2,f3,f4
nameList.put(name, application.getInitParameter(name)); // map에다가 넣어줌
}
application.setAttribute("nameList", nameList);
}
}
// cmtList 가져오기
public static Vector<Comment> getCmtList(String code) {
Map<String, Vector<Comment>> cmtList = (Map<String, Vector<Comment>>)application.getAttribute("cmtList");
if(cmtList == null) {
cmtList = new Hashtable<String, Vector<Comment>>();
application.setAttribute("cmtList", cmtList);
}
Vector<Comment> list = cmtList.get(code);
if(list == null) {
list = new Vector<Comment>();
cmtList.put(code, list);
}
return list;
}
// 해당하는 이름 가져오기
public static String getName(String code) {
Map<String, String> nameList =
(Map<String, String>)application.getAttribute("nameList");
return nameList.get(code);
}
// cmt 추가하기
public static void addCmt(String code, Comment cmt) {
getCmtList(code).add(cmt);
}
}
✔️ application 초기화 파라미터 설정
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>04_27</display-name>
<context-param>
<param-name>f1</param-name>
<param-value>춘식</param-value>
</context-param>
<context-param>
<param-name>f2</param-name>
<param-value>라이언</param-value>
</context-param>
<context-param>
<param-name>f3</param-name>
<param-value>콘</param-value>
</context-param>
<context-param>
<param-name>f4</param-name>
<param-value>고영희</param-value>
</context-param>
</web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="kr.ac.green.*" %>
<%
DataManager.setApplication(application);
String param = request.getParameter("code");
String code = (param != null) ? param : "f1";
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>template.jsp</title>
</head>
<body>
<table border="1" width="80%">
<tr>
<td colspan="2">
<jsp:include page="logo.jsp"/>
</td>
</tr>
<tr>
<td>
<jsp:include page="menu.jsp"/>
</td>
<td>
<jsp:include page="contents.jsp">
<jsp:param name="code" value="<%= code %>" />
</jsp:include>
</td>
</tr>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
- Menu -
<br>
<ul>
<%
Map<String, String> nameList = (Map<String, String>)application.getAttribute("nameList");
Set<String> names = nameList.keySet();
for(String name :names) {
%>
<li><a href="template.jsp?code=<%=name%>"><%=nameList.get(name) %></a></li>
<%
}
%>
</ul>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%
Map<String, String> nameList = (Map<String, String>)application.getAttribute("nameList");
%>
<h1>F<%= nameList.size() %>팬페이지 </h1>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="kr.ac.green.*" %>
<%
String code = request.getParameter("code");
Vector<Comment> list = DataManager.getCmtList(code);
%>
<h1><%= DataManager.getName(code) %></h1>
<form action="save.jsp" method="post">
메세지 : <input type="text" name="cmt" />
<br>
작성자 : <input type="text" name="writer" />
<br>
<input type="hidden" name="code" value="<%= code %>" />
<input type="submit" value="등록" />
<hr>
<table>
<tr>
<th>번호</th>
<th>내용</th>
<th>작성자</th>
</tr>
<%
for(int i=list.size()-1; i>=0; i--) {
Comment cmt = list.get(i);
%>
<tr>
<td><%= i+1 %></td>
<td><%= cmt.getValue() %></td>
<td><%= cmt.getWriter() %></td>
</tr>
<%
}
%>
</table>
</form>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="kr.ac.green.*" %>
<%
request.setCharacterEncoding("utf-8");
Comment cmt = new Comment(
request.getParameter("writer"),
request.getParameter("cmt")
);
String code = request.getParameter("code");
DataManager.addCmt(code, cmt);
response.sendRedirect("template.jsp?code=" + code);
%>