๐Ÿ’ป

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - [Servlet] Cookie(์ฟ ํ‚ค)/ Session(์„ธ์…˜) ๋ณธ๋ฌธ

KITRI/JSP

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - [Servlet] Cookie(์ฟ ํ‚ค)/ Session(์„ธ์…˜)

๋˜ํšจ๋‹ˆ 2020. 6. 9. 12:33

Cookie(์ฟ ํ‚ค)

- ํด๋ผ์ด์–ธํŠธ ์ฟ ํ‚ค ํด๋”์— ์ €์žฅ

- ์˜ˆ: ์žฅ๋ฐ”๊ตฌ๋‹ˆ

 

Session(์„ธ์…˜)

- ์„œ๋ฒ„์— ์ €์žฅ

- ์˜ˆ: ๋กœ๊ทธ์ธ/๋กœ๊ทธ์•„์›ƒ

 

  • ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์„œ๋ฒ„๋กœ ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๋ฉด, ์„œ๋ฒ„์ธก์—์„œ๋Š” ์•Œ๋งž์€ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•œ ํ›„ ๋ฐ์ดํ„ฐ๋ฅผ ์›น ๋ธŒ๋ผ์šฐ์ €์— ์‘๋‹ตํ•œ ํ›„ ์„œ๋ฒ„๋Š” ์›น ๋ธŒ๋ผ์šฐ์ €์™€์˜ ๊ด€๊ณ„๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค. 
  • ์—ฐ๊ฒฐ์ด ๋Š์–ด์กŒ์„ ๋•Œ ์–ด๋–ค ์ •๋ณด๋ฅผ ์ง€์†์ ์œผ๋กœ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ์œผ๋กœ ์ฟ ํ‚ค๋ผ๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ฟ ํ‚ค๋Š” ์„œ๋ฒ„์—์„œ ์ƒ์„ฑํ•˜์—ฌ ์„œ๋ฒ„๊ฐ€ ์•„๋‹Œ ํด๋ผ์ด์–ธํŠธ์ธก์— ํŠน์ • ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค.(4KB ์šฉ๋Ÿ‰ ์ œํ•œ)
  • ์„ธ์…˜๋„ ์ฟ ํ‚ค์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„œ๋ฒ„์™€์˜ ๊ด€๊ณ„๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ์ž…๋‹ˆ๋‹ค.  
    ๋‹จ, ์ฟ ํ‚ค์™€ ๋‹ฌ๋ฆฌ ํด๋ผ์ด์–ธํŠธ์˜ ํŠน์ • ์œ„์น˜์— ์ €์žฅ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์„œ๋ฒ„์ƒ์— ๊ฐ์ฒด๋กœ ์กด์žฌํ•œ๋‹ค.
  • ๋”ฐ๋ผ์„œ ์„ธ์…˜์€ ์„œ๋ฒ„์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜์—ฌ ๋ณด์•ˆ์— ์ข‹๊ณ , ๋ฐ์ดํ„ฐ ์šฉ๋Ÿ‰ ํ•œ๊ณ„๊ฐ€ ์—†๋‹ค.

 


[์˜ˆ์ œ] ์žฅ๋ฐ”๊ตฌ๋‹ˆ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž.

 

 

