Skip to content

JangHyeonJun2/Java-servlet-basic2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

23 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Java-servlet-basic2

9.1 ์ฟ ๊ธฐ์™€ ์„ธ์…˜ ์•Œ์•„๋ณด๊ธฐ

์›น ํŽ˜์ด์ง€ ์—ฐ๊ฒฐ ๊ธฐ๋Šฅ

๋ณดํ†ต ์›น ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ •๋ณด๋Š” ์„œ๋ธ”๋ฆฟ์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๊ฐ€์ ธ์˜จ๋‹ค.๊ทธ๋Ÿฌ๋‚˜ ๋™์‹œ ์‚ฌ์šฉ์ž ์ˆ˜๊ฐ€ ๋งŽ์•„์ง€๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™์†๋„๋„ ์˜ํ–ฅ์„ ๋ฐ›๊ฒŒ ๋˜๋ฏ€๋กœ ์ •๋ณด์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ์–ด๋–ค ์ •๋ณด๋“ค์€ ํด๋ผ์ด์–ธํŠธ PC๋‚˜ ์„œ๋ฒ„์˜ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•ด๋‘๊ณ  ์‚ฌ์šฉํ•˜๋ฉด ์ข€ ๋” ํ”„๋กœ๊ทธ๋žจ์„ ๋น ๋ฅด๊ฒŒ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ์ด ์žฅ์—์„œ๋Š” ๊ทธ ๋ฐฉ๋ฒ•๊ณผ ํ•จ๊ป˜ ์„œ๋ธ”๋ฆฟ์ด ๋กœ๊ทธ์ธ ์‹œ ์‚ฌ์šฉ์ž์˜ ๋กœ๊ทธ์ธ ์ƒํƒœ๋ฅผ ์ผ์ •ํ•˜๊ฒŒ ์œ ์ง€์‹œํ‚ค๋Š” ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด ์‚ดํŽด ๋ณด๊ฒ ๋‹ค.


9.1.1 ์„ธ์…˜ ํŠธ๋ž˜ํ‚น

์‡ผํ•‘๋ชฐ์„ ์ด์šฉํ•˜๋Š” ์ผ๋ฐ˜ ์‚ฌ์šฉ์ž๋“ค์€ ๋กœ๊ทธ์ธ ์ƒํƒœ๋ฅผ ๊ฐ๊ฐ์˜ ์›น ํŽ˜์ด์ง€๋“ค์ด ์ž๋™์ ์œผ๋กœ ์•Œ๊ณ  ์žˆ์„ ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ HTTP ํ”„๋กœํ† ์ฝœ ๋ฐฉ์‹์œผ๋กœ ํ†ต์‹ ํ•˜๋Š” ์›น ํŽ˜์ด์ง€๋“ค์€ ์„œ๋กœ ์–ด๋–ค ์ •๋ณด๋„ ๊ณต์œ ํ•˜์ง€ ์•Š๋Š”๋‹ค.

์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ ์›น ํŽ˜์ด์ง€ ์‚ฌ์ด์˜ ์ƒํƒœ๋‚˜ ์ •๋ณด๋ฅผ ๊ณต์œ ํ•˜๋ ค๋ฉด ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์„ธ์…˜ํŠธ๋ž˜ํ‚น ์ด๋ผ๋Š” ์›น ํŽ˜์ด์ง€ ์—ฐ๊ฒฐ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

![แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2019-09-05 แ„‹แ…ฉแ„’แ…ฎ 9.45.57](/Users/janghyeonjun/Desktop/แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2019-09-05 แ„‹แ…ฉแ„’แ…ฎ 9.45.57.png)

HTTP ํ”„๋กœํ† ์ฝœ์€ ์„œ๋ฒ„-ํด๋ผ์ด์–ธํŠธ ํ†ต์‹  ์‹œ stateless ๋ฐฉ์‹์œผ๋กœ ํ†ต์‹ ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ƒˆ ์›น ํŽ˜์ด์ง€๋ฅผ ์—ด๋ฉด ๊ธฐ์กด์˜ ์›น ํŽ˜์ด์ง€๋‚˜ ์„œ๋ธ”๋ฆฟ์— ๊ด€ํ•œ ์–ด๋–ค ์—ฐ๊ฒฐ ์ •๋ณด๋„ ์ƒˆ ์›น ํŽ˜์ด์ง€์—์„œ๋Š” ์•Œ ์ˆ˜ ์—†๋‹ค.

์ •๋ฆฌํ•˜๋ฉด

