Session

์ฝœ๋ผ๋ง˜ยท2023๋…„ 6์›” 20์ผ
0

JSP

๋ชฉ๋ก ๋ณด๊ธฐ
9/17
post-thumbnail

๐Ÿ Http Session

  • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ตœ์ดˆ๋กœ ์›น ์„œ๋ฒ„์— ์ ‘์†ํ•˜๋ฉด ์„œ๋ฒ„๋Š” ํ•ด๋‹น ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์„ธ์…˜ID๋ฅผ ๋ฐœ๊ธ‰ํ•œ๋‹ค
  • ์„ธ์…˜ID๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ ‘์†ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•œ ์›น ๋ธŒ๋ผ์šฐ์ €์˜ ํŠน์ •๋ถ€๋ถ„(์ฟ ํ‚ค)์— ๋ณด๊ด€๋œ๋‹ค (๋‹ค๋ฅธ ์›น ๋ธŒ๋ผ์šฐ์ €๋กœ ์„œ๋ฒ„์— ์ ‘์†ํ•˜๋ฉด ์ƒˆ๋กœ์šด ์„ธ์…˜ID๋ฅผ ๋ฐœ๊ธ‰๋ฐ›๋Š”๋‹ค)
  • ์›น ๋ธŒ๋ผ์šฐ์ €์˜ ์ฟ ํ‚ค์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์„ธ์…˜ID๋Š” ์ดํ›„ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ ๋งˆ๋‹ค ํ•ญ์ƒ ํ•จ๊ป˜ ์ „์†ก๋˜์–ด ๋ณด๊ด€ํ•จ ์—ด์‡  ์—ญํ• ์„ ํ•œ๋‹ค
  • ์„ธ์…˜ ๊ฐ์ฒด์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์€ ์„œ๋ฒ„์˜ ์ž์›์„ ๋™์‹œ ์ ‘์†์ž ์ˆ˜ ๋งŒํผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋„ˆ๋ฌด ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์„ธ์…˜ ์˜์—ญ์— ์ €์žฅํ•˜๋Š” ๊ฒƒ์€ ์ง€์–‘ํ•ด์•ผ ํ•œ๋‹ค(์ ๋‹นํžˆ ํ•ด๋ผ)

๐Ÿ ํ˜„์žฌ ์„ธ์…˜ ์ •๋ณด ํ™•์ธํ•˜๊ธฐ

โœ” jsp ์ฝ”๋“œ >>

<li>SESSION ID: <%=session.getId() %></li>
<li>์ตœ์ดˆ ์ ‘์† ์‹œ๊ฐ„: <%= new Date(session.getCreationTime()) %></li>
<li>๋งˆ์ง€๋ง‰์œผ๋กœ ํ™œ๋™ํ•œ ์‹œ๊ฐ„: <%= new Date(session.getLastAccessedTime()) %> </li>
<li>๋ฏธํ™œ๋™์‹œ ์„ธ์…˜ ๋งŒ๋ฃŒ๋˜๋Š” ์‹œ๊ฐ„: <%=session.getMaxInactiveInterval() %>์ดˆ</li>

๐Ÿ“บ ์ถœ๋ ฅ ๊ฒฐ๊ณผ >>


๐Ÿ ์„ธ์…˜ ์˜์—ญ์— ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํ•ด๋ณด๊ธฐ

โœ” jsp ํŒŒ์ผ >>

<div>
	<form action="/chap04/session/add">
		Key : 
		<select name="key" id=""> 
			<option value="appetite">์—ํ”ผํƒ€์ด์ € ์„ ํƒ</option>
			<option value="main">๋ฉ”์ธ ์„ ํƒ</option>
			<option value="fruit">๊ณผ์ผ ์„ ํƒ</option>
			<option value="dessert">๋””์ €ํŠธ ์„ ํƒ</option>
		</select>
		Value : <input type="text" name="value"/> 
		<button type="submit">์ถ”๊ฐ€</button>
	</form>
</div

โœ” SessionAddServlet.java >>

@WebServlet("/session/add")
public class SessionAddServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
		// ๊นœ๋นกํ•˜๊ณ  doPost๋งŒ ๋งŒ๋“ค์—ˆ์„ ๋•Œ doGet๋„ ๋งŒ๋“ค์–ด์„œ doPost๋กœ ๋ณด๋‚ด๋ผ๊ณ  ํ•˜๋ฉด๋จ
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String key = request.getParameter("key");
		String value = request.getParameter("value");
		// JSP์—๋Š” session ๊ฐ์ฒด๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ ธ ์žˆ์ง€๋งŒ ์„œ๋ธ”๋ฆฟ์—์„œ๋Š” ์ง์ ‘ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค
		HttpSession session = request.getSession();
		session.setAttribute(key, value);	
        response.sendRedirect("/chap04/session/index.jsp");	
	}
}

๐Ÿ“บ ์ถœ๋ ฅ ๊ฒฐ๊ณผ >>

โ— ์—ฌ๊ธฐ์„œ ์ถ”๊ฐ€๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ํ˜„์žฌ ์„ธ์…˜์˜ ๋ฐ์ดํ„ฐ ๋ชฉ๋ก์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค โ—


๐Ÿ ํ˜„์žฌ ์„ธ์…˜ ์˜์—ญ์˜ ๋ฐ์ดํ„ฐ ๋ชฉ๋ก ๋ณด๊ธฐ

<div>
	<%
		Enumeration<String> names = session.getAttributeNames();
	
		//session.Attribute์˜ ๋ชจ๋“  name๊ณผ value๋ฅผ ์ถœ๋ ฅ
		while(names.hasMoreElements()){
			String name = names.nextElement();
			out.print(String.format("%s = %s<br>", name, session.getAttribute(name)));
		}
	%>
	</div>
  • Enumeration : ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆด ์ˆ˜ ์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค (import ํ•ด์•ผํ•จ! )

    JSP์—์„œ import ํ•˜๋Š” ๋ฐฉ๋ฒ•
    <%@page import="java.util.Enumeration ,java.util.Date"%>

๐Ÿ“บ ์ถœ๋ ฅ ๊ฒฐ๊ณผ >>

โ— ์—ฌ๊ธฐ์„œ ์ถœ๋ ฅ๋˜๋Š” ๋ฐ์ดํ„ฐ ๋ชฉ๋ก์€ ์•ž์—์„œ ์ถ”๊ฐ€๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์ถ”๊ฐ€ํ•œ ๋ฐ์ดํ„ฐ์ด๋‹ค โ—


๐Ÿ ์„ธ์…˜ ์‚ญ์ œํ•˜๊ธฐ(์„ธ์…˜ ๋งŒ๋ฃŒ)

โœ” jsp ์ฝ”๋“œ >>

	<a href="./invalidate">์„ธ์…˜ ์‚ญ์ œํ•˜๊ธฐ</a>

โœ” SessionInvalidateServlet.java >>

@WebServlet("/session/invalidate")
public class SessionInvalidateServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		// ์„ธ์…˜ ๋งŒ๋ฃŒ์‹œํ‚ค๊ธฐ
		req.getSession().invalidate();
		resp.sendRedirect("./index.jsp"); // ๋“ค์–ด์™”๋˜ ์ฃผ์†Œ์˜ ์ƒ๋Œ€๊ฒฝ๋กœ
	}
}

๐Ÿ“บ ์ถœ๋ ฅ ๊ฒฐ๊ณผ >>

โ— ๋งํฌ๋ฅผ ๋ˆ„๋ฅด๋ฉด ๋ฐ์ดํ„ฐ ๋ชฉ๋ก์— ์ถ”๊ฐ€๋˜์–ด์žˆ๋˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ชจ๋‘ ์‚ฌ๋ผ์ง„๋‹ค โ—

profile
์ฝœ๋ผ๋ง˜ ์ฝ”๋”ฉ์ผ๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€