Session(세션)
-브라우저가 종료되기전까지 클라이언트의 정보를 유지하게 해주는 기술
-사용자 정보 파일을 서버 측에서 관리
Session 사용 예
로근인 같이 보안상 중요한 작업을 수행할 때 사용
Session 특징
1. 각 client에게 고유 ID 부여
2. 보안 명에서 쿠키보다 우수
3. 사용자가 많아지면 서버 메모리 많이 차지하게 됨
세션확인하기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
// 1. session 생성
session.setAttribute("id", "gjds2361");
session.setAttribute("age", "72");
// 내장객체이기 때문에 별도로 생성할 필요가 없다.
// 서버상에 데이터가 저장되기 때문에 다시 되돌려줄 필요가 없다.
%>
<a href = "ex03GetSeesion.jsp">세션 확인하기</a>
<a href = "ex03AllSession.jsp">모든 세션 확인하기</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//1. session 정보 가져오기
String id = (String)session.getAttribute("id");
int age = Integer.parseInt((String)session.getAttribute("age"));
//WrapperClass : 기본 자료형을 클래스(객체) 형태로 만든 것
%>
<h1>ID : <%=id %></h1>
<h1>AGE : <%=age %></h1>
</body>
</html>
<%@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>Insert title here</title>
</head>
<body>
<%
//1.모든 세션의 이름을 가져오기
Enumeration names = session.getAttributeNames();
//열거형 --> 가지고 올 때 사용하기 편한 메소드들이 기본적으로 설계되어있다.
// --> 객체들을 하나씩 처리하기에 용이함
//2. 키값에 맞는 데이터를 꺼내오기
//2-1) 데이터 유무부터 확인
while(names.hasMoreElements()){
//2-2) 데이터(key, name값) 꺼내오기
String name =(String)names.nextElement();
//2-3) session안에 있는 정보 가져오기
String data =(String)session.getAttribute(name);
out.print(data);
}
%>
</body>
</html>
실습1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action = "logincheck.jsp">
아이디 : <input type ="text" name = "id">
<br>
비밀번호 : <input type ="password" name = "pw">
<br>
<input type = "submit" value = "로그인">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//화면에 출력해주는 역할이 아니라, 로직을 제어하는 컨트롤 타워 역할
//1. 데이터 가져오기
String id = request.getParameter("id");
String pw = request.getParameter("pw");
// 2) 조건을 판단
// id=test / pw=12345라면 main.jsp 이동
if(id.equals("test") && pw.equals("12345")){
//세션 생성하기
session.setAttribute("nickname", "쿠키몬스터");
//페이지 이동
response.sendRedirect("main.jsp");
}else{
// 로그인 실패
response.sendRedirect("loginform.jsp");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%= session.getAttribute("nickname")%>님 환영합니다~
<a href = "logout.jsp">로그아웃</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//비지니스 로직만 제어하는 역할
//세션을 삭제하고 loginform으로 페이지를 이동
session.removeAttribute("nickname");
response.sendRedirect("loginform.jsp");
//세션을 모두 삭제하기
session.invalidate();
%>
</body>
</html>
Scope