HTTPํ”„๋กœํ† ์ฝœ์€ ๊ฐ ์›น ํŽ˜์ด์ง€์˜ ์ƒํƒœ๋‚˜ ์ •๋ณด๋ฅผ ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋“ค๊ณผ ๊ณต์œ ํ•˜์ง€ ์•Š๋Š” stateless ๋ฐฉ์‹์œผ๋กœ ํ†ต์‹ ์„ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์›น ํŽ˜์ด์ง€๋‚˜ ์„œ๋ธ”๋ฆฟ๋ผ๋ฆฌ ์ƒํƒœ๋‚˜ ์ •๋ณด๋ฅผ ๊ณต์œ ํ•˜๋ ค๋ฉด ์›น ํŽ˜์ด์ง€๋ฅผ ์—ฐ๋™ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • ํƒœ๊ทธ: HTML์˜ ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด ์›น ํŽ˜์ด์ง€๋“ค ์‚ฌ์ด์˜ ์ •๋ณด๋ฅผ ๊ณต์œ ํ•œ๋‹ค.
  • URL Rewriting: GET ๋ฐฉ์‹์œผ๋กœ URL ๋’ค์— ์ •๋ณด๋ฅผ ๋ถ™์—ฌ์„œ ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋กœ ์ „์†กํ•œ๋‹ค.
  • ์ฟ ๊ธฐ: ํด๋ผ์ด์–ธํŠธ PC์˜ Cookie ํŒŒ์ผ์— ์ •๋ณด๋ฅผ ์ €์žฅํ•œ ํ›„ ์›น ํŽ˜์ด์ง€๋“ค์ด ๊ณต์œ ํ•œ๋‹ค.
  • ์„ธ์…˜: ์„œ๋ฒ„ ๋ฉ”๋ชจ๋ฆฌ์— ์ •๋ณด๋ฅผ ์ €์žฅํ•œ ํ›„ ์›น ํŽ˜์ด์ง€๋“ค์ด ๊ณต์œ ํ•œ๋‹ค.

9.2 ํƒœ๊ทธ์™€ URL Rewriting ์ด์šฉํ•ด ์›น ํŽ˜์ด์ง€ ์—ฐ๋™ํ•˜๊ธฐ

ํƒœ๊ทธ๋Š” ๋ธŒ๋ผ์šฐ์ €์—๋Š” ํ‘œ์‹œ๋˜์ง€ ์•Š์ง€๋งŒ ๋ฏธ๋ฆฌ ์ €์žฅ๋œ ์ •๋ณด๋ฅผ ์„œ๋ธ”๋ฆฟ์„ ์ „์†กํ•  ์ˆ˜ ์žˆ๋‹ค.

1.login.html์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.๋กœ๊ทธ์ธ์ฐฝ์—์„œ ID์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ฏธ๋ฆฌ ํƒœ๊ทธ์— ์ €์žฅ๋œ ์ฃผ์†Œ,์ด๋ฉ”์ผ ,ํœด๋Œ€ํฐ ๋ฒˆํ˜ธ๋ฅผ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์ „์†กํ•œ๋‹ค.

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <title>๋กœ๊ทธ์ธ ์ฐฝ</title>
</head>
<body>
    <form name="frmLogin" method="post" action="login" enctype="UTF-8">
        ์•„์ด๋””:<input type="text" name="user_id">
        ๋น„๋ฐ€๋ฒˆํ˜ธ:<input type="password" name="user_pw"><br>
        <input type="submit" value="๋กœ๊ทธ์ธ">
        <input type="reset" value="๋‹ค์‹œ ์ž…๋ ฅ">
        <input type="hidden" name="user_address" value="์„œ์šธ์‹œ ์„ฑ๋ถ๊ตฌ">
        <input type="hidden" name="user_email" value="[email protected]">
        <input type="hidden" name="user_hp" value="010-1111-1111">
    </form>
</body>
</html>

2.LoginServletํด๋ž˜์Šค๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค. getParameter()๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ „์†ก๋œ ํšŒ์› ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ ํ›„ ๋ธŒ๋ผ์šฐ์ €๋กœ ๋‹ค์‹œ ์ถœ๋ ฅํ•œ๋‹ค.

