티스토리 뷰
정보를 서버가 기억하는 것 : 세션
viewSession1.jsp에서 logId1의 세션을 종료시키면 다음과 같은 결과
...
if(name.equals("logId1")){
session.removeAttribute("logId1"); //logId1 제거
}
}
//session.invalidate(); //모든 세션 종료
%>
...
setSession.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>setSession.jsp</title>
</head>
<body>
<%
String strId1="apple";
String strId2="orange";
/*세션설정*/
session.setAttribute("logId1", strId1);
session.setAttribute("logId2", strId2);
%>
세션에 속성을 설정했습니다<br>
<a href="viewSession1.jsp">세션정보확인</a>
</body>
</html>
viewSession1.jsp
<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>viesSession1.jsp</title>
</head>
<body>
<%
Enumeration ob=session.getAttributeNames();
while(ob.hasMoreElements())
{
String name=(String)ob.nextElement(); // logId1, logId2
String value=(String)session.getAttribute(name); // apple, orange
out.print("session name:"+ name +"<br>");
out.print("session value:"+ value +"<br>");
out.print("session id:"+ session.getId() +"<br><br>");
}
%>
<a href="viewSession2.jsp">다음페이지로 이동</a>
</body>
</html>
viewSesson2.jsp
ver1
<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>viesSession1.jsp</title>
</head>
<body>
<%
Enumeration ob=session.getAttributeNames();
while(ob.hasMoreElements())
{
String name=(String)ob.nextElement();
String value=(String)session.getAttribute(name);
out.print("세션 이름:"+ name +"<br>");
out.print("세션 값:"+ value +"<br>");
out.print("세션 id:"+ session.getId() +"<br><br>");
}
%>
</body>
</html>
ver2
<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>viesSession2.jsp</title>
</head>
<body>
<%
Enumeration ob=session.getAttributeNames();
while(ob.hasMoreElements())
{
String name=(String)ob.nextElement();
String value=(String)session.getAttribute(name);
%>
세션 이름:<%=name%><br>
세션 값:<%=value%><br>
세션 id:<%=session.getId() %><br><br>
<%
}
%>
</body>
</html>
댓글