각 객체가 저장되는 메모리의 유효기간으로 4가지의 영역으로 구성된다.
영역크기 순으로 application > session> request > page 순이다.
자바빈즈 규약에 따라 작성한다.
package com.common;
public class Person {
private String name; //private 멤버 변수(규약 2번)
private int age; //private 멤버 변수(규약 2번)
//기본 생성자가 있어야한다
public Person(){}
public Person(String name, int age){
super();
this.age = age;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
page랑 request는 나중에 해보자
Session 객체는 클라이언트가 브라우저를 닫을 때까지 공유가 가능하다.
세션은 클라이언트가 서버에 접속해 있는 상태 혹은 단위를 뜻한다.
주로 회원인증 후 로그인 상태를 유지하는 처리에 사용.
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<title>Title</title>
<%
ArrayList<String> lists = new ArrayList<String>();
lists.add("리스트");
lists.add("컬렉션");
session.setAttribute("lists", lists);
%>
<html>
<head><title>sesstion 영역</title>
</head>
<body>
<%--Session 객체는 클라이언트가 브라우저를 닫을 때까지 공유가 가능하다.--%>
<%--세션은 클라이언트가 서버에 접속해 있는 상태 혹은 단위를 뜻한다.--%>
<h2>페이지 이동 후 session 영역의 속성 읽기 </h2>
<a href = "SessionLocation.jsp">SessionLocation.jsp 바로가기</a>
</body>
</html>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>session 영역</title>
</head>
<body>
<h2>페이지 이동 후 session 영역의 속성 읽기</h2>
<%
ArrayList<String> lists = (ArrayList<String>)session.getAttribute("lists");
for(String str : lists)
out.print(str + "<br/>");
%>
</body>
</html>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>session 영역</title>
</head>
<body>
<h2>페이지 이동 후 session 영역의 속성 읽기</h2>
<%
ArrayList<String> lists = (ArrayList<String>)session.getAttribute("lists");
for(String str : lists)
out.print(str + "<br/>");
%>
</body>
</html>
application 영역
한 번 저장되면 웹 애플리케이션이 종료될 때까지 유지됨.
-> 서버가 다운되지 않는다면 언제까지든 공유되는 영역.
<%@ page import="com.common.Person" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>application 영역</title>
</head>
<body>
<h2>application 영역의 공유</h2>
<%--//웹 애플리케이션 하나의 application 객체 생성--%>
<%--모든 요청들은 application 객체를 공유한다.--%>
<%--서버를 닫기 전까지는 계속 유지가 된다.--%>
<%
Map<String, Person> maps = new HashMap<>();
maps.put("actor1", new Person("강동원", 30));
maps.put("actor2", new Person("송강호", 28));
application.setAttribute("maps", maps);
%>
application 영역에 속성이 저장되었습니다.
</body>
</html>
<%@ page import="java.util.Map" %>
<%@ page import="com.common.Person" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.Map" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>application 영역</title>
</head>
<body>
<h2> application 영역의 속성 읽기</h2>
<%
Map<String, Person> maps
= (Map<String, Person>) application.getAttribute("maps");
Set<String> keys = maps.keySet();
for (String key : keys){
Person person =maps.get(key);
out.print(String.format("이름 : %s, 나이 : %d<br/>",
person.getName(), person.getAge()));
//포맷팅
}
%>
</body>
</html>