๐ป
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Cookie(์ฟ ํค)/ Session(์ธ์ ) ๋ณธ๋ฌธ
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Cookie(์ฟ ํค)/ Session(์ธ์ )
๋ํจ๋ 2020. 6. 9. 12:33Cookie(์ฟ ํค)
- ํด๋ผ์ด์ธํธ ์ฟ ํค ํด๋์ ์ ์ฅ
- ์: ์ฅ๋ฐ๊ตฌ๋
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~~~~~~~~~~~~~~~~~~~~~~~");
}
}
'KITRI > JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JSP ํ๋ก๊ทธ๋๋ฐ - JSTL(JSP Standard Tag Library) ๋ค์ด๋ก๋ / c:set, c:remove (0) | 2020.06.10 |
---|---|
JSP ํ๋ก๊ทธ๋๋ฐ - EL, JSTL (0) | 2020.06.10 |
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Servlet ๊ฐ์ฒด ๋ฒ์ (0) | 2020.06.09 |
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Servlet(์๋ธ๋ฆฟ) (2) (0) | 2020.06.08 |
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Servlet(์๋ธ๋ฆฟ) (1) (0) | 2020.06.05 |