[Day7] JSP/Servlet 7 [12/12]
[ p 205 Chapter 09 ํด๋ผ์ด์ธํธ์ ๋ํ - ์ฟ ํค ] ์ํ๊ด๋ฆฌ *****
[ p 232 Chapter 10 ํด๋ผ์ด์ธํธ์ ๋ํ - ์ธ์ ] ์ํ๊ด๋ฆฌ *****
1. ์ฟ ํค( cookie ) == ํด๋ผ์ด์ธํธ(์น๋ธ๋ผ์ฐ์ )์ ์ ์ฅ๋ ํ ์คํธํ์์ ์ ๋ณด(data)
jsp ์ฟ ํค ์ ์ฅ(๋ช ๋ น)
[ํด] -> ์์ฒญ ->[์]
<-์๋ต <-
์ฟ ํค์ ์ฅ
js ์ฟ ํค์ ์ฅ
[ํด] -> ์ฃผ๋ฌธ์์ฒญ -> [์]
์ฟ ํค(ํจ๊ป) ํ์ฉ~
<-์๋ต <-
[ํด] -> ์์ฒญ ->[์]
์ฟ ํค(ํจ๊ป) ํ์ฉ~
<-์๋ต <-
:
:
:
์ฟ ํค์ฅ๋ฐ๊ตฌ๋
P01
P02
2. js ์ฟ ํค - document.cookie ์์ฑ : ์ฐ๊ธฐ, ์ฝ๊ธฐ
3. jsp/servlet ์์ ์ฟ ํค ์์ฑ, ์ฟ ํค ์ญ์ ๋ฑ๋ฑ
4. ์ฟ ํค ๊ตฌ์ฑ
์ฟ ํค์ด๋ฆ(ํ์)
/์ฟ ํค๊ฐ(ํ์)
/๋ง๋ฃ์์
-1 ๋ธ๋ผ์ฐ์ ๋ซ์ ๋ ์ฟ ํค ์๋ ์ ๊ฑฐ
0 ์ฟ ํค ์ ๊ฑฐ
๋ณ๋์ ๋ง๋ฃ์์ ์ ์ค์ ํ์ง ์์ผ๋ฉด ์น ๋ธ๋ผ์ฐ์ ๋ฅผ ์ข ๋ฃํ ๋ ์ฟ ํค ์๋ ์ ๊ฑฐ
/๋๋ฉ์ธ localhost
/ํจ์ค(๊ฒฝ๋ก) /days07
/
์ ์ฅ๋ ์ฟ ํค๋ ํจ๊ป ๊ฐ์ง๊ณ ์์ฒญํ์ธ์...
http://localhost:80/jspPro/days07/xxx.jsp
http://localhost:80/jspPro/days07/xxx.htm
http://localhost:80/jspPro/days07/xxx.jsp
-> ์์ฒญ์ด ์ผ์ด๋๋ฉด ์ ์ฅ๋ ์ฟ ํค ํจ๊ผ ๊ฐ์ง๊ณ ๊ฐ๋ผ๋ ๋ป
/๋ณด์ ์ต์
5. jsp/servlet ์ฟ ํค ์์ฑ.
p 206 ๊ทธ๋ฆผ 9.1 ์ฟ ํค ๋์ ๋ฐฉ์
ใฑ. jsp ์ฟ ํค ์์ฑ : Cookie ํด๋์ค ์ฌ์ฉํ๋ค.
Cookie ๊ฐ์ฒด ์์ฑํด์ ์๋ต+ ์์ฑ๋ ์ฟ ํค ์ถ๊ฐ
์)
Cookie c = new Cookie( ์ฟ ํค๋ช , ์ฟ ํค๊ฐ);
c.setXXX(); // ๋ง๊ธฐ์์
c.setXXX(); // ๋๋ฉ์ธ
c.setXXX(); // ํจ์ค
response.addCookie( c );
ex02_02.jsp( -> ex02_02_ok.jsp ์ฟ ํค ์์ฑ ), ex02_03.jsp( ์ฟ ํค ํ์ธ)
ex02.jsp
<a href="ex02_02.jsp">jsp ์ฟ ํค ์์ฑ</a><br>
<a href="ex02_03.jsp">jsp ์ฟ ํค ํ์ธ</a><br>
ex02_02.jsp - ์์ฑํ ์ฟ ํค๋ช ,์ฟ ํค๊ฐ์ ์ ๋ ฅ
<form action="ex02_02_ok.jsp">
์์ฑํ ์ฟ ํค ์ด๋ฆ : <input type="text" name="cookieName" value="name"><br>
์์ฑํ ์ฟ ํค ๊ฐ : <input type="text" name="cookieValue" value="ํ๊ธธ๋"><br>
<input type="submit">
</form>
ex02_02_ok.jsp ์ฟ ํค ์์ฑ
<%@page import="java.net.URLEncoder"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- <h3>ex02_02_ok.jsp</h3> -->
<%
String cookieName = request.getParameter("cookieName"); // name
String cookieValue = request.getParameter("cookieValue"); // ํ๊ธธ๋
// jsp ๋ฐ๋์ ์ฃผ์ ์ฌํญ : ์ฟ ํค๊ฐ์ด ํ๊ธ์ธ ๊ฒฝ์ฐ์๋ ์ธ์ฝ๋ฉ(utf-8) ํ์
Cookie c = new Cookie(cookieName, URLEncoder.encode(cookieValue, "UTF-8"));
c.setMaxAge(-1); // ๋ธ๋ผ์ฐ์ ๋ซํ ๋ ์๋ ์ฟ ํค ์ ๊ฑฐ
// c.setDomain(domain); localhost
// c.setPath(uri) /days07
response.addCookie( c );
String location = "ex02.jsp";
response.sendRedirect(location);
%>
ex02_03.jsp - ์์ฑ๋ ์ฟ ํค ํ์ธ
<form>
<%
// ์์ฒญํ ๋ ์ฟ ํค๋ ํจ๊ป ...
Cookie[] cookies = request.getCookies();
for( Cookie c : cookies ){
String cname = c.getName() ; // getXXX()
// [์ฃผ์] ํ๊ธ ์ธ์ฝ๋ฉ -> ๋์ฝ๋ฉ
// URLEncoder.encode(cookieValue, "UTF-8")
String cvalue = URLDecoder.decode( c.getValue(), "UTF-8" ) ;
System.out.printf("maxAge:%d, domain:%s, path:%s\n"
, c.getMaxAge(), c.getDomain(), c.getPath());
%>
<input type="checkbox" name="ckbCookie" value="<%= cname %>"><%= cname %> - <%= cvalue %><br>
<%
} // for
%>
</form>
<a href="ex02.jsp">์ฟ ํค Home</a><br>
<br>
<br>
์์ ํ ์ฟ ํค๋ฅผ ์ ํ์ ํ ํ ์ฟ ํค ์ญ์ , ์์ <br>
<a href="ex02_04.jsp">์ฟ ํค ์ญ์ </a><br>
<a href="ex02_05.jsp">์ฟ ํค ์์ </a><br> <!-- ex02_05_ok.jsp -->
<script>
$(function (){
$("a").on("click", function (){
// ์ฒดํฌ๋ฐ์ค์ ์ฒดํฌํ ์ฟ ํค์ value์์ฑ์ ํ์
ํด์
// ex02_04.jsp?cookie=name&cookie=age
// ckbCookie=name&ckbCookie=age
// ***** (์๊ธฐ) jquery serialize() ๋ฉ์๋
// alert( $("form").serialize() );
event.preventDefault();
var queryString = $("form").serialize();
location.href=$(this).attr( "href" ) + "?" + queryString;
// http://localhost/jspPro/days07/ex02_04.jsp?ckbCookie=name
});
})//
</script>
์ฟ ํค์ด๋ฆ ์ฟ ํค๊ฐ
JSESSIONID - 78C5C6A12641DEA2D94B9F8805396433
ใด ์ ์ํ๊ธฐ๋งํ๋ฉด ๋ฐ๊ธ๋ฐ๋ ์์ด๋
์ฟ ํค ์์ฑํ ์ ์ด ์๊ณ ๋จ์ง ex02.jsp ํ์ด์ง ํ ๋ฒ ์์ฒญ
์๋ฒ ์ธ์ ์์ญ : ์ธ์ ์์ด๋
* jquery serialize() ๋ฉ์๋ *****
์ฟผ๋ฆฌ๋ฌธ ๋ฐ๋ณต๋ฌธ์์ด ํ๋ฒ์ ๋ฃ์ด์ฃผ๋ ๋ฉ์๋
ex02_04.jsp (์ฟ ํค์ญ์ )
<%@page import="java.net.URLEncoder"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- <h3>ex02_04.jsp - ์ฟ ํค ์ญ์ </h3> -->
<%
// js ์ฟ ํค ์ญ์ ? ์ฟ ํค ์๋ก ์์ฑ + ๋ง๋ฃ์์ (๊ณผ๊ฑฐ ์ค์ )
// jsp ์ฟ ํค ์ญ์ ? c.setMaxAge(0) ๋ฐ๋ก ์ญ์
// jsp ์ฟ ํค ์ญ์ ? c.setMaxAge(-1) ๋ธ๋ผ์ฐ์ ๋ซํ ๋ ์ญ์ .
// ์ญ์ ํ ์ฟ ํค์ ์ด๋ฆ : name, age
// http://localhost/jspPro/days07/ex02_04.jsp?ckbCookie=name&ckbCookie=age...
// ( ๊ธฐ์ต ) java ์๋ฌ ๋ฐ์ -> ์ค๋จ์ , ๋๋ฒ๊น
// jsp/servlet "
String [] delCookieNames= request.getParameterValues("ckbCookie");
for( int i = 0 ; i < delCookieNames.length ; i++ ){
String cookieName = delCookieNames[i];
// ๊ทธ ์ฟ ํค ์ญ์
Cookie c = new Cookie(cookieName, "");
c.setMaxAge( 0 ); // ์๋ต๋ฐ์ผ๋ฉด ๋ฐ๋ก ์ฟ ํค ์ญ์
response.addCookie( c );
} // for
// ์ฟ ํค ์ญ์ ์๋ฃ - ๊ฒฝ๊ณ ์ฐฝ alert("์ฟ ํค ์ญ์ ์๋ฃ")
// ex02_03.jsp
// ๊ฒฝ๊ณ ์ฐฝ ๋์ธ ๋ฐฉ๋ฒ์ด ์์ด์..
// response.sendRedirect(location);
%>
<script>
alert("์ฟ ํค ์ญ์ ์๋ฃ");
location.href = "ex02_03.jsp";
</script>
ex02_05.jsp (์ฟ ํค์์ )
<%@page import="java.net.URLDecoder"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 12. ์ค์ 11:36:09</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
</style>
</head>
<body>
<h3>ex02_05.jsp - ์ฟ ํค ์์ (์์ ํ ๊ฐ ์
๋ ฅ..)</h3>
<%!
// ์ ์ธ๋ถ
public String getCookie( String cname , HttpServletRequest request) throws Exception{
String cvalue = null;
Cookie [] cookies = request.getCookies();
for( Cookie c : cookies ){
if( c.getName().equals(cname ) ){
cvalue = URLDecoder.decode( c.getValue() , "UTF-8" ) ;
break;
}
} //
return cvalue;
}
%>
<!-- http://localhost/jspPro/days07/ex02_05.jsp?ckbCookie=name&ckbCookie=age... -->
<form action="ex02_05_ok.jsp">
<%
String [] editCookieNames = request.getParameterValues("ckbCookie");
for( int i = 0 ; i < editCookieNames.length ; i++ ){
String cookieName = editCookieNames[i];
String cooKieValue = getCookie(cookieName, request);
%>
<li> <%= cookieName %> : <input type="text" name="<%= cookieName %>" value="<%= cooKieValue %>"> </li>
<%
} // for
%>
<input type="submit" value="์ฟ ํค ์์ ">
<%@page import="java.net.URLEncoder"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Map.Entry"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.Map"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!--
<h3>ex02_05_ok.jsp ์ค์ ์์ ํ๋ ์ฝ๋ฉ</h3>
http://localhost/jspPro/days07/ex02_05_ok.jsp?name=aaa&tst=bbb
-->
<%
Map<String, String[]> map =request.getParameterMap();
Set<Entry<String, String[]>> set = map.entrySet();
Iterator<Entry<String, String[]>> ir = set.iterator();
while( ir.hasNext() ){
Entry<String, String[]> entry = ir.next();
String cookieName = entry.getKey(); // name tst
String cookieValue = entry.getValue()[0]; // aaa bbb
Cookie c = new Cookie(cookieName, URLEncoder.encode(cookieValue, "UTF-8"));
c.setMaxAge(-1);
response.addCookie( c ); /// set-Cookie ์์ฑ์ผ๋ก ํค๋ ์ ๋ณด๋ก ์ ๋ฌ(์๋ต.)
} // ์์
String location = "ex02_03.jsp";
response.sendRedirect(location);
%>
์ ๋ฆฌ
[jsp ์ฟ ํค ์์ฑ ๋ฐฉ๋ฒ]
1) Cookie c ๊ฐ์ฒด ์์ฑ : ์ฟ ํค๋ช ,์ฟ ํค๊ฐ,๋ง๊ธฐ์์ ,๋๋ฉ์ธ,ํจ์ค,
2) response.addCookie( c )
[jsp ์ฟ ํค ์ญ์ ]
1) ์ญ์ ํ ์ฟ ํค์ ๋ง๊ธฐ์์ ์ ๊ณผ๊ฑฐ๋ก ์ค์ ํด์ ์๋ก์ด ์ฟ ํค ์์ฑ.
[jsp ์ฟ ํค ์์ ]
1) ์์ ํ ์ฟ ํค๊ฐ์ ๊ฐ์ง ์ฟ ํค ๋ฅผ ์์ฑํด์ ๋๊ฐ์ ์ฟ ํค์ด๋ฆ์ด๋ก ์๋ต
p 221
p 221 ์ฟ ํค ์ฒ๋ฆฌ๋ฅผ ์ํ ์ ํธ๋ฆฌํฐ ํด๋์ค
js Cookie.js
jsp/servlet com.util.Cookies.java
-jsp cookie-
package com.util;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
// JSP ์์ ์ฟ ํค ํด๋์ค p 222 ์ฝ๋ฉ.
public class Cookies {
// ํ๋
// key value
// ์ฟ ํค์ด๋ฆ ์ฟ ํค๊ฐ์ฒด
// name Cookie
// age Cookie
public Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
// ๋ํดํธ ์์ฑ์
public Cookies(HttpServletRequest request) {
// ๋ชจ๋ ์ฟ ํค๋ฅผ ์ป์ด์์ cookieMap์์ ์์ ์ถ๊ฐ
Cookie [] cookies = request.getCookies();
if ( cookies != null ) {
for (int i = 0; i < cookies.length; i++) {
String cname = cookies[i].getName();
// String cvalue = cookies[i].getValue();
this.cookieMap.put(cname, cookies[i]);
}
} // if
} //
public Cookie getCookie(String cname) {
return this.cookieMap.get(cname);
}
public boolean exists(String cname) {
return this.cookieMap.get(cname) != null;
}
public static Cookie createCookie(String cname, String cvalue) throws UnsupportedEncodingException {
Cookie cookie = new Cookie(cname, URLEncoder.encode( cvalue , "UTF-8") );
return cookie;
}
public static Cookie createCookie(String cname, String cvalue, String path, int expiry) throws UnsupportedEncodingException {
Cookie cookie = new Cookie(cname, URLEncoder.encode( cvalue , "UTF-8") );
cookie.setPath(path);
cookie.setMaxAge(expiry);
return cookie;
}
public static Cookie createCookie(String cname, String cvalue, String domain , String path, int expiry) throws UnsupportedEncodingException {
Cookie cookie = new Cookie(cname, URLEncoder.encode( cvalue , "UTF-8") );
cookie.setDomain(domain);
cookie.setPath(path);
cookie.setMaxAge(expiry);
return cookie;
}
// cvalue
public String getValue(String cname) throws UnsupportedEncodingException {
String cvalue = null;
Cookie cookie = this.cookieMap.get(cname);
if (cookie != null) {
cvalue = URLDecoder.decode( cookie.getValue() , "UTF-8" );
}
return cvalue;
}
}
์ํ๊ด๋ฆฌ (์ฟ ํค) - ์ฅ๋ฐ๊ตฌ๋
์ํ๊ด๋ฆฌ (์ธ์ ) - ์ฅ๋ฐ๊ตฌ๋
์ํ๊ด๋ฆฌ (ํ ์ด๋ธ) - ์ฅ๋ฐ๊ตฌ๋
๋ฑ๋ฑ
*** [ ์ฟ ํค๋ฅผ ์ฌ์ฉํด์ ์ธ์ฆ(๋ก๊ทธ์ธ) ์ฒ๋ฆฌ~~ ์์ ]
ใฑ. com.util.Cookies.java
ใด. ex03_default.jsp (๋ฉ์ธํ์ด์ง)
<%@page import="com.util.Cookies"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/WEB-INF/inc/auth.jspf" %>
<%--
<%
// ์คํฌ๋ฆฝํธ๋ฆฟ
// ์ธ์ฆ(๋ก๊ทธ์ธ)๋์๋ค๋ฉด ์ฟ ํค์ด๋ฆ(auth) - ์ฟ ํค๊ฐ( ๋ก๊ทธ์ธID ) ๋ฅผ ์ ์ฅ(์ฝ์)
String cname = "auth";
String logonID = null;
// com.util.Cookies.java
// cookieMap key ์ฟ ํค์ด๋ฆ value ์ฟ ํค๊ฐ์ฒด
Cookies cookies = new Cookies(request);
if( cookies.exists(cname) ){
// ์ธ์ฆ๋ฐ์๋ค( ๋ก๊ทธ์ธ ํ๋ค. )
logonID = cookies.getValue(cname) ;
}
%>
--%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2022. 12. 12. ์คํ 2:10:17</title>
<link rel="shortcut icon" type="image/x-icon" href="../images/SiSt.ico">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
div{
border: 1px solid gray;
width:300px;
height:100px;
padding:20px;
}
</style>
</head>
<body>
<h3>ex03_default.jsp - ๋ฉ์ธ ํ์ด์ง</h3>
<pre>
p 226 ์ฟ ํค๋ฅผ ์ฌ์ฉํ ๋ก๊ทธ์ธ ์ํ ์ ์ง
</pre>
<%
if( logonID == null ){ // ๋ก๊ทธ์ธ X
%>
<div id="logon">
<form action="ex03_logon.jsp">
์์ด๋ : <input type="text" name="id" value="admin" /><br>
๋น๋ฐ๋ฒํธ : <input type="password" name="passwd" value="1234" /><br>
<input type="submit" value="๋ก๊ทธ์ธ" />
<input type="button" value="ํ์๊ฐ์
" />
<!--
๋ก๊ทธ์ธ ์คํจํ๊ณ ๋ค์ ex03_default.jsp ๋ก ์ด๋ํ์ ๋ ๋ณด์ฌ์ง ํ๊ทธ
ex03_default.jsp?error
-->
<%
String error = request.getParameter("error");
if( error != null ){
%>
<span style="color:red">๋ก๊ทธ์ธ ์คํจํ์ต๋๋ค.</span>
<%
} //
%>
<script>
$(function (){
$("#logon span").fadeOut(5000);
})
</script>
</form>
</div>
<%
}else{ // ๋ก๊ทธ์ธ O
%>
<div id="logout">
[<%= logonID %>]๋ ํ์ํฉ๋๋ค<br>
<a href="ex03_logout.jsp">๋ก๊ทธ์์</a>
</div>
<%
}
%>
<a href="/jspPro/cstvsboard/list.htm">๊ฒ์ํ</a><br>
<a href="#">๊ณต์ง์ฌํญ</a><br>
<%
if( logonID != null ){
%>
<!-- ๋ฐ๋์ ์ธ์ฆ(๋ก๊ทธ์ธ)์ ๋ฐ์์ผ์ง ์ฌ์ฉํ ์ ์๋ ๋ฉ๋ด -->
<a href="#">์ผ์ ๊ด๋ฆฌ</a><br>
<a href="#">์๋ฃ์ค</a><br>
<!-- ์ธ์ฆ + ๊ด๋ฆฌ์ ๊ถํ์ด ์์ด์ผ ์ฌ์ฉํ ์ ์๋ ๋ฉ๋ด -->
<%
if( logonID.equals("admin") ){
%>
<a href="#">๊ธ์ฌ๊ด๋ฆฌ</a><br>
<a href="#">๊ทผํ๊ด๋ฆฌ</a><br>
<%
} // if
} // if
%>
ใท. ex03_logon.jsp
<%@page import="com.util.Cookies"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- <h3>ex03_logon.jsp</h3> -->
<%
// DB member ํ
์ด๋ธ id/pwd ์ฒดํฌ
String id= request.getParameter("id");
String passwd= request.getParameter("passwd");
//
if( id.equals("admin") && passwd.equals("1234") ){
// ๊ด๋ฆฌ์ ์ธ์ฆ
Cookie c = Cookies.createCookie("auth", id, "/", -1);
response.addCookie(c);
String location = "ex03_default.jsp";
response.sendRedirect(location);
}else if(id.equals("hong") && passwd.equals("1234") ){
// ์ผ๋ฐ๊ณ์ฆ์ผ๋ก ์ธ์ฆ
Cookie c = Cookies.createCookie("auth", id, "/", -1);
response.addCookie(c);
String location = "ex03_default.jsp";
response.sendRedirect(location);
}else if(id.equals("park") && passwd.equals("1234") ){
// ์ผ๋ฐ๊ณ์ฆ์ผ๋ก ์ธ์ฆ
Cookie c = Cookies.createCookie("auth", id, "/", -1);
response.addCookie(c);
String location = "ex03_default.jsp";
response.sendRedirect(location);
}else{ // ์ธ์ฆ ์คํจ
String location = "ex03_default.jsp?error";
response.sendRedirect(location);
}
%>
ใน. ex03_logout.jsp
<%@page import="com.util.Cookies"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/WEB-INF/inc/auth.jspf" %>
<!-- <h3>ex03_logout.jsp</h3> -->
<%--
<%
String cname = "auth";
String logonID = null;
Cookies cookies = new Cookies(request);
if( cookies.exists(cname) ){
logonID = cookies.getValue(cname) ;
}
%>
--%>
<%
// ์ฟ ํค(auth) ์ญ์ : ๋ง๋ฃ์์ 0 + ์ฟ ํค ์์ฑ
Cookie c = Cookies.createCookie("auth", "", "/", 0);
response.addCookie(c);
%>
<script>
alert("[<%= logonID %>]๋ ๋ก๊ทธ ์์ ํ์
จ์ต๋๋ค.");
location.href = "ex03_default.jsp";
</script>
์ด๋ค jsp ํ์ด์ง๋ฅผ ์์ฒญํ ๋ ๋ง๋ค "๋ก๊ทธ์ธ ํ๋ ์ง ์ฌ๋ถ๋ฅผ ์ฒดํฌ""๋ฅผ ํด์ผ๋๋ค๊ณ ํ๋ค๋ฉด ๋ชจ๋ํ ์์ผ์ include ์ง์์๋ฅผ ์ด๋ด ๋ ์ฌ์ฉํ๋ฉด ํธ๋ฆฌํ๋ค. ( auth.jspf ํ์ผ ์ถ๊ฐ) |
WEB-INF > inc ํด๋
auth.jspf
<%@page import="com.util.Cookies"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
// [WEB-INF/inc/auth.jspf ]
String cname = "auth";
String logonID = null;
Cookies cookies = new Cookies(request);
if( cookies.exists(cname) ){
logonID = cookies.getValue(cname) ;
}
%>
์ถ๊ฐ
<%@ include file="/WEB-INF/inc/auth.jspf" %>
์ปค๋ฅ์ ํ (p415)
์ง๊ธ๊น์ง DB ์ฐ๋ํ ๋
Connection conn = DBConn.getConnection();
:
:
DBCon.close();
๋ฌธ์ ์ ? ๋์ ์ ์์๊ฐ ๋ง์ ๋๋ ์ฑ๋ฅ์ด ๋จ์ด์ง๋ค.
์๋ต์ด ๋ฆ๋ค. (ํ์ด์ง๋ง๋ค ์์ฑํด์)
=> ์ปค๋ฅ์ ํ ๊ธฐ๋ฒ์ ์ฌ์ฉํ๋ค.
[ ์ปค๋ฅ์ ํ( Connection Pool )]
DB ์ฐ๊ฒฐํ๋ Connection ๊ฐ์ฒด๋ฅผ ๋ฏธ๋ฆฌ ์์ฑํด์ ํ(Pool) ์ ์ ์ฅ
-> ๊บผ๋ด์ ์ฌ์ฉ
-> ์ฌ์ฉ์ด ๋๋๋ฉด ๋ค์ ํ(Pool)์ ๋ฐํ
[์ปค๋ฅ์ ํ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ]
ใฑ. p417. DBCP๋ฅผ ์ด์ฉํด์ ์ปค๋ฅ์ ํ ์ฌ์ฉํ๊ธฐ. X
3๊ฐ์ ๋ค์ด๋ก๋ ํ์ผ์ WEB-INF/lib ํด๋์ ๋ณต์ฌ ๋ถ์ด๊ธฐ
ํ์ด์ง๊ฐ์ - ํํ์ด์ง์ dbcp ํด๋ฆญ - release์๋ ๋ค์ด๋ก๋ ํด๋ฆญ
-> binaries
dbcp, pool, logging ์ธ๊ฐ ๋ค์ด
*ํ์ง๋ง ์ด๊ฑฐ ์ฌ์ฉ์ํ ๊ฑฐ์!
ใด. WAS(ํฐ์บฃ)์ ์ด์ฉํ ์ปค๋ฅ์ ํ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ O
- p 418 ์ฝ๋ฉ X
- jar X
- ์๋ธ๋ฆฟ ์ค์ X
(1) jar ํ์ผ ์ถ๊ฐ.
C:\apache-tomcat-8.5.83\lib\tomcat-dbcp.jar -> WEB-INF/lib/ ๋ณต์ฌ ๋ถ์ด๊ธฐ.
(2) ์ปค๋ฅ์ ํ ์ด๊ธฐํ ์ค์ .
ํฐ์บฃ(WAS) ์ด ์์๋ ๋
๋ฏธ๋ฆฌ ํ ์์ ์ปค๋ฅ์ ๊ฐ์ฒด๋ฅผ ์์ฑ-> ์ค๋น
์ค์ ..
https://tomcat.apache.org/tomcat-8.5-doc/jndi-datasource-examples-howto.html#Oracle_8i,_9i_&_10g
ใด documentation - 8.5 - 10)jdbcdatasources - database connection pool(dbcp2) configurations - 8i9i 10g
1. Context configuration
META-INF ํด๋
ใด context.xml ํ์ผ ์ถ๊ฐ
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:mysid"
username="scott" password="tiger"
p 423 ํ 14.4
maxTotal="20" ํ์ด ๊ด๋ฆฌํ๋ ์ปค๋ฅ์
์ ์ต๋ ๊ฐฏ์ ( ์์์ด๋ฉด ์ ํ ์๋ค. )
maxIdle="10" ํ์ด ๋ณด๊ดํ ์ ์๋ ์ต๋ ์ ํด ๊ฐฏ์ ( ์์์ด๋ฉด ์ ํ ์๋ค. )
maxWaitMillis="-1" ๋ชจ๋ ์ปค๋ฅ์
๊ฐ์ฒด๊ฐ ์ฌ์ฉ ์ค์ด๋ผ๋ฉด
๋๊ธฐ ํ๋ ์๊ฐ(ms)
์์๋ ๋ฌดํ์ ๋๊ธฐ.
/>
</Context>
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/myoracle"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:xe"
username="scott"
password="tiger"
maxTotal="20"
maxIdle="10"
maxWaitMillis="-1"/>
</Context>
web.xml
<!-- DBCP ์ค์ -->
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
-jsp-
<%
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
%>
ใดimport
<%@page import="com.util.ConnectionProvider"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
java-com-util
ConnectionProvider.java ์ถ๊ฐ
package com.util;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class ConnectionProvider {
public static Connection getConnection() throws NamingException, SQLException {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
return conn;
}
}
<%
Connection conn2 = ConnectionProvider.getConnection();
%>
DBCP conn2 = <%= conn2 %>
<%
conn2.close();
%>
[p 232 Chapter 10 ํด๋ผ์ด์ธํธ์์ ๋ํ : ์ธ์ ]
[ํด] -> ์์ฒญ -> [์] ์ค์ฝํ(์์ญ)
<-์๋ต<- ์ธ์ (Session) ์ธ์ฆ,์ฅ๋ฐ๊ตฌ๋
์บ๋น๋ท(101๋ฒ) + ์ด์
์ ์ฅ๊ณต๊ฐ JSESSIONID
๊ณ ์ ๊ฐ : JSESSIONID
* ์ฟ ํค์์ ์ฐจ์ด : ์๋ฒ์ ์ ์ฅ๋จ (ํด๋น ๋ธ๋ผ์ฐ์ ์ ๋ ๋ฆฝ์ ์ธ ์บ๋น๋ท์ด ์๊ธฐ๋ ๊ฒ๊ณผ ๊ฐ์)
1. ์ธ์ ์ธ์ ์์ฑ ? ํ๋์ ์น๋ธ๋ผ์ฐ์ -> ์ฒซ๋ฒ์งธ ์์ฒญ [์๋ฒ ์ธ์ ==์บ๋น๋ท]
<- ์๋ต
JSESSINID ์ธ์ ID (์ฟ ํค๋ก ์๋ต)
2. ์ธ์ ์ธ์ ์ข ๋ฃ ? ์ฝ๋ฉ์ผ๋ก ๊ฐ์ ์ธ์ ์ข ๋ฃ.
์์ฒญ-> X , 20๋ถ ์๋ค ์๋ ์ข ๋ฃ
3. session="true" ์ธ์ ์ฌ์ฉ์ ๊ธฐ๋ณธ ์ค์ ๋์ด์ ธ ์๋ค.
4๊ฐ์ง ์ค์ฝํ( scope) - getAttribute(),. setAttribute()
pageContext scope
request
session
application "
4. p 234 ํ 10.1
getId()
getCreationTime() ์ธ์ ์์ฑ๋ ์๊ฐ ms 1970.1.1~
getLastAccessedTime() ๋ง์ง๋ง ์ ์์๊ฐ ms "
<%
// session JSP 9๊ฐ์ง ๊ธฐ๋ณธ ๊ฐ์ฒด ์ค ํ๋.
String sid = session.getId();
long ms = session.getCreationTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date();
d.setTime(ms);
long lastms = session.getLastAccessedTime();
%>
์ธ์
ID = <%= sid %><br>
์ธ์
์์ฑ๋ ์ง = <%= sdf.format(d) %><br>
<%
d.setTime(lastms);
%>
์ธ์
๋ง์ง๋ง ์ ์ ๋ ์ง = <%= sdf.format(d) %><br>
* ๋ณ์ ์ ์ธ์์ด ๊ฐ์ฒด ๋ฐ๋ก ์ฌ์ฉ -> JSP 9๊ฐ ๊ธฐ๋ณธ๊ฐ์ฒด ์ค ํ๋
--------------------------------------------------------------------------------
jsp ํ์ด์ง : session ๊ธฐ๋ณธ ๊ฐ์ฒด ์ฌ์ฉ.
servlet : ์ธ์ ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ.. ...
<a href="ex05_02.htm">์๋ธ๋ฆฟ ํธ์ถ</a>
package days07;
import java.io.IOException;
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("/days07/ex05_02.htm")
public class Ex05_Session extends HttpServlet {
private static final long serialVersionUID = 1L;
public Ex05_Session() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// System.out.println("> Ex05_Session.doGet() ํธ์ถ๋จ...");
// jspํ์ด์ง์์ ์ธ์
์ ์ฌ์ฉํ ๋๋ ๊ธฐ๋ณธ๊ฐ์ฒด : session
// [์๋ธ๋ฆฟ์์๋ ์ธ์
๊ฐ์ฒด๋ฅผ ์ป์ด์ค๋ ๋ฐฉ๋ฒ] p 241
// 1. HttpSession session = request.getSession();
// ใด ์ธ์
์ด ์กด์ฌํ์ง ์์ผ๋ฉด ์๋ก์ด ์ธ์
๊ฐ์ฒด๋ฅผ ์์ฑํด์ ๋ฐํ์ ํ๊ณ ,
// 2. HttpSession session = request.getSession(false);
// ใด ์ธ์
์ด ์กด์ฌํ์ง ์์ผ๋ฉด null์ ๋ฐํํ๋ค.
HttpSession session = request.getSession( false );
if( session == null) {
// X
}else {
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
* ์๋ธ๋ฆฟ์์ ์ธ์ ์ฌ์ฉํ๋ ๋ฒ (๊ทธ๋ฅ ์คฌ์๋ -> ์ธ์ ์กด์ฌํ์ง ์์ผ๋ฉด ์๋ก์ด ์ธ์ ๊ฐ์ฒด ์์ฑํด์ ๋ฐํ, false -> ๋ ๋ฐํ)
p 242 ์ธ์ ์ ์ฌ์ฉํ ๋ก๊ทธ์ธ ์ํ ์ ์ง
์ฟ ํค ์ธ์ฆ์ฒ๋ฆฌ ์ธ์ ์ธ์ฆ์ฒ๋ฆฌ
ex03_default.jsp
ex03_logon.jsp
ex03_logout.jsp
EL, JSTL, *** Chapter 18 MVC ***, ํํฐ ,1~2 ๊ฐ ๊ฐ๋
ํ์๊ฐ์ ์์
์๋ฃ์ค ์์
๋ฐฉ๋ช ๋ก ์์
๋ต๋ณ๊ฒ์ํ ๋ฑ๋ฑ
๊ตฌ๊ธ๋งต
ํ์ผ์ ๋ก๋ + ์๋ฃ์ค
jquery ajax
์ฐจํธ
๋ฑ๋ฑ
์ต๊ทผ๋๊ธ