09_example.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ €์žฅ, ๋ณด๊ธฐ, ์‚ญ์ œ  -->
	<form action="http://localhost:8181/webTesting/com/java/servlet/Example09_CookieSet" method="post">
		<table>
			<tr>
				<td>
					<img src="./../img/image1.jpg" width="200" height="150" />
				</td>
				<td>
					<img src="./../img/image2.jpg" width="200" height="150" />
				</td>
				<td>
					<img src="./../img/image3.jpg" width="200" height="150" />
				</td>
			</tr>
			<tr>
				<td align="center">
					<input type="checkbox" name="food" value="image1"/>๊ณ ๊ตฌ๋งˆ
				</td>
				<td align="center">
					<input type="checkbox" name="food" value="image2"/>์˜ฅ์ˆ˜์ˆ˜
				</td>
				<td align="center">
					<input type="checkbox" name="food" value="image3"/>๋ธ”๋ฃจ๋ฒ ๋ฆฌ
				</td>	
			</tr>
			<tr>
				<td colspan="3" align="center">
					<input type="submit" value="์žฅ๋ฐ”๊ตฌ๋‹ˆ"/>
					<input type="reset" value="์ทจ์†Œ"/>
				</td>
			</tr>
		</table>
		
		<div>
			<a href="http://localhost:8181/webTesting/com/java/servlet/Example09_CookieGet">
				์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ณด๊ธฐ</a><br /><br />
			<a href="http://localhost:8181/webTesting/com/java/servlet/Example09_CookieDel">
				์žฅ๋ฐ”๊ตฌ๋‹ˆ ์‚ญ์ œ</a>
		</div>
	
	</form>
</body>
</html>

 

Example09_CoockieSet.java

package com.java.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Example09
 */
