JiYoung Dev πŸ–₯

[JSP/Servlet] μ„Έμ…˜(session) (2023.05.18) λ³Έλ¬Έ

full stack/Back-End

[JSP/Servlet] μ„Έμ…˜(session) (2023.05.18)

Shinjio 2023. 5. 19. 09:39

 

Session

 

🎈 μ„Έμ…˜(Session)

 

 

ν΄λΌμ΄μ–ΈνŠΈλŠ” μ„Έμ…˜μ„ 톡해 μ‚¬μš©μžμ˜ 정보가 μ•„λ‹Œ μ„Έμ…˜ 아이디λ₯Ό λ³΄μœ ν•˜κ³  있음. 

μ‚¬μš©μž μ •λ³΄λŠ” μ„œλ²„κ°€ 가지고 있음. (λΈŒλΌμš°μ €κ°€ μ’…λ£Œλ˜κΈ° μ „κΉŒμ§€)

- μ„Έμ…˜ 아이디 : μ„œλ²„κ°€ ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ μž„μ˜μ μœΌλ‘œ λ°œκΈ‰ν•˜λŠ” 아이디. 

λͺ¨λ“  ν΄λΌμ΄μ–ΈνŠΈλ§ˆλ‹€ λ‹€ λ‹€λ₯΄κ²Œ λ°œκΈ‰ν•¨

μΏ ν‚€λŠ” ν΄λΌμ΄μ–ΈνŠΈκ°€ 정보λ₯Ό 가지고 있음

 

 

μ‚¬μš©μžμ˜ μ •λ³΄λŠ” μ„œλ²„μ—μ„œ μ €μž₯, ν΄λΌμ΄μ–ΈνŠΈλŠ” μ„Έμ…˜ μ•„μ΄λ””λ§Œ 가지고 있게됨

μ„Έμ…˜μ„ μ‚¬μš©ν•˜λ©΄ μ‚¬μš©μžμ˜ μ •λ³΄λŠ” μ„œλ²„μƒμ—μ„œλ§Œ μ €μž₯λ˜λ―€λ‘œ 쿠킀에 λΉ„ν•΄ λ³΄μ•ˆμƒ 쒋을 것 κ°™μ§€λ§Œ, 

μ„Έμ…˜ μ•„μ΄λ””λŠ” ν΄λΌμ΄μ–ΈνŠΈ ν•˜λ‚˜λ§Œμ„ μ‹λ³„ν•˜λŠ” κ°’μœΌλ‘œ μ‚¬μš©μž 정보가 될 수 있음. 

μ„Έμ…˜μ•„μ΄λ””κ°€ λ„˜κ²¨μ§€λŠ” 과정을 보닀가 곡격을 ν•  수 도 있음

μ™„μ „ν•˜κ²Œ λ³΄μ•ˆμƒμœΌλ‘œ μ’‹λ‹€κ³  ν•  μˆ˜λŠ” μ—†λ‹€. μΏ ν‚€λ³΄λ‹€λŠ” 쒋을 수 있음

 

πŸ“– μ„Έμ…˜ μ‚¬μš© μ˜ˆμ‹œ

둜그인

 

πŸ“– μ„Έμ…˜ μž₯/단점

 

μ„Έμ…˜μ€ μ„œλ²„ μŠ€ν† λ¦¬μ§€λ₯Ό μ‚¬μš©ν•˜λ―€λ‘œ 쿠킀보닀 μš©λŸ‰μ΄ 크닀. 

 

πŸ“– JSP & Servlet μ„Έμ…˜ 생성 & κ°€μ Έμ˜€κΈ° & μ‚­μ œ

βš™οΈ μ„Έμ…˜ μƒμ„±ν•˜κΈ°

μ„Έμ…˜ 생성은 쿠킀보닀 쉽닀

 

 

jsp λ‚΄μž₯ 객체둜 HttpSession을 가지고 있음 >> 객체 생성 없이 λ°”λ‘œ μ‚¬μš© κ°€λŠ₯

 

μ„Έμ…˜ 생성 및 name&value μ €μž₯ :

