본문 바로가기

Programming/Servlet & JSP

방명록(어플리케이션)

addTalk.jsp

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setCharacterEncoding("EUC-KR");
		String id = request.getParameter("id");
		String talk = request.getParameter("talk");
		ArrayList<String> id_list = (ArrayList<String>) application.getAttribute("id_list");
		ArrayList<String> talk_list = (ArrayList<String>) application.getAttribute("talk_list");
		if (id_list == null) {
			id_list = new ArrayList<String>();
			talk_list = new ArrayList<String>();
		}
		id_list.add(id);
		talk_list.add(talk);
		application.setAttribute("id_list", id_list);
		application.setAttribute("talk_list", talk_list);
		response.sendRedirect("main.jsp");
	%>
</body>
</html>

 

main.jsp

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
	pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<style>
div {
	width: 100%;
	margin: 0px atuo;
	text-align: center;
}
table {
	width: 400px; margin : 0px auto;
	text-align: center;
	border: 1px solid black;
	margin: 0px auto;
}
</style>
</head>
<body>
	<div>
		<form action="addTalk.jsp" method="post">
			<input type="text" name="id" placeholder="아이디 입력"> : <input
				type="text" name="talk" placeholder="하고 싶은 말"> <input
				type="submit" value="전송">
		</form>

		<%
			ArrayList<String> id_list = (ArrayList<String>) application.getAttribute("id_list");
			ArrayList<String> talk_list = (ArrayList<String>) application.getAttribute("talk_list");
		%>
		<br />
		<table>
			<tr>
				<td>아이디</td>
				<td>하고 싶은 말</td>
			</tr>
			<%
				if (id_list != null) { // 작성한 말이 없을 때 null point Exception 발생 처리
					for (int i = (id_list.size() - 1); i >= 0; i--) {
			%>

			<tr>
				<td><%=id_list.get(i)%></td>
				<td><%=talk_list.get(i)%></td>
				<%
					}
					}
				%>
			</tr>
		</table>
	</div>
</body>
</html>

'Programming > Servlet & JSP' 카테고리의 다른 글

랜덤 당첨  (0) 2020.02.23
방문자수  (0) 2020.02.20
장바구니(쿠키)  (0) 2020.02.20
로그인(세션)  (0) 2020.02.20
전화번호 관리  (0) 2020.02.20