본문 바로가기

Study/JSP

[jsp] request, response, <jsp:forward>, <jsp:param>, 쿠키 실습

attributeTest1_Form.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<html>
<head>
</head>
<body>
<h2>영역과 속성 테스트</h2>
<form action="attributeTest1.jsp" method="post">
<table border="1" cellpadding="0" cellspacing="0">
 <tr><td colspan="2">Application 영역에 저장할 내용들</td></tr>
 <tr>
  <td>이름</td>
  <td><input type="text" name="name"></td>
 </tr>
 <tr>
  <td>아이디</td>
  <td><input type="text" name="id"></td>
 </tr>
 <tr>
  <td colspan="2"><input type="submit" value="전송"></td>
 </tr>
</table>
</form>
</body>
</html>

 

 


attributeTest1.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h2>영역과 속성 테스트</h2>
<%
 request.setCharacterEncoding("KSC5601");

 String name = request.getParameter("name");
 String id = request.getParameter("id");
 
 session.setAttribute("sName", name);
 session.setAttribute("sId", id);

 out.print(name);
%>님 반갑습니다.<br />
<% out.print(name); %>님의 아이디는 <% out.print(id); %>입니다.
<form action="attributeTest2.jsp" method="post">
<table border="1" cellpadding="0" cellspacing="0">
 <tr>
  <td colspan=2>Session영역에 저장할 내용들</td>
 </tr>
 
 <tr>
   <td>e-mail주소</td>
   <td><input type=text name=email></td></tr>
 
 <tr>
   <td>집주소</td>
   <td><input type="text" name="address"></td>
 </tr>
 
 <tr>
   <td>전화번호</td>
   <td><input type="text" name="phone"></td>
 </tr>
 
 <tr>
  <td colspan="2"><input type="submit" value="전송"></td>
 </tr>
</table> 
</form>
</body>
</html>

 

 

 


attributeTest2.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h2>영역과 속성 테스트</h2>
<%
 request.setCharacterEncoding("KSC5601");

 String email = request.getParameter("email");
 String address = request.getParameter("address");
 String phone = request.getParameter("phone");
  
 session.setAttribute("sEmail", email);
 session.setAttribute("sAddr", address);
 session.setAttribute("sPhone", phone);
 
 Object name = (String)session.getAttribute("sName");
 out.print(name);
%>님의 정보가 모두 저장되었습니다.<br />
<a href="attributeTest3.jsp">확인하러가기</a>
</body>
</html>

 

 


attributeTest3.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h2>영역과 속성 테스트</h2>
<%
 Object name = (String)session.getAttribute("sName");
 Object id = (String)session.getAttribute("sId");
 Object email = (String)session.getAttribute("sEmail");
 Object address = (String)session.getAttribute("sAddr");
 Object phone = (String)session.getAttribute("sPhone");
%>
<table border="1">
 <tr><td colspan="2">Application 영역에 저장할 내용들</td></tr>
 <tr>
  <td>이름</td>
  <td><%= name %></td>
 </tr>
 <tr>
  <td>아이디</td>
  <td><%= id %></td>
 </tr>
</table>
<p>
<table border="1" cellpadding="0" cellspacing="0">
 <tr>
  <td colspan=2>Session영역에 저장할 내용들</td>
 </tr>
 <tr>
   <td>e-mail주소</td>
   <td><%=email %></td></tr> 
 <tr>
   <td>집주소</td>
   <td><%= address %></td>
 </tr>
 <tr>
   <td>전화번호</td>
   <td><%= phone %></td>
 </tr>

</table> 
</body>
</html>

 

 





login.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
<h2>로그인</h2>
<hr>
<form action="choice.jsp" method="post">
 <input type="text" name="name">
 <input type="submit" value="로그인">
</form>
</center>
</body>
</html>

 

 


choice.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
<h2>상품선택</h2>
<hr>
<%
 request.setCharacterEncoding("KSC5601");

 ArrayList<String> list = new ArrayList<String>();
 session.setAttribute("list", list);
 
 String name = request.getParameter("name");
 out.print(name);
%>님이 로그인 한 상태 입니다.
<%= name %>
<hr>
<form action="choiceCal.jsp" method="post">
 <select name="sel">
  <option value="사과">사과</option>
  <option value="귤">귤</option>
  <option value="바나나">바나나</option>
  <option value="포도">포도</option>
  <option value="레몬">레몬</option>
 </select>
 <input type="submit" value="추가"> 
</form>

<a href="cal.jsp">계산</a>
</center>
</body>
</html>


choiceCal.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<%
 request.setCharacterEncoding("KSC5601");
 
 ArrayList<String> arrList = (ArrayList)session.getAttribute("list");
 String selCho = request.getParameter("sel");
 
 arrList.add(selCho);
 session.setAttribute("list", arrList);
%>
<script type="text/javascript">
 alert("<%=selCho %>이(가) 추가되었습니다.");
 history.back();
</script>
</head>
<body>
</body>
</html>

 


cal.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.util.ArrayList"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
<h2>계산</h2>
선택한 상품 목록
<hr>
<%
 ArrayList<String> listCal = (ArrayList)session.getAttribute("list");
 for(int i=0; i<listCal.size(); i++) { 
  out.println(listCal.get(i));
%><br /><%
 }
%>

</center>
</body>
</html>



 login.jsp  ->  choice.jsp  ->  choiceCal.jsp choice.jsp->  cal.jsp
 이름입력
 로그인
이름데이터전달 이름데이터받기
option선택
ArrayList생성
session저장

option데이터session전달 alert창띄우기
option데이터받고ArrayList에추가
다시session에재저장(session의리셋)
 계산입력  ArrayList에저장된리스트를하나씩받아서출력