session.setAttribute(String(name), Object(value)

 

<%@page import="java.util.ArrayList"%>
<%@ 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. μ„Έμ…˜μ— κ°’ μ„€μ • : setAttribute(String(name),Object(value))
		//   ValueλŠ” Object 객체 νƒ€μž…μœΌλ‘œ λͺ¨λ“  νƒ€μž…μ΄ λ“€μ–΄κ°ˆ 수 있음
		session.setAttribute("id","smart");
		session.setAttribute("age", 20);
		
		ArrayList<String> al = new ArrayList<String>();
		al.add("apple");
		al.add("banana");
		session.setAttribute("list", al);
	%>
	
	<a href="Ex05GetSession.jsp">μ„Έμ…˜ν™•μΈ</a>
	<a href="GetSession">μ„Έμ…˜ν™•μΈ(Servlet)</a>
</body>
</html>

 

βš™οΈ μ„Έμ…˜ κ°’ κ°€μ Έμ˜€κΈ°

getAttribute(μ„Έμ…˜ name);

 

<%@page import="java.util.ArrayList"%>
<%@ 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>
	<%
	
	//-- μ„Έμ…˜ κ°’ κ°€μ Έμ˜€κΈ°
	//getAttirbute(String(name))
	String id = (String)session.getAttribute("id");
	//Object νƒ€μž…μœΌλ‘œ λ°˜ν™˜ >> μ›λž˜ νƒ€μž…μœΌλ‘œ λ‹€μš΄ μΊμŠ€νŒ…
	Integer age = (Integer)session.getAttribute("age");
	ArrayList<String> al = (ArrayList<String>)session.getAttribute("list");
	
	%>
	
	id : <%=id %><br>
	age : <%=age %><br>
	list : <%=al %>
	
	<a href="Ex06RemoveSession.jsp">μ„Έμ…˜ μ‚­μ œ</a>
	<a href="Ex07Invalidate.jsp">μ„Έμ…˜ μ „λΆ€ μ‚­μ œ</a>
	
</body>
</html>

 

μ„Έμ…˜ κ°’ κ°€μ Έμ˜¬λ•Œ μ—…μΊμŠ€νŒ… 주의!

 

 

μ„Έμ…˜μ˜ 값을 뢈러올 λ•Œ getAttribute의 λ§€κ°œλ³€μˆ˜λ‘œ name을 μ§€μ •ν•˜μ§€λ§Œ μ‹€μ œλ‘œ 가지고 μ˜€λŠ” 값은 valueκ°’

value의 νƒ€μž…μ€ Object

λ¬Έμžμ—΄, μ •μˆ˜, ArrayList λͺ¨λ‘ Object의 ν•˜μœ„ νƒ€μž…

λ”°λΌμ„œ μ €μž₯될 λ•Œ value값은 μ–΄λ–€ νƒ€μž…μ΄λ“  upcasting 됨 

λ”°λΌμ„œ κΈ°μ‘΄ νƒ€μž…μœΌλ‘œ μ €μž₯ν•˜λ €λ©΄ downcastingν•΄μ€˜μ•Ό 함

 

 

βš™οΈ μ„Έμ…˜ μ‚­μ œ

- ν•˜λ‚˜μ˜ μ„Έμ…˜λ§Œ μ‚­μ œν•  λ•Œ : session.removeAttribute(μ„Έμ…˜λ„€μž„)

 

<%@ 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>
	<%
		//-- μ„Έμ…˜ μ‚­μ œ (ν•˜λ‚˜λ§Œ) 
		//removeAttribute λ©”μ„œλ“œ ν™œμš©
		session.removeAttribute("age");
	%>
	
	<a href="Ex05GetSession.jsp">μ„Έμ…˜ 확인</a>
</body>
</html>

 

전체 μ„Έμ…˜ μ‚­μ œν•  λ•Œ : session.invalidate();

 

<%@ 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.invalidate();
	%>
	
	<a href="Ex05GetSession.jsp">μ„Έμ…˜ 확인</a>
</body>
</html>

 

< μ„Έμ…˜ μ‚­μ œ ν›„ μ„Έμ…˜κ°’λΆˆλŸ¬μ˜¬λ•Œ λ°œμƒν•  수 μžˆλŠ” 였λ₯˜ >

 

 

μ„Έμ…˜ κ°’ 뢈러올 λ•Œ int μ •μˆ˜ν˜•μ„ μ‚¬μš©ν•˜λ©΄ 였λ₯˜ λ°œμƒ

μ™œ? 

age(int) μ‚­μ œν•˜κ³  λ‚˜λ©΄ λ°˜ν™˜ν•˜λŠ” 값은 null κ°’

κΈ°λ³Ένƒ€μž…μ€ null값을 받을 수 μ—†μŒ

λ”°λΌμ„œ NullPointerException λ°œμƒ 

κΈ°λ³Ένƒ€μž…μ€ null값을 κ°€μ§ˆ 수 μ—†μŒ >> Integer μ‚¬μš©

 

Servlet으둜 μ„Έμ…˜ 생성/κ°€μ Έμ˜€κΈ°

 

servletμ—μ„œ μ„Έμ…˜μ„ 닀루고 μ‹ΆμœΌλ©΄ session객체λ₯Ό 생성해야 함

HttpSession session = request.getSession();

 


import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet("/GetSession")
public class GetSession extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//μ„Έμ…˜ 객체 생성
		//μ„Έμ…˜ μ•„μ΄λ””λŠ” ν΄λΌμ΄μ–ΈνŠΈμ— μ €μž₯λ˜μ–΄ 있음. ν΄λΌμ΄μ–ΈνŠΈ>>μ„œλ²„ μš”μ²­ 
		HttpSession session = request.getSession();
		
		//-- μ„Έμ…˜ κ°’ κ°€μ Έμ˜€κΈ°
		//getAttirbute(String(name))
		String id = (String)session.getAttribute("id");
		//Object νƒ€μž…μœΌλ‘œ λ°˜ν™˜ >> μ›λž˜ νƒ€μž…μœΌλ‘œ λ‹€μš΄ μΊμŠ€νŒ…
		int age = (int)session.getAttribute("age");
		ArrayList<String> al = (ArrayList<String>)session.getAttribute("list");
		
		//응닡 데이터 ν˜•μ‹
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter(); //PrintWriter ν…μŠ€νŠΈ 좜λ ₯ 슀트림 생성
		out.print("id : " + id + "<br>");
		out.print("age : " + age + "<br>");
		out.print("list : " + al.toString());
	}

}

 

🎈 μΏ ν‚€ vs μ„Έμ…˜

 

 

μΏ ν‚€λŠ” μ‚¬μš©μžμ˜ pc에 정보 μ €μž₯ λ‚΄κ°€ 가진 정보λ₯Ό κ·ΈλŒ€λ‘œ μ“°λ©΄ λ˜λ‹ˆκΉŒ μƒλŒ€μ μœΌλ‘œ λΉ λ₯΄λ‹€

μ„Έμ…˜μ€ μ„œλ²„μ—μ„œ 정보λ₯Ό 가져와야 함 > μƒλŒ€μ μœΌλ‘œ λŠλ¦¬λ‹€