public class Example09_CookieSet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Example09_CookieSet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String[] food = request.getParameterValues("food");
		System.out.println(food.length);
		
		// sessionScope : context(webTesting) ์•ˆ์— ๋ชจ๋“  page์—์„œ ๊ณต์œ 
		// response : ์ฟ ํ‚ค ์„ค์ •(์ €์žฅ), request : ์ฟ ํ‚ค ์ฝ๊ธฐ
		
		if(food.length != 0) {
			for(int i=0; i<food.length; i++) {
				// key, value
				String key = "food" + (i+1);
				String value = food[i];
				System.out.println(key + "\t" + value);
				
				Cookie cookie = new Cookie(key, value);
				// ์ฟ ํ‚ค๊ฐ€ ์‚ด์•„์žˆ๋Š” ์‹œ๊ฐ„ 10๋ถ„
				cookie.setMaxAge(60*10);  //์ดˆ*๋ถ„*์‹œ*์ผ (60*60*24*365)
										  //์‚ฌ์šฉ์ž๊ฐ€ ์ฟ ํ‚ค ํด๋”์—์„œ ์ง€์šฐ๋ฉด 
				response.addCookie(cookie);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 

 

Example09_CoockieGet.java

package com.java.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Example09_CookieGet
 */

public class Example09_CookieGet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Example09_CookieGet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		Cookie[] cookies = request.getCookies();
		//System.out.println(cookies.length);
		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		out.print("<html>");
		out.print("<head><title></title></head>");
		out.print("<body>");
		
		if(cookies != null) {
			for(int i=0; i<cookies.length;i++) {
				out.print("<h3>" + cookies[i].getName() + "</h3>");
				out.print("<h3>" + cookies[i].getValue() + "</h3>");
			}
			String contextPath = request.getContextPath();
			for(int i=0; i<cookies.length;i++) {
				String imgStr = contextPath+"/img/"+ cookies[i].getValue() + ".jpg";
				System.out.println(imgStr);
				out.print("<img src='"+ imgStr +"' width='200' height='150' />");
			}
		}else {
			out.print("<h3>์žฅ๋ฐ”๊ตฌ๋‹ˆ๊ฐ€ ๋น„์—ˆ์Šต๋‹ˆ๋‹ค.</h3>");
		}
		
		out.print("</body>");				
		out.print("</html>");				
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 

 

 

Example09_CoockieDel.java

package com.java.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class Example09_CookieDel
 */
@WebServlet("/Example09_CookieDel")
public class Example09_CookieDel extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Example09_CookieDel() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Cookie[] cookies = request.getCookies();
		if(cookies!=null) {
			for(int i=0; i<cookies.length; i++) {
				// ์ฟ ํ‚ค๋ฅผ ์ง€์šด๋‹ค.
				cookies[i].setMaxAge(0);
				response.addCookie(cookies[i]);
			}
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print("<html>");
		out.print("<head><title></title></head>");
		out.print("<body>");
		
		out.print("<script type='text/javascript'>");
		out.print("alert('์žฅ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ๋น„์› ์Šต๋‹ˆ๋‹ค.');");
		out.print("location.href='http://localhost:8181/webTesting/Servlet/09_example.html';");
		out.print("</script>");
		out.print("</body>");				
		out.print("</html>");				
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 

 

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
  <display-name>webTesting</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
	<servlet-name>Example09_CookieSet</servlet-name>
	<servlet-class>com.java.servlet.Example09_CookieSet</servlet-class>	
  </servlet>

  <servlet-mapping>
	<servlet-name>Example09_CookieSet</servlet-name>
	<url-pattern>/com/java/servlet/Example09_CookieSet</url-pattern>
  </servlet-mapping>
  
  <servlet>
	<servlet-name>Example09_CookieGet</servlet-name>
	<servlet-class>com.java.servlet.Example09_CookieGet</servlet-class>	
  </servlet>

  <servlet-mapping>
	<servlet-name>Example09_CookieGet</servlet-name>
	<url-pattern>/com/java/servlet/Example09_CookieGet</url-pattern>
  </servlet-mapping>
  
  <servlet>
	<servlet-name>Example09_CookieDel</servlet-name>
	<servlet-class>com.java.servlet.Example09_CookieDel</servlet-class>	
  </servlet>

  <servlet-mapping>
	<servlet-name>Example09_CookieDel</servlet-name>
	<url-pattern>/com/java/servlet/Example09_CookieDel</url-pattern>
  </servlet-mapping>
  
</web-app>

 

 

 

 

์ฟ ํ‚ค ํ™•์ธ

ํฌ๋กฌ ์„ค์ • > ๊ฐœ์ธ์ •๋ณด ๋ฐ ๋ณด์•ˆ > ์‚ฌ์ดํŠธ ์„ค์ • > ์ฟ ํ‚ค ๋ฐ ์‚ฌ์ดํŠธ ๋ฐ์ดํ„ฐ > ๋ชจ๋“  ์ฟ ํ‚ค ๋ฐ ์‚ฌ์ดํŠธ ๋ฐ์ดํ„ฐ ๋ณด๊ธฐ

์„ ํƒํ•œ ๊ณ ๊ตฌ๋งˆ, ์˜ฅ์ˆ˜์ˆ˜ ์ฟ ํ‚ค๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

์„ค์ •ํ•œ ์œ ํšจ์‹œ๊ฐ„์ธ 10๋ถ„ ํ›„ ์ฟ ํ‚ค๊ฐ€ ์‚ฌ๋ผ์กŒ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

๊ณ ๊ตฌ๋งˆ, ์˜ฅ์ˆ˜์ˆ˜, ๋ธ”๋ฃจ๋ฒ ๋ฆฌ ์„ ํƒ ํ›„ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ์ถ”๊ฐ€
์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋น„์šฐ๊ธฐ ํด๋ฆญ
๋น„์šด ํ›„ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๋ณด๊ธฐ ํด๋ฆญ

 

 


[์˜ˆ์ œ] ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด์„œ ์„ธ์…˜์„ ์‚ฌ์šฉํ•ด๋ณด์ž.

 

10_example.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
  <form action="http://localhost:8181/webTesting/com/java/servlet/Example10_SessionSet" method="post">
 	<label>์•„์ด๋””</label>
	<input type="text" name="id"/>
	<br /><br />
	
	<label>๋น„๋ฐ€๋ฒˆํ˜ธ</label>
	<input type="text" name="pwd"/>
	<br /><br />
   
   	<input type="submit" value="๋กœ๊ทธ์ธ"/>
	<input type="reset" value="์ทจ์†Œ" />
   </form>
   <br /><br />
   
   <div>
	<a href="http://localhost:8181/webTesting/com/java/servlet/Example10_SessionGet">
		์„ธ์…˜ ๋ณด๊ธฐ</a><br /><br />
	<a href="http://localhost:8181/webTesting/com/java/servlet/Example10_SessionDel">
		๋กœ๊ทธ์•„์›ƒ</a>
	</div>
</body>
</html>

 

 

 

Example10_SessionSet.java

package com.java.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * Servlet implementation class Example10_SessionSet
 */
public class Example10_SessionSet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Example10_SessionSet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    /* ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์„œ๋ฒ„๋กœ ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๋ฉด, ์„œ๋ฒ„์ธก์—์„œ๋Š” ์•Œ๋งž์€ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•œ ํ›„
     * ๋ฐ์ดํ„ฐ๋ฅผ ์›น ๋ธŒ๋ผ์šฐ์ €์— ์‘๋‹ตํ•œ ํ›„ ์„œ๋ฒ„๋Š” ์›น ๋ธŒ๋ผ์šฐ์ €์™€์˜ ๊ด€๊ณ„๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค. 
     * ์—ฐ๊ฒฐ์ด ๋Š์–ด์กŒ์„ ๋•Œ ์–ด๋–ค ์ •๋ณด๋ฅผ ์ง€์†์ ์œผ๋กœ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ์œผ๋กœ ์ฟ ํ‚ค๋ผ๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.
     * ์ฟ ํ‚ค๋Š” ์„œ๋ฒ„์—์„œ ์ƒ์„ฑํ•˜์—ฌ ์„œ๋ฒ„๊ฐ€ ์•„๋‹Œ ํด๋ผ์ด์–ธํŠธ์ธก์— ํŠน์ • ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค.(4KB ์šฉ๋Ÿ‰ ์ œํ•œ)
     * 
     * ์„ธ์…˜๋„ ์ฟ ํ‚ค์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„œ๋ฒ„์™€์˜ ๊ด€๊ณ„๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ์ž…๋‹ˆ๋‹ค. 
     * ๋‹จ, ์ฟ ํ‚ค์™€ ๋‹ฌ๋ฆฌ ํด๋ผ์ด์–ธํŠธ์˜ ํŠน์ • ์œ„์น˜์— ์ €์žฅ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์„œ๋ฒ„์ƒ์— ๊ฐ์ฒด๋กœ ์กด์žฌํ•œ๋‹ค.
     * ๋”ฐ๋ผ์„œ ์„ธ์…˜์€ ์„œ๋ฒ„์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜์—ฌ ๋ณด์•ˆ์— ์ข‹๊ณ , ๋ฐ์ดํ„ฐ ์šฉ๋Ÿ‰ ํ•œ๊ณ„๊ฐ€ ์—†๋‹ค.
     */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String id = request.getParameter("id");
		String pwd = request.getParameter("pwd");
		System.out.println(id + "\t" + pwd);
		
		// ํ˜„์žฌ session ์ด ์กด์žฌํ•˜๋ฉด ๊ธฐ์กด session ๋ฆฌํ„ดํ•˜๊ณ , ์กด์žฌํ•˜์ง€ ์•Š์€ ์ƒˆ๋กœ์šด session ๋ฆฌํ„ด
		HttpSession session = request.getSession();
		String msg = "";
		if(session.isNew()) {
			msg= "์ƒˆ๋กœ์šด ์„ธ์…˜์ด ์ƒ์„ฑ";
			
			session.setAttribute("id", id);
			session.setAttribute("pwd", pwd);
		}else {
			msg= "๊ธฐ์กด ์„ธ์…˜์ด ๋ฐ˜ํ™˜";
		}
		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print("<html>");
		out.print("<head><title></title></head>");
		out.print("<body>");
		
		out.print("<script type='text/javascript'>");
		out.print("alert('"+ msg +": ๋กœ๊ทธ์ธ ์„ฑ๊ณต');");
		out.print("location.href='http://localhost:8181/webTesting/Servlet/10_example.html';");
		out.print("</script>");
		out.print("</body>");				
		out.print("</html>");				
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}

 

 

 

Example10_SessionGet.java

package com.java.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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;

/**
 * Servlet implementation class Example10_SessionGet
 */
public class Example10_SessionGet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Example10_SessionGet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		if(!session.isNew()) {
			String id= (String) session.getAttribute("id");
			String pwd = (String) session.getAttribute("pwd");
			
			System.out.println(id + "\t" + pwd + "\t" + session.getId());
			
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.print("<html>");
			out.print("<head><title></title></head>");
			out.print("<body>");
			
			out.print("<h3> ์•„์ด๋””:" +id+ "</h3>");
			out.print("<h3> ๋น„๋ฐ€๋ฒˆํ˜ธ:" +pwd+ "</h3>");

			out.print("</body>");				
			out.print("</html>");				
			out.close();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}

 

 


Example10_SessionDel.java

package com.java.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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;

/**
 * Servlet implementation class Example10_SessionDel
 */
public class Example10_SessionDel extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Example10_SessionDel() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		if(request.isRequestedSessionIdValid()) {
			System.out.println("์„ธ์…˜ ์•„์ด๋””๊ฐ€ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.");
		}else {
			System.out.println("์„ธ์…˜ ์•„์ด๋””๊ฐ€ ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
		}
		
		HttpSession session = request.getSession();
		
		if(!session.isNew()) {
			session.invalidate();	//์„ธ์…˜์ข…๋ฃŒ
			
			if(request.isRequestedSessionIdValid()) {
				System.out.println("์„ธ์…˜ ์•„์ด๋””๊ฐ€ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.");
			}else {
				System.out.println("์„ธ์…˜ ์•„์ด๋””๊ฐ€ ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
			}
		}
		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print("<html>");
		out.print("<head><title></title></head>");
		out.print("<body>");
		
		out.print("<h3> ๋กœ๊ทธ์•„์›ƒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. </h3>");
		
		out.print("</body>");
		out.print("</html>");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 

 

 


Example11.java

package com.java.servlet;

import java.io.IOException;
import java.util.Date;

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;

/**
 * Servlet implementation class Example11
 */
@WebServlet("/Example11")
public class Example11 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Example11() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		// ์„ธ์…˜ ์ƒ์„ฑ์‹œ๊ฐ„
		long createTime = session.getCreationTime();
		
		// ์„ธ์…˜ ๋งˆ์ง€๋ง‰ ์ ‘๊ทผ์‹œ๊ฐ„
		long lastTime = session.getLastAccessedTime();
		
		// ์‚ฌ์ดํŠธ ๋จธ๋ฌธ ์‹œ๊ฐ„ = ๋งˆ์ง€๋ง‰ ์ ‘๊ทผ์‹œ๊ฐ„ - ์ƒ์„ฑ์‹œ๊ฐ„ 
		long userTime = (lastTime - createTime)/60000;
		
		Date date = new Date(createTime);
		System.out.println("์ƒ์„ฑ์‹œ๊ฐ„: "+createTime + "\t" + date);
		System.out.println("๋งˆ์ง€๋ง‰ ์ ‘๊ทผ์‹œ๊ฐ„:" +lastTime + "\t" + new Date(lastTime).toString());
		System.out.println("๋จธ๋ฌธ์‹œ๊ฐ„:" +userTime);
		
		/*if(userTime < 5) { // 5๋ถ„์ด ์•ˆ๋˜๋ฉด ์„ธ์…˜์ž๋™์œผ๋กœ ๋Š์–ด์ค€๋‹ค.
			session.invalidate();
		}*/
		
		int max = session.getMaxInactiveInterval() / 60; // 30๋ถ„๋ถ€์—ฌ - ์„œ๋ฒ„
		System.out.println(max + "๋ถ„");
		
		// ์•ก์…˜์ด ์—†์„ ๊ฒฝ์šฐ ์„ธ์…˜ ์‹œ๊ฐ„ ์„ค์ •์œผ๋กœ ์ž๋™ ์ข…๋ฃŒ
		if(request.isRequestedSessionIdValid()) {
			System.out.println("์„ธ์…˜ ์•„์ด๋””๊ฐ€ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.");
		}else {
			System.out.println("์„ธ์…˜ ์•„์ด๋””๊ฐ€ ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
		}
		
		// ์•ก์…˜(๋งˆ์šฐ์Šค๋‚˜ ํ‚ค๋ณด๋“œ)์ด ์—†์„๋•Œ ์„ธ์…˜ ์‹œ๊ฐ„ ์„ค์ •์œผ๋กœ ์ž๋™ ์ข…๋ฃŒ
		session.setMaxInactiveInterval(60); 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

์„ธ์…˜ ํ…Œ์ŠคํŠธ ํด๋ฆญ

 

 

 

 

Example11.java

package com.java.servlet;

import java.io.IOException;
import java.util.Date;

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;

/**
 * @author : ๊น€์†Œํ˜„
 * @Date : 2020. 6. 10.
 * @Description :
 */

public class Example11 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Example11() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		// ์„ธ์…˜ ์ƒ์„ฑ์‹œ๊ฐ„
		long createTime = session.getCreationTime();
		
		// ์„ธ์…˜ ๋งˆ์ง€๋ง‰ ์ ‘๊ทผ์‹œ๊ฐ„
		long lastTime = session.getLastAccessedTime();
		
		// ์‚ฌ์ดํŠธ ๋จธ๋ฌธ ์‹œ๊ฐ„ = ๋งˆ์ง€๋ง‰ ์ ‘๊ทผ์‹œ๊ฐ„ - ์ƒ์„ฑ์‹œ๊ฐ„ 
		long userTime = (lastTime - createTime)/60000;
		
		Date date = new Date(createTime);
		System.out.println("์ƒ์„ฑ์‹œ๊ฐ„: "+createTime + "\t" + date);
		System.out.println("๋งˆ์ง€๋ง‰ ์ ‘๊ทผ์‹œ๊ฐ„:" +lastTime + "\t" + new Date(lastTime).toString());
		System.out.println("๋จธ๋ฌธ์‹œ๊ฐ„:" +userTime);
		
		/*if(userTime < 5) { // 5๋ถ„์ด ์•ˆ๋˜๋ฉด ์„ธ์…˜์ž๋™์œผ๋กœ ๋Š์–ด์ค€๋‹ค.
			session.invalidate();
		}*/
		
		int max = session.getMaxInactiveInterval() / 60; // 30๋ถ„๋ถ€์—ฌ - ์„œ๋ฒ„
		System.out.println(max + "๋ถ„");
		
		// ์•ก์…˜์ด ์—†์„ ๊ฒฝ์šฐ ์„ธ์…˜ ์‹œ๊ฐ„ ์„ค์ •์œผ๋กœ ์ž๋™ ์ข…๋ฃŒ
		if(request.isRequestedSessionIdValid()) {
			System.out.println("์„ธ์…˜ ์•„์ด๋””๊ฐ€ ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.");
		}else {
			System.out.println("์„ธ์…˜ ์•„์ด๋””๊ฐ€ ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
		}
		
		// ์•ก์…˜(๋งˆ์šฐ์Šค๋‚˜ ํ‚ค๋ณด๋“œ)์ด ์—†์„๋•Œ ์„ธ์…˜ ์‹œ๊ฐ„ ์„ค์ •์œผ๋กœ ์ž๋™ ์ข…๋ฃŒ
		session.setMaxInactiveInterval(60); 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 

 

 

ContextLifeCycle.java

package com.java.contextLifeCycle;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @author : ๊น€์†Œํ˜„
 * @Date : 2020. 6. 10.
 * @Description : ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜(webTesting / context) ์ƒ๋ช…์ฃผ๊ธฐ
 * 
 *  
 * <listener>
 * 	<listener-class>com.java.contextLifeCycle.ContextListener</listener-class>
 * </listener>
 *  
 */
public class ContextListener implements ServletContextListener{
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextInitialized~~~~~~~~~~~~~~~~~~~~~~~");
    }
    
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
	System.out.println("contextDestroyed~~~~~~~~~~~~~~~~~~~~~~~");
    }
}

๋ฐ˜์‘ํ˜•
Comments