include 예시 - guestBook

제이·2023년 4월 29일
0

목록 보기
12/15

설명 : getList.jsp는 게시글 목록을 보여주기 위해 사용되며, save.jsp는 새로운 게시글을 작성하여 게시글 목록에 추가하기 위해 사용.

web.xml

<?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>guestBook</display-name>
  <welcome-file-list>
    <welcome-file>template.jsp</welcome-file>
  </welcome-file-list>
</web-app>

getList.jspf

설명 :

  • 서버의 어플리케이션의 공용 데이터인 'articleList'.
  • 이 코드는 애플리케이션이 시작될 때마다 articleList가 생성되며, application 객체에 저장됩니다.
  • application 객체의 articleList 속성에 list 객체를 저장하고 있으므로, list 객체는 해당 웹 애플리케이션의 어느 페이지에서든 접근하여 사용할 수 있습니다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import = "java.util.*"%>
<%@ page import ="org.doo.Article" %>



<%
	
	Vector<Article> list = (Vector<Article>) application.getAttribute("articleList");
	if(list == null) {
		list = new Vector<Article>();
		application.setAttribute("articleList", list);
		
	}
%>

write.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<form action ="save.jsp" method="post">
	<table>
		<tr>
			<th>작성자</th>
			<td><input type="text" name="writer"/></td>
		</tr>
		<tr>
			<th>내용</th>
			<td><input type="text" name="comment"/></td>
		</tr>
		<tr>
			<th colspan="2">
			<input type="submit" value="등록"/>
			</th>
		</tr>
	
	</table>

</form>

list.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ include file ="getList.jspf" %>

<table id="listTable">
	<tr id = "header">
		<th id="num">번호</th>
		<th id="comment">내용</th>
		<th id="writer">작성자</th>
		<th id="date">작성일</th>
		
	</tr>
	<%
		if(list.size() ==0){
	%>
		<tr>
			<th colspan ="4">
				empty
			</th>
		</tr>
		
			
	<%
		}
		for(int i = list.size()-1; i>=0; i--) {
			Article temp = list.get(i);
		
	%>
		<tr>
			<th><%=i+1 %></th>
			<th><%= temp.getComment() %></th>
			<th><%= temp.getWriter() %></th>
			<th><%= temp.getDate() %></th>
			
		</tr>
	<%
		}
	%>

</table>

save.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ include file="getList.jspf" %>

<%
	request.setCharacterEncoding("euc-kr");
	
	String writer = request.getParameter("writer");
	String comment = request.getParameter("comment");
	Article article = new Article(writer, comment);
	
	list.add(article);
	response.sendRedirect(request.getContextPath());
	
%>

css/main.css

@charset "EUC-KR";

body {
	text-align: center;
}
#mainBox {
	margin: 0 auto;
	border: 2px solid gray;
	width: 80%;
}
tabel {
	margin: 0 auto;
}
#listTable {
	width: 80%;
	border: 1px solid gray;
	border-collapse: collapse;
}
#listTable td, #listTable th {
	border: 1px solid gray;
}
#header {
	background-color: #EEEEEE;
}
#num {
	width: 10%
}
#comment {
	width: 50%
}
#writer {
	width: 20%
}
#top, #bottom {
	margin: 1em;
}
input[type="text"] {
	width: 20em;
}

template.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>guestBook</title>
<link href="css/main.css" rel="stylesheet" />
</head>

<body>
<h1>Guest Book</h1>
<div id="mainBox">
<div id="top">
<jsp:include page="write.jsp"/>
</div>
<hr>
<div id="bottom">
<jsp:include page="list.jsp" />
</div>
</div>
</body>
</html>

<결과>

profile
Hello :)

0개의 댓글