[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

    http://commons.apache.org

    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/

              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

   ์ฐจํŠธ 

   ๋“ฑ๋“ฑ

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