package sec01.ex01;

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 java.io.IOException;
import java.io.PrintWriter;
//@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        System.out.println("init ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        String user_id = req.getParameter("user_id");
        String user_pw = req.getParameter("user_pw");
        String user_address = req.getParameter("user_address");
        String user_email = req.getParameter("user_email");
        String user_hp = req.getParameter("user_hp");

        String data = "์•ˆ๋…•ํ•˜์„ธ์š”!<br> ๋กœ๊ทธ์ธ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.<br><br>";
        data+="<html><body>";
        data+="์•„์ด๋””: "+user_id;
        data+="<br>";
        data+="ํŒจ์Šค์›Œ๋“œ: "+user_pw;
        data+="<br>";
        data+="์ฃผ์†Œ: "+user_address;
        data+="<br>";
        data+="์ด๋ฉ”์ผ: "+user_email;
        data+="<br>";
        data+="์ „ํ™”๋ฒˆํ˜ธ: "+user_hp;
        data+="</body></html>";
        out.print(data);
    }

    @Override
    public void destroy() {
        System.out.println("destroy ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ");
    }
}

9.2.2 URL Rewriting์„ ์ด์šฉํ•œ ์„ธ์…˜ ํŠธ๋ž˜ํ‚น ์—ฐ์Šต

  • ์ด๋ฒˆ์—๋Š” URL Rewriting์„ ์ด์šฉํ•ด ๋กœ๊ทธ์ธ์ฐฝ์—์„œ ์ž…๋ ฅ ๋ฐ›์€ ID์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋‹ค๋ฅธ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์ „์†กํ•˜์—ฌ ๋กœ๊ทธ์ธ ์ƒํƒœ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ๋‹ค.

    **1.**LoginServlet.java ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค. ๋กœ๊ทธ์ธ์ฐฝ์—์„œ ์ž…๋ ฅ๋ฐ›์€ ID์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํƒœ๊ทธ์˜ ๋‘ ๋ฒˆ์งธ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋ณด๋‚ด๊ธฐ๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋กœ๊ทธ์ธ์ฐฝ์—์„œ ์ž…๋ ฅํ•œ ID์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ์ •๋ณด๋“ค์„ GET๋ฐฉ์‹์„ ์ด์šฉํ•ด ๋‘ ๋ฒˆ์งธ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์ „์†กํ•œ๋‹ค.

    package sec01.ex02;
    
    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 java.io.IOException;
    import java.io.PrintWriter;
    import java.net.URLEncoder;
    
    @WebServlet("/login")
    public class LoginServlet extends HttpServlet {
        @Override
        public void init() throws ServletException {
            System.out.println("init ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter out = resp.getWriter();
            String user_id = req.getParameter("user_id");
            String user_pw = req.getParameter("user_pw");
            String user_address = req.getParameter("user_address");
            String user_email = req.getParameter("user_email");
            String user_hp = req.getParameter("user_hp");
    
            String data = "์•ˆ๋…•ํ•˜์„ธ์š”!<br> ๋กœ๊ทธ์ธ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.<br><br>";
            data+="<html><body>";
            data+="์•„์ด๋””: "+user_id;
            data+="<br>";
            data+="ํŒจ์Šค์›Œ๋“œ: "+user_pw;
            data+="<br>";
            data+="์ฃผ์†Œ: "+user_address;
            data+="<br>";
            data+="์ด๋ฉ”์ผ: "+user_email;
            data+="<br>";
            data+="์ „ํ™”๋ฒˆํ˜ธ: "+user_hp;
            data+="<br>";
            out.print(data);
    
            user_address = URLEncoder.encode(user_address,"utf-8");//GET๋ฐฉ์‹์œผ๋กœ ํ•œ๊ธ€์„ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด ์ธ์ฝ”๋”ฉํ•œ๋‹ค.
            out.print("<a href='/second?user_id="+user_id+"&user_pw="+user_pw+"&user_address="+user_address+"'>๋‘ ๋ฒˆ์งธ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋ณด๋‚ด๊ธฐ</a>");//a ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด ๋งํฌ ํด๋ฆญ ์‹œ ์„œ๋ธ”๋ฆฟ
                                                                                                                                // /second๋กœ ๋‹ค์‹œ ๋กœ๊ทธ์ธ ์ •๋ณด๋ฅผ ์ „์†กํ•œ๋‹ค.
            data+="</body></html>";
        }
    
        @Override
        public void destroy() {
            System.out.println("destroy ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ");
        }
    }

    **2.**SecondServlet.java๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.์ฒซ ๋ฒˆ์งธ ์„œ๋ธ”๋ฆฟ์—์„œ ์ „์†กํ•œ ๋ฐ์ดํ„ฐ ์ค‘ ID์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ ธ์™”์œผ๋ฉด ์ด๋ฏธ ์ฒซ ๋ฒˆ์งธ ์„œ๋ธ”๋ฆฟ์—์„œ ๋กœ๊ทธ์ธํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ๋กœ๊ทธ์ธ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋„๋ก ํ•œ๋‹ค.

    package sec01.ex02;
    
    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 java.io.IOException;
    import java.io.PrintWriter;
    
    @WebServlet("/second")
    public class SecondServlet extends HttpServlet {
        @Override
        public void init() throws ServletException {
            System.out.println("init ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ");
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter out = resp.getWriter();
    
            /*
             * ์ฒซ ๋ฒˆ์งธ ์„œ๋ธ”๋ฆฟ์—์„œ ์ „์†กํ•œ ๋กœ๊ทธ์ธ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
             */
            String user_id = req.getParameter("user_id");
            String user_pw = req.getParameter("user_pw");
            String user_address = req.getParameter("user_address");
    
            out.println("<html><body>");
            if(user_id!=null && user_id.length() != 0){
                out.println("์ด๋ฏธ ๋กœ๊ทธ์ธ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.<br><br>");
                out.println("์ฒซ ๋ฒˆ์งธ ์„œ๋ธ”๋ฆฟ์—์„œ ๋„˜๊ฒจ์ค€ ์•„์ด๋”” : " + user_id+"<br>");
                out.println("์ฒซ ๋ฒˆ์งธ ์„œ๋ธ”๋ฆฟ์—์„œ ๋„˜๊ฒจ์ค€ ๋น„๋ฐ€๋ฒˆํ˜ธ: "+user_pw+"<br>");
                out.println("์ฒซ ๋ฒˆ์งธ ์„œ๋ธ”๋ฆฟ์—์„œ ๋„˜๊ฒจ์ค€ ์ฃผ์†Œ: " +user_address+"<br>");
                out.println("</body></html>");
            }else {
                out.println("๋กœ๊ทธ์ธ ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.<br><br>");
                out.println("๋‹ค์‹œ ๋กœ๊ทธ์ธํ•˜์„ธ์š”!");
                out.println("<a href='login.html'>๋กœ๊ทธ์ธ ์ฐฝ์œผ๋กœ ์ด๋™ํ•˜๊ธฐ</>");
            }
        }
    
        @Override
        public void destroy() {
            System.out.println("destroy ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ");
        }
    }
    • 9.3 ์ฟ ๊ธฐ๋ฅผ ์ด์šฉํ•œ ์›น ํŽ˜์ด์ง€ ์—ฐ๋™ ๊ธฐ๋Šฅ

    • ์ฟ ํ‚ค๋ž€ ์›น ํŽ˜์ด์ง€๋“ค ์‚ฌ์ด์˜ ๊ณต์œ  ์ •๋ณด๋ฅผ ํด๋ผ์ด์–ธํŠธ PC์— ์ €์žฅํ•ด ๋†“๊ณ  ํ•„์š”ํ•  ๋•Œ ์—ฌ๋Ÿฌ ์›น ํŽ˜์ด์ง€๋“ค์ด ๊ณต์œ ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งค๊ฐœ ์—ญํ• ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

    ์ฟ ํ‚ค์˜ ํŠน์ง•

    • ์ •๋ณด๊ฐ€ ํด๋ผ์ด์–ธํŠธ PC์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
    • ์ €์žฅ ์ •๋ณด ์šฉ๋Ÿ‰์— ์ œํ•œ์ด ์žˆ๋‹ค.
    • ๋ณด์•ˆ์ด ์ทจ์•ฝํ•˜๋‹ค
    • ํด๋ผ์ด์–ธํŠธ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‚ฌ์šฉ ์œ ๋ฌด๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๋„๋ฉ”์ธ๋‹น ์ฟ ํ‚ค๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค(์›น ์‚ฌ์ดํŠธ๋‹น ํ•˜๋‚˜์˜ ์ฟ ํ‚ค๊ฐ€ ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค.)

    ์ฟ ํ‚ค๋Š” ํด๋ผ์ด์–ธํŠธ PC์— ์ •๋ณด๋ฅผ ์ €์žฅํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๋ณด์•ˆ์— ์ทจ์•ฝํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ์ฟ ํ‚ค๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์€ ์ฃผ๋กœ ๋ณด์•ˆ๊ณผ ๋ฌด๊ด€ํ•œ ๊ฒฝ์šฐ์— ํ•œํ•ด ์‚ฌ์šฉํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์šฐ๋ฆฌ๊ฐ€ ์›น ํŽ˜์ด์ง€๋ฅผ ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ ์–ด๋–ค ํŒ์—…์ฐฝ์ด ๋‚˜ํƒ€๋‚˜๋ฉด '์˜ค๋Š˜์€ ๋” ์ด์ƒ ๋ณด์ง€ ์•Š๊ธฐ'๋ฅผ ์ฒดํฌํ•˜๋Š”๋ฐ, ์ด์ฒ˜๋Ÿผ ํŒ์—…์ฐฝ์ด ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๊ฒŒ ํ•˜๋Š” ๊ฒฝ์šฐ ๋“ฑ์— ์‚ฌ์šฉํ•œ๋‹ค.

    ์ฟ ํ‚ค์˜ ์ข…๋ฅ˜

    ์†์„ฑ Persistence์ฟ ํ‚ค Session์ฟ ๊ธฐ
    ์ƒ์„ฑ ์œ„์น˜ ํŒŒ์ผ๋กœ ์ƒ์„ฑ ๋ธŒ๋ผ์šฐ์ € ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์„ฑ
    ์ข…๋ฃŒ ์‹œ๊ธฐ ์ฟ ํ‚ค๋ฅผ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ์ฟ ํ‚ค ์„ค์ • ๊ฐ’์ด ์ข…๋ฃŒ๋œ ๊ฒฝ์šฐ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ข…๋ฃŒํ•œ ๊ฒฝ์šฐ
    ์ตœ์ดˆ ์ ‘์† ์‹œ ์ „์†ก ์—ฌ๋ถ€ ์ตœ์ดˆ ์ ‘์† ์‹œ ์„œ๋ฒ„๋กœ ์ „์†ก ์ตœ์ดˆ ์ ‘์† ์‹œ ์„œ๋ฒ„๋กœ ์†กx
    ์šฉ๋„ ๋กœ๊ทธ์ธ ์œ ๋ฌด ๋˜๋Š” ํŒ์—…์ฐฝ์„ ์ œํ•œํ•  ๋•Œ ์‚ฌ์ดํŠธ ์ ‘์† ์‹œ Session ์ธ์ฆ์ •๋ณด๋ฅผ ์œ ์ง€ํ•  ๋•Œ

9.3.1 ์ฟ ํ‚ค ๊ธฐ๋Šฅ ์‹คํ–‰ ๊ณผ์ •

๋ธŒ๋ผ์šฐ์ €์—์„œ ์›น ์‚ฌ์ดํŠธ์— ์ตœ์ดˆ ์ ‘์†ํ•˜๋ฉด ์›น ์„œ๋ฒ„์—์„œ ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•ด ํด๋ผ์ด์–ธํŠธ๋กœ ์ „์†กํ•œ๋‹ค.๊ทธ๋ฆฌ๊ณ  ๋ธŒ๋ผ์šฐ์ €๋Š” ์ฟ ํ‚ค๋ฅผ ํŒŒ์ผ๋กœ ์ €์žฅํ•œ๋‹ค.์ดํ›„ ๋‹ค์‹œ ์ ‘์†ํ•ด ์„œ๋ฒ„๊ฐ€ ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ์ฟ ํ‚ค ์ „์†ก์„ ์š”์ฒญํ•˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ์ฟ ํ‚ค ์ •๋ณด๋ฅผ ์„œ๋ฒ„์— ์ „์†กํ•˜๊ณ  ์„œ๋ฒ„๋Š” ์ฟ ํ‚ค ์ •๋ณด๋ฅผ ์ด์šฉํ•ด์„œ ์ž‘์—…์„ ํ•œ๋‹ค.

  • ์ตœ์ดˆ ์‚ฌ์ดํŠธ ์ ‘์† ์‹œ
    1. ๋ธŒ๋ผ์šฐ์ €๋กœ ์‚ฌ์ดํŠธ์— ์ ‘์†ํ•œ๋‹ค.
    2. ์„œ๋ฒ„๋Š” ์ •๋ณด๋ฅผ ์ €์žฅํ•œ ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
    3. ์ƒ์„ฑ๋œ ์ฟ ํ‚ค๋ฅผ ๋ธŒ๋ผ์šฐ์ €๋กœ ์ „์†กํ•œ๋‹ค.
    4. ๋ธŒ๋ผ์šฐ์ €๋Š” ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ์ฟ ํ‚ค ์ •๋ณด๋ฅผ ์ฟ ํ‚ค ํŒŒ์ผ์— ์ €์žฅํ•œ๋‹ค.
  • ์žฌ์ ‘์† ์‹œ 5. ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋‹ค์‹œ ์ ‘์†ํ•ด ์„œ๋ฒ„๊ฐ€ ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ์ฟ ํ‚ค ์ „์†ก์„ ์š”์ฒญํ•˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ์ฟ ํ‚ค ์ •๋ณด๋ฅผ ์„œ๋ฒ„์— ๋„˜๊ฒจ์ค€๋‹ค. 6. ์„œ๋ฒ„๋Š” ์ฟ ํ‚ค ์ •๋ณด๋ฅผ ์ด์šฉํ•ด ์ž‘์—…์„ ํ•œ๋‹ค.

9.3.2 ์ฟ ํ‚ค API

  • ์ฟ ํ‚ค๋Š” Cookieํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ •๋ณด๋ฅผ ์ •์žฅํ•œ ํ›„ ์„œ๋ฒ„์—์„œ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „์†กํ•ด ํŒŒ์ผ๋กœ ์ €์žฅ๋œ๋‹ค. ์ฟ ํ‚ค๊ด€๋ จ API์˜ ํŠน์ง•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
    • javax.servlet.http.Cookie๋ฅผ ์ด์šฉํ•œ๋‹ค.
    • HttpServletResponse์˜ addCookie() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ํด๋ผ์ด์–ธํŠธ ๋ธŒ๋ผ์šฐ์ €์— ์ฟ ํ‚ค๋ฅผ ์ „์†กํ•œ ํ›„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    • HttpServletRequest์˜ getCookie() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ฟ ํ‚ค๋ฅผ ์„œ๋ฒ„๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.
๋ฉ”์„œ๋“œ ์„ค๋ช…
getComment() ์ฟ ํ‚ค์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๊ฐ€์ ธ์˜จ๋‹ค.
getDomain() ์ฟ ํ‚ค์˜ ์œ ํšจํ•œ ๋„๋ฉ”์ธ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
getMaxAge() ์ฟ ํ‚ค ์œ ํšจ๊ธฐ๊ฐ„์„ ๊ฐ€์ ธ์˜จ๋‹ค.
getName() ์ฟ ํ‚ค ์ด๋ฆ„์„ ๊ฐ€์ ธ์˜จ๋‹ค.
getPath() ์ฟ ํ‚ค์˜ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
getValue() ์ฟ ํ‚ค์˜ ์„ค์ • ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
setComment(String) ์ฟ ํ‚ค์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์„ค์ •ํ•œ๋‹ค.
setDomain(String) ์ฟ ํ‚ค์˜ ์œ ํšจํ•œ ๋„๋ฉ”์ธ์„ ์„ค์ •ํ•œ๋‹ค.
setMaxAge(int) ์ฟ ํ‚ค ์œ ํšจ ๊ธฐ๊ฐ„์„ ์„ค์ •ํ•œ๋‹ค.
setValue(String) ์ฟ ํ‚ค ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค.

์ฟ ํ‚ค ์ƒ์„ฑ ์‹œ setMaxAge() ๋ฉ”์„œ๋“œ ์ธ์ž ๊ฐ’์˜ ์ข…๋ฅ˜๋ฅผ ์ง€์ •ํ•ด์„œ ํŒŒ์ผ์— ์ €์žฅํ•˜๋Š” Persistence ์ฟ ํ‚ค๋ฅผ ๋งŒ๋“ค๊ฑฐ๋‚˜ ๋ฉ”๋ชจ๋ฆฌ์—๋งŒ ์ €์žฅํ•˜๋Š” Session ์ฟ ํ‚ค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ setMaxAge() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•œ ์ฟ ํ‚ค ์ €์žฅ ๋ฐฉ์‹์€ ๋‹ค์Œ ๋‘ ๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.

์ธ์ž ๊ฐ’์œผ๋กœ ์Œ์ˆ˜๋‚˜ setMaxAge() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ฟ ํ‚ค๋ฅผ ๋งŒ๋“ค๋ฉด Session ์ฟ ํ‚ค๋กœ ์ €์žฅ๋œ๋‹ค.

์ธ์ž ๊ฐ’์œผ๋กœ ์–‘์ˆ˜๋ฅด ์ง€์ •ํ•˜๋ฉด Persistence ์ฟ ํ‚ค๋กœ ์ €์žฅ๋œ๋‹ค.

9.3.3 ์„œ๋ธ”๋ฆฟ์—์„œ ์ฟ ํ‚ค ์‚ฌ์šฉํ•˜๊ธฐ

1.Cookie ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ์ฟ ํ‚ค ์ด๋ฆ„์„ cookieTest๋กœ ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  setMaxAge() ๋ฉ”์„œ๋“œ์— ์ฟ ํ‚ค ์œ ํšจ ์‹œ๊ฐ„์„ 24์‹œ๊ฐ„์œผ๋กœ ์„ค์ •.๊ทธ๋Ÿฐ ๋‹ค์Œ response์˜ addCookie()๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ƒ์„ฑ๋œ ์ฟ ํ‚ค๋ฅผ ๋ธŒ๋ผ์šฐ์ €๋กœ ์ „์†กํ•œ๋‹ค.

package sec02.ex01;

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;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Date;
@WebServlet("/set")
public class SetCookieValue extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        Date date = new Date();



        Cookie c = new Cookie("cookieTest", URLEncoder.encode("JSPํ”„๋กœ๊ทธ๋ž˜๋ฐ์ž…๋‹ˆ๋‹ค.","utf-8"));
        //c.setMaxAge(24*60*60);
        c.setMaxAge(-1); // ์ฟ ํ‚ค๋ฅผ session์œผ๋กœ ์„ค๋ช…ํ•˜๋Š” ๋ฐฉ๋ฒ• ์Œ์ˆ˜๋ฅผ ๋„˜๊ฒจ์ฃผ
        resp.addCookie(c);
        out.println("ํ˜„์žฌ์‹œ๊ฐ„: "+date);
        out.println("ํ˜„์žฌ ์‹œ๊ฐ„์„ Cookie๋กœ ์ €์žฅํ•œ๋‹ค.");//์ด๊ฒŒ ๋ฌด์Šจ ๋ง์ด์ง€....?
    }
}

2.๋‘ ๋ฒˆ์งธ ์„œ๋ธ”๋ฆฟ ์š”์ฒญ ์‹œ์—๋Š” request์˜ getCookies() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ๋ธŒ๋ผ์šฐ์ €๋กœ๋ถ€ํ„ฐ ์ฟ ํ‚ค๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค ๊ทธ๋ฆฌ๊ณ  ์ „๋‹ฌ๋œ ์ฟ ํ‚ค์—์„œ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉํ•œ ์ด๋ฆ„์ธ cookieTest๋กœ ๊ฒ€์ƒ‰ํ•ด ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.

package sec02.ex01;

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;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

@WebServlet("/get")
public class GetCookieValue extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        Cookie[] allValues = req.getCookies();
        for(int i=0; i<allValues.length;i++){
            if(allValues[i].getName().equals("cookieTest")){
                out.println("<h2>Cookie ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ :"+ URLEncoder.encode(allValues[i].getValue(),"utf-8"));
            }
        }
    }
}

9.3.5 ์ฟ ํ‚ค ์ด์šฉํ•ด ํŒ์—…์ฐฝ ์ œํ•œํ•˜๊ธฐ

1.์›น ํŽ˜์ด์ง€๊ฐ€ ๋ธŒ๋ผ์šฐ์ €์— ๋กœ๋“œ๋  ๋•Œ pageLoad()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ํ›„ ์ฟ ํ‚ค์— ์ ‘๊ทผํ•ด ํŒ์—…์ฐฝ ๊ด€๋ จ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค. getCookieValue() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ฟ ํ‚ค ์ด๋ฆ„ notShowPop์˜ ๊ฐ’์ด true๊ฐ€ ์•„๋‹ˆ๋ฉด ํŒ์—…์ฐฝ์„ ๋‚˜ํƒ€๋‚ด๊ณ , notShowPop์˜ ๊ฐ’์ด true์ด๋ฉด ํŒ์—…์ฐฝ์„ ๋‚˜ํƒ€๋‚ด์ง€ ์•Š๋Š”๋‹ค.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์ฟ ๊ธฐ ์‚ฌ์šฉ</title>
    <script type="text/javascript">
        window.onload = pageLoad;<!-- ๋ธŒ๋ผ์šฐ์ €์— ์›น ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ๋  ๋•Œ pageLoad()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‹คํ–‰ํ•œ๋‹ค. -->
        function pageLoad() {
            notShowPop = getCookieValue();
            if(notShowPop!="true"){
                window.open("popUp.html","pop","width=200,height=300,history=no,resizable=no,status=no,scrollbars=yes,menubar=no");
            }
        }
        function getCookieValue() {
            var result = "false";
            if(document.cookie != "") {
                cookie = document.cookie.split(";");<!-- document์˜ cookie ์†์„ฑ์œผ๋กœ ์ฟ ํ‚ค ์ •๋ณด๋ฅผ ๋ฌธ์ž์—ด๋กœ ๊ฐ€์ ธ์˜จ ํ›„ ์„ธ๋ฏธ์ฝœ๋ก (;)์œผ๋กœ ๋ถ„๋ฆฌํ•ด ๊ฐ๊ฐ์˜ ์ฟ ํ‚ค๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค. -->
                for (var i = 0; i < cookie.length; i++) {
                    element = cookie[i].split("=");
                    value = element[0];
                    value = value.replace(/^\s*/, '');<!-- ์ •๊ทœ์‹์„ ์ด์šฉํ•ด ์ฟ ํ‚ค ์ด๋ฆ„ ๋ฌธ์ž์—ด์˜ ๊ณต๋ฐฑ(โ‚ฉs)์„ ์ œ๊ฑฐํ•œ๋‹ค. -->
                    if (value == "notShowPop") {
                        result = element[1]; <!-- ์ฟ ํ‚ค ์ด๋ฆ„์ด notShowPop์ด๋ฉด ํ•ด๋‹นํ•˜๋Š” ์ฟ ํ‚ค ๊ฐ’์„ ๊ฐ€์ ธ์™€ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. -->
                    }
                }
            }
            return result;
        }
        function deleteCookie() {
            document.cookie = "notShowPop=" + "false" + ";path=/; expires=-1";
        }
    </script>
</head>
<body>
    <form>
        <input type="button" value="์ฟ ํ‚ค ์‚ญ์ œ" onclick="deleteCookie()">
    </form>
</body>
</html>
  • ์ •๊ทœ์‹ ๊ณต๋ถ€ํ•˜๊ธฐ
  • cookieํ˜•ํƒœ ๋‹ค์‹œ ๋ณด๊ณ  javascript์˜ document.cookie ์•Œ์•„๋ณด๊ณ  ๋ฌธ์„œํ™”ํ•˜๊ธฐ

2.popUp.html์—์„œ๋Š” ์˜ค๋Š˜ ๋” ์ด์ƒ ํŒ์—…์ฐฝ ๋„์šฐ์ง€ ์•Š๊ธฐ์— ์ฒดํฌํ•˜๋ฉด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜์ธ setPopUpStart()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด notShowPop์˜ ๊ฐ’์„ true๋กœ ์„ค์ •ํ•˜์—ฌ ์žฌ์ ‘์† ์‹œ ํŒ์—…์ฐฝ์„ ๋‚˜ํƒ€๋‚ด์ง€ ์•Š๋„๋ก ์„ค์ •ํ•œ๋‹ค.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>์—†์Œ</title>
    <script type="text/javascript">
        function setPopUpStart(obj) {
            if(obj.checked == true){
                var expireDate = new Date();
                expireDate.setMonth(expireDate.getMonth()+1);<!-- ์œ ํšจ๊ธฐ๊ฐ„์„ ํ•œ ๋‹ฌ๋กœ ์ •ํ•œ๋‹ค. -->
                document.cookie = "notShowPop=" +"true"+";path=/;expires="+expireDate.toString();<!-- ์˜ค๋Š˜ ๋” ์ด์ƒ ํŒ์—…์ฐฝ ๋„์šฐ์ง€ ์•Š๊ธฐ์— ์ฒดํฌํ•˜๋ฉด notShowPop ์ฟ ํ‚ค ๊ฐ’์„ true๋กœ ์„ค์ •ํ•˜์—ฌ ์žฌ์ ‘์†์‹œ ํŒ์—…์ฐฝ์„ ๋‚˜ํƒ€๋‚ด์ง€ ์•Š๋Š”๋‹ค.-->
                window.close();
            }
        }
    </script>
</head>
<body>
    ์•Œ๋ฆผ ํŒ์—…์ฐฝ์ž…๋‹ˆ๋‹ค.
    <br><br><br><br><br><br><br>
    <form>
        <input type="checkbox" onclick="setPopUpStart(this)">์˜ค๋Š˜ ๋” ์ด์ƒ ํŒ์—…์ฐฝ ๋„์šฐ์ง€ ์•Š๊ธฐ
    </form>
</body>
</html>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published