/ Java /
@WebListener
public class InitializeCourseListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
}
@Override
public void contextDestroyed(ServletContextEvent event) {
}
}
ServletContextListener
: 웹어플리케이션을 감시하는 리스너
contextInitialized()
: 리스너의 어플리케이션이 시작될 때 호출
contextDestroy()
: 리스너의 어플리케이션이 종료될 때 호출
/ Java1 /
@WebListener
public class InitializeCourseListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
System.out.println("contextInitialized() method called");
ServletContext context = event.getServletContext();
InputStream is = null;
BufferedReader reader = null;
List<String[]> contentList = new ArrayList<>();
try {
is = context.getResourceAsStream("/WEB-INF/classes/course_contents.txt"); // 텍스트를 Character 타입으로 읽어온 것을 is에
// 담음
reader = new BufferedReader(new InputStreamReader(is)); // Character 타입 텍스트를 String으로 변환하고, BufferedReader로
// 텍스트를 모은것을 reader에 담음
String record = null;
while ((record = reader.readLine()) != null) { // 한 라인의 텍스트를 읽어오고, 텍스트가 없을 때 까지 계속 읽어옴
String[] fields = record.split("\t");
contentList.add(fields);
}
context.setAttribute("contentList", contentList);
context.log("The course contents has been loaded");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void contextDestroyed(ServletContextEvent event) {
System.out.println("contextDestroyed() method called");
}
}
/ Java2 /
@WebServlet("/mod009/list_course_contents.do")
public class ListCourseContentsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static List<String[]> contentList;
@SuppressWarnings("unchecked")
public void init(ServletConfig config) throws ServletException {
ServletContext context = config.getServletContext();
contentList = (List<String[]>) context.getAttribute("contentList");
contentList = tapToSpace(contentList);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute("contentList", contentList);
request.getRequestDispatcher("list_course_contents.jsp").forward(request, response);
}
private List<String[]> tapToSpace(List<String[]> contentList) {
if (contentList != null) {
for (String[] contents : contentList) {
for (int i = 0; i < contents.length; i++) {
if (contents[i].length() == 0) {
contents[i] = " ";
}
}
}
} else {
String[] str = new String[] { "=No Data=" };
ArrayList<String[]> list = new ArrayList<>();
list.add(str);
contentList = list;
}
return contentList;
}
}
/ JSP /
<c:forEach>
를 중첩해서 사용<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div id="wrapper">
<div id="header"></div>
<div id="content-wrapper">
<form action="list_course_contents.do" method="get">
<c:forEach var="contents" items="${contentList}">
<div>
<c:forEach var="content" items="${contents}">
${content}
</c:forEach>
</div>
</c:forEach>
<button type="submit">확인</button>
</form>
</div>
<div id="footer"></div>
</div>
</body>
</html>