๐Ÿ’ป Programming (358)

 

๋‘ฅ๊ทผ ํ† ๊ธ€ ๋ฒ„ํŠผ

๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” html, css, javascript๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‘ฅ๊ทผ toggle๋ฒ„ํŠผ์„ ๋งŒ๋“ค๊ณ , ๋ฒ„ํŠผ์˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ๊นŒ์ง€ ๋งŒ๋“ค์–ด ๋ด…๋‹ˆ๋‹ค. ๋ณธ ๋ณด์ŠคํŒ…์— ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ์ฝ”๋“œ๋Š” w3schools์—์„œ ๊ฐ€์ง€๊ณ  ์˜จ ๊ฒƒ์ž…๋‹ˆ๋‹ค. w3schools์—์„œ๋Š” ๋‹จ์ˆœํžˆ css๋ฅผ ์ด์šฉํ•ด์„œ ํ† ๊ธ€๋ฒ„ํŠผ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” ๊ฒƒ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ ๊นŒ์ง€๋งŒ ๋ณด์—ฌ์ฃผ์—ˆ๋Š”๋ฐ, ์ €๋Š” ๊ทธ๋ ‡๊ฒŒ ํ† ๊ธ€์ด ๋  ๋•Œ๋งˆ๋‹ค ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ์–ด๋–ค ๊ธฐ๋Šฅ์ด ์‹คํ–‰๋˜๋Š” ๋ถ€๋ถ„๊นŒ์ง€ ํ™•์žฅํ•ด์„œ ํฌ์ŠคํŒ…ํ•ฉ๋‹ˆ๋‹ค.

 

์šฐ์„  toggle.html ํŒŒ์ผ์— input tag๋ฅผ ์ด์šฉํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ค๋‹ˆ๋‹ค.

<!DOCTYPE html>
<html>

<head>
</head>

<body>
    <label class="switch">
        <input type="checkbox" />
        <span class="slider round"></span>
    </label>
</body>

</html>

 

๊ทธ๋ฆฌ๊ณ  toggle.css ํŒŒ์ผ์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ ๊ทธ ์•ˆ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑ์„ ํ•ด์ค๋‹ˆ๋‹ค.

/* ์Šฌ๋ผ์ด๋” ์™ธ๋ถ€๋ฅผ ๊ฐ์‹ธ๋Š” ๋ผ๋ฒจ์— ๋Œ€ํ•œ ์Šคํƒ€์ผ */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

/* HTML ๊ธฐ๋ณธ ์ฒดํฌ๋ฐ•์Šค ์ˆจ๊ธฐ๊ธฐ */
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

/* ์Šฌ๋ผ์ด๋” - ์‹ค์ œ๋กœ ํ† ๊ธ€๋  ๋ถ€๋ถ„ */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* ์Šฌ๋ผ์ด๋”๋ฅผ ๋™๊ทธ๋ž—๊ฒŒ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ ๋ถ€๋ถ„ */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}

 

์ด์ œ HTML ํŒŒ์ผ์˜ <head></head> ๋ถ€๋ถ„์— ์œ„ css ํŒŒ์ผ์„ ๋งํฌ์‹œ์ผœ์ค๋‹ˆ๋‹ค.

<head>
	<link rel="stylesheet" type="text/css" href="toggle.css" />
</head>

 

์ด์ œ toggle.html ํŒŒ์ผ์„ ๋”๋ธ”ํด๋ฆญํ•ด์„œ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์—ด์–ด๋ณด๋ฉด ์•„๋ž˜ ์˜์ƒ์ฒ˜๋Ÿผ ์›€์ง์ด๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋‘ฅ๊ทผ ํ† ๊ธ€ ๋ฒ„ํŠผ ์›€์ง์ด๋Š” ์˜์ƒ

 

์ž, ์ด์ œ ์—ฌ๊ธฐ์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด๋ณด๋„๋ก ํ• ๊ฑด๋ฐ์š”,

์ด ํ† ๊ธ€๋ฒ„ํŠผ์€ input ํƒœ๊ทธ์˜ checkbox๋ฅผ ์ด์šฉํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ํ† ๊ธ€์ด ๋  ๋•Œ๋งˆ๋‹ค checked ์†์„ฑ์ด ๋ณ€๊ฒฝ๋˜๋„๋ก ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๊ธฐ๋ณธ์ ์ธ ๋‚ด์šฉ์„ ๊ธฐ์–ตํ•˜๊ณ  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํ† ๊ธ€๋ฒ„ํŠผ์˜ checked ์†์„ฑ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํŠน์ • ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์€ ๋”ฐ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๊ทธ๋ƒฅ html ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•˜๋„๋ก ํ• ๊ฒŒ์š”.

์šฐ์„ , input ํƒœ๊ทธ์— onclick ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ toggle์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ํ•˜๊ณ , toggle ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜์ฒ˜๋Ÿผ input  ํƒœ๊ทธ์˜ onclick ์†์„ฑ์„ ๋„ฃ์–ด์ฃผ์„ธ์š”. 

<label class="switch">
    <input type="checkbox" onclick="toggle(this)">
    <span class="slider round"></span>
</label>

์ด๋ ‡๊ฒŒ ํ•ด์ฃผ๋ฉด ํ† ๊ธ€๋ฒ„ํŠผ(์ฒดํฌ๋ฐ•์Šค)๊ฐ€ ํด๋ฆญ๋  ๋•Œ๋งˆ๋‹ค toggle์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ž๊ธฐ์ž์‹ ์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ฒจ์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์ž, ์ด์ œ body ์•„๋ž˜์ชฝ์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

<script>
    function toggle(element) {
        console.log(element.checked);
    }
</script>

์œ„ ์ฝ”๋“œ๋Š” <head></head> ์— ์œ„์น˜ํ•ด๋„ ๋˜๊ณ  <body></body> ์‚ฌ์ด์— ์œ„์น˜ํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.

๋‹น์—ฐํžˆ ๋ณ„๋„ ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์ž‘์„ฑํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.

๋ถ„๋ฆฌํ•˜์—ฌ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๊ณผ ๊ด€๋ จํ•ด์„œ๋Š” ์ œ๊ฐ€ ์ž‘์„ฑํ•œ ๋‹ค๋ฅธ ํฌ์ŠคํŠธ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. -> https://keichee.tistory.com/356

์ €๋Š” <body></body> ์‚ฌ์ด์— ๋„ฃ์–ด๋†“์•˜์Šต๋‹ˆ๋‹ค.

<body>
    <label class="switch">
        <input type="checkbox" onclick="toggle(this)">
        <span class="slider round"></span>
    </label>

    <script>
        function toggle(element) {
            console.log(element.checked);
        }
    </script>
</body>

 

์ž, ์—ฌ๊ธฐ๊นŒ์ง€ html ํŒŒ์ผ ์ „์ฒด ์ฝ”๋“œ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋˜๋Š”์ง€ ๋‹ค์‹œ ํ•œ๋ฒˆ ๋ณด์—ฌ๋“œ๋ฆด๊ฒŒ์š”.

<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" type="text/css" href="toggle.css" />
</head>

<body>
    <label class="switch">
        <input type="checkbox" onclick="toggle(this)">
        <span class="slider round"></span>
    </label>

    <script>
        function toggle(element) {
            console.log(element.checked);
        }
    </script>
</body>

</html>

 

์—ฌ๊ธฐ๊นŒ์ง€ ์™„์„ฑํ•˜์—ฌ ์‹คํ–‰ํ•˜๋ฉด ์ฒดํฌ๋ฐ•์Šค(ํ† ๊ธ€๋ฒ„ํŠผ)์„ ํด๋ฆญํ•  ๋•Œ๋งˆ๋‹ค toggleํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉด์„œ ํ˜„์žฌ ์ฒดํฌ๋ฐ•์Šค์˜ ์ƒํƒœ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ฐ”๋€Œ์—ˆ๋Š”์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

ํ™•์ธ์„ ์œ„ํ•ด์„œ ๋ธŒ๋ผ์šฐ์ €์—์„œ toggle.html ํŒŒ์ผ์„ ์ƒˆ๋กœ๊ณ ์นจํ•˜์—ฌ ๋‹ค์‹œ ์—ด์–ด์ฃผ์‹œ๊ณ ,

ํ™”๋ฉด์—์„œ ์šฐํด๋ฆญ > ๊ฒ€์‚ฌ (inspect)๋ฅผ ์„ ํƒํ•˜์—ฌ ๊ฐœ๋ฐœ์ž๋„๊ตฌ(developer tools)๋ฅผ ์—ด์–ด์„œ consoleํƒญ์„ ์—ด์–ด์ฃผ์„ธ์š”.

๊ทธ๋ฆฌ๊ณ  ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ๋•Œ๋งˆ๋‹ค ์–ด๋–ค ๊ฐ’์ด ์ถœ๋ ฅ์ด ๋˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

ํ† ๊ธ€ ๋ฒ„ํŠผ ๋™์ž‘ ์˜์ƒ

 

์ž, ์ด๋ ‡๊ฒŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ํ† ๊ธ€๋ฒ„ํŠผ์˜ ์ƒํƒœ๊ฐ’์„ ์ถœ๋ ฅํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์–ด๋–ค๊ฐ€์š”? ๋ณ„๋กœ ์–ด๋ ต์ง€ ์•Š์ฃ ? css ์Šคํƒ€์ผ์„ ํ•ด์„ํ•˜๊ธฐ ํž˜๋“œ์‹ค ์ˆœ ์žˆ๊ฒ ์œผ๋‚˜, ์—ฌ๊ธฐ์„œ ๊ทธ๋Ÿฐ ๋ถ€๋ถ„์„ ์ž์„ธํ•˜๊ฒŒ ๋‹ค๋ฃจ์ง„ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค.

์ข€ ๋” ๋‚˜์•„๊ฐ€์„œ ํ† ๊ธ€๋ฒ„ํŠผ์˜ ์ƒํƒœ๊ฐ’์— ๋”ฐ๋ผ ajax ์š”์ฒญ์„ ์„œ๋ฒ„์— ๋‚ ๋ ค ์‹ค์ œ DB๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ฑฐ๋‚˜, 

๋˜๋Š” html์˜ ํŠน์ • element๋ฅผ hideํ•˜๊ฑฐ๋‚˜ showํ•˜๋Š” ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”.

 

๊ทธ๋Ÿผ ์˜ค๋Š˜๋„ ์ฆ๊ฑฐ์šด ์ฝ”๋”ฉํ•˜์‹œ๊ธธ ๋ฐ”๋ž˜์š” ^-^

 

- ๊น๊นํ•œ ๊ฐœ๋ฐœ์ž -

์›น์‚ฌ์ดํŠธ๋ฅผ ๋งŒ๋“ค๋‹ค๋ณด๋ฉด HTMLํŒŒ์ผ ํฌ๊ธฐ๊ฐ€ ์ปค์ง€๊ธฐ ๋งˆ๋ จ์ž…๋‹ˆ๋‹ค.

์›น์‚ฌ์ดํŠธ๊ฐ€ dynamicํ•œ ๋™์  ์‚ฌ์ดํŠธ๋ผ๋ฉด ๋”๋”์šฑ ๊ทธ๋Ÿด ๊ฐ€๋Šฅ์„ฑ์ด ์ปค์ง‘๋‹ˆ๋‹ค.

์ด๋Ÿฐ ์ €๋Ÿฐ ๊ธฐ๋Šฅ์„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๊ตฌํ˜„์„ ํ•˜๋‹ค๋ณด๋ฉด ์ฃผ์ฒดํ•  ์ˆ˜ ์—†์ด ์ปค์ง€๋Š” html ํŒŒ์ผ์„ ๋ณผ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ์š”

์ด๋Ÿด ๋•Œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ถ€๋ถ„์„ ๋ณ„๋„์˜ .js ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•œ ๋’ค html ํŒŒ์ผ์— ๋งํฌ๋ฅผ ๊ฑธ์–ด์„œ

๋งˆ์น˜ html ํŒŒ์ผ์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋ฐฉ๋ฒ•๋„ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ html ์˜ˆ์ œ๋ฅผ ๋จผ์ € ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" type="text/css" href="toggle.css" />
</head>

<body>
    <label class="switch">
        <input type="checkbox" onclick="toggle(this)">
        <span class="slider round"></span>
    </label>

    <script>
        function toggle(element) {
            console.log(element.checked);
        }
    </script>
</body>

</html>

์œ„ ์ฝ”๋“œ๋Š” ํ† ๊ธ€๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ ์‹œ๊ฐ„์— ์‚ฌ์šฉํ–ˆ๋˜ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

์œ„ ์ฝ”๋“œ์—์„œ <script> ํƒœ๊ทธ๋กœ ๊ฐ์‹ผ ๋ถ€๋ถ„์„ .js ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•ด๋‚ด๊ณ  ํ•ด๋‹น ํŒŒ์ผ์„ htmlํŒŒ์ผ์— ๋งํฌ(import, include๋ผ๊ณ  ์–˜๊ธฐํ•˜๊ธฐ๋„ ํ•จ)๋ฅผ ๊ฑธ์–ด์„œ ๊ธฐ๋Šฅ์ด ์ •์ƒ์ ์œผ๋กœ ๋Œ์•„๊ฐ€๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์šฐ์„  html ๋ฌธ์„œ์˜ <script> ํƒœ๊ทธ ๋‚ด์— ์žˆ๋˜ ๋‚ด์šฉ์„ ๋ณต์‚ฌํ•˜์—ฌ toggle.js ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

function toggle(element) {
    console.log(element.checked);
}

์ด์ œ <head>ํƒœ๊ทธ ๋‚ด์— ์•„๋ž˜์™€ ๊ฐ™์ด toggle.js ํŒŒ์ผ์„ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๊ณ , <body>์•ˆ์— ์žˆ๋˜ script๋Š” ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" type="text/css" href="toggle.css" />
    <script src="toggle.js"></script>
</head>

<body>
    <label class="switch">
        <input type="checkbox" onclick="toggle(this)">
        <span class="slider round"></span>
    </label>
</body>

</html>

<script> ํƒœ๊ทธ๊ฐ€ ์‚ฌ๋ผ์ง€๋‹ˆ ํ™”๋ฉด์— ๊ทธ๋ฆผ์„ ๊ทธ๋ ค์ฃผ๋Š” ์š”์†Œ๋“ค๋งŒ <body>์— ๋‚จ์•„์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์œ„ ์ฝ”๋“œ ์ž์ฒด๊ฐ€ ์›Œ๋‚™ ์ž‘์€ ์ฝ”๋“œ๋ผ ๊น”๋”ํ•ด์กŒ๋‹ค๋Š” ๋Š๋‚Œ์„ ๋ฐ›์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ์œผ๋‚˜, 

์œ„ ์ฒ˜๋Ÿผ ์งง์€ html ๋ฌธ์„œ๋Š” ํ…Œ์ŠคํŠธ์šฉ๋ฐ–์— ์—†์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๊ณ ์‹ถ๋‹ค๋ฉด ํ•ญ์ƒ .js ํŒŒ์ผ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋งํฌ๊ฑธ์–ด ์‚ฌ์šฉํ•˜๊ธธ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค.

 

๋ญ”๊ฐ€ ๊ธธ๊ฒŒ ์„ค๋ช…๋“œ๋ ธ์ง€๋งŒ, htmlํŒŒ์ผ์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์—ฐ๊ฒฐ/๋งํฌ/import/include ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์€ <head>ํƒœ๊ทธ ๋‚ด์— ์•„๋ž˜์™€ ๊ฐ™์ด ํ•œ ์ค„๋งŒ ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

<script src="toggle.js"></script>

์—ฌ๊ธฐ์„œ .js ํŒŒ์ผ์˜ ์œ„์น˜๋Š” html ํŒŒ์ผ์˜ ์œ„์น˜์—์„œ ์ƒ๋Œ€๊ฒฝ๋กœ๋กœ ์ง€์ •ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋˜๋Š” ์›น์ƒ์— ์žˆ๋Š” ํŒŒ์ผ์ผ ๊ฒฝ์šฐ URL ์ฃผ์†Œ๊ฐ€ ๋“ค์–ด์˜ฌ ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

 

์ตœ๊ทผ์— React.js ๊ฐœ๋ฐœ์„ ๊ณต๋ถ€ํ•˜๊ธฐ์œ„ํ•ด node.js๋ถ€ํ„ฐ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ๋กœ ํ–ˆ์–ด์š”.

์šฐ์„  ์ปค๋งจ๋“œ์ฐฝ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•ด๋ณด๋‹ˆ

$ npm --help

๋ช…๋ น์–ด ์‚ฌ์šฉ๋ฒ•์ด ํ‹€๋ ธ๋‹ค๊ณ  ํ•˜๋ฉด์„œ ์‚ฌ์šฉ๋ฒ•์„ ์ถœ๋ ฅํ•ด์ฃผ๋”๋‹ˆ

๋งˆ์ง€๋ง‰์—๋Š” ์นœ์ ˆํ•˜๊ฒŒ๋„ npm ํ˜„์žฌ ๋ฒ„์ „์ด ๋‚ฎ์œผ๋‹ˆ ์ตœ์‹ ๋ฒ„์ „์œผ๋กœ ์—…๋ฐ์ดํŠธํ•˜๋ผ๋Š” ์•ˆ๋‚ด ๋ฌธ๊ตฌ๊ฐ€ ๋‚˜์˜ค๋„ค์š”.

์•ˆ์“ด์ง€ ์˜ค๋ž˜๋˜์„œ ๋ฒ„์ „์ด 5.6.0 ์ด๋„ค์š” ใ… ใ… 

 

์‹คํ–‰๋ช…๋ น์–ด๋ฅผ ๊ฐ€๋ฅด์ณ์คฌ์œผ๋‹ˆ ์‹คํ–‰ํ•ด๋ณผ๊ฒŒ์š”~

$ npm i -g npm

๊ทผ๋ฐ ์ž˜ ์‹คํ–‰ํ•˜๋‹ค๊ฐ€ ์•„๋ž˜์ฒ˜๋Ÿผ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋„ค์š”. ์•„ํ‘ ใ…œใ…œ

npm ์—…๋ฐ์ดํŠธ ์˜ค๋ฅ˜

์—๋Ÿฌ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‹ˆ ๋””๋ ‰ํ† ๋ฆฌ์— ๊ถŒํ•œ์ด ์—†์—ˆ๋„ค์š”. 

์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ๋‹ค์‹œ ์‹œ๋„ํ•ด๋ณผ๊ฒŒ์š”.

$ sudo npm i -g npm

npm ์—…๋ฐ์ดํŠธ ์™„๋ฃŒ

์Š‰~์Š‰~ ๋ญ๊ฐ€ ๋‚˜์˜ค๋”๋‹ˆ 11์ดˆ๋งŒ์— ์—…๋ฐ์ดํŠธ๊ฐ€ ๋๋‚ฌ์–ด์š”.

 

#npm๋ฒ„์ „ํ™•์ธ๋ช…๋ น์–ด ๋กœ ์—…๋ฐ์ดํŠธ๊ฐ€ ์ž˜ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณผ๊ฒŒ์š”.

$ npm -v
6.9.2

 6.9.2๋กœ ์—…๋ฐ์ดํŠธ๊ฐ€ ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋„ค์š”.

 

์—…๋ฐ์ดํŠธ ์„ฑ๊ณต~

JSP์™€ Javascript์˜ ์ฐจ์ด๊ฐ€ ๋ญ˜๊นŒ์š”? ๋ผ๋Š” ์งˆ๋ฌธ์„ ๋ฐ›์€ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ดˆ๋ณด์ž ๋ถ„๋“ค์ด ์ถฉ๋ถ„ํžˆ ํ• ๋งŒํ•œ ์งˆ๋ฌธ์ด์ฃ . JSP๋„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋„ ์ œ๋Œ€๋กœ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ ์ „์—๋Š” ๋‘ ๊ฐ€์ง€๊ฐ€ ๋ญ๊ฐ€ ๋‹ค๋ฅธ์ง€ ์ž˜ ๋ชจ๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ดˆ๋ณด์ž ๋ถ„๋“ค์ด ์ดํ•ดํ•  ์ •๋„๋กœ ๊ฐ„๋žตํ•˜๊ณ  ์‰ฝ๊ฒŒ ์š”์ ๋งŒ ์„ค๋ช…๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

Javascript๋Š” ์›น๋ธŒ๋ผ์šฐ์ €(์‚ฌ์šฉ์ž์˜ ์ปดํ“จํ„ฐ)์—์„œ ์‹คํ–‰โ€‹์ด๋˜๋Š” ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด์ž…๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์€ .js ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง€๋ฉฐ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์— ๋”ฐ๋ผ ์ž‘์„ฑ์„ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด ์ค‘ ํ•˜๋‚˜๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

โ€‹

๋ฐ˜๋ฉด, JSP๋Š” Java Server Page์˜ ์•ฝ์ž๋กœ .jsp ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง€๋ฉฐ, ์–ธ์–ด์˜ ์ข…๋ฅ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค.

.jsp ํŒŒ์ผ์—๋Š” htmlํƒœ๊ทธ์™€ javascript ํ•จ์ˆ˜๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ถ”๊ฐ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด <% java code %> ํ˜•ํƒœ๋กœ ์ž๋ฐ”๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

<html>

<head><title>Hello World</title></head>

<body> Hello World!<br/>

<% out.println("Your IP address is " + request.getRemoteAddr()); %>  <!-- ์—ฌ๊ธฐ๊ฐ€ JSP ๋ฌธ๋ฒ• -->

</body>

</html>

 

JSP์— ์ž‘์„ฑ๋œ ๋‚ด์šฉ์€ ์„œ๋ฒ„์—์„œ ์‹คํ–‰๋˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ์›น๋ธŒ๋ผ์šฐ์ €(์‚ฌ์šฉ์ž ์ปดํ“จํ„ฐ)๋กœ ์ „์†ก๋ฉ๋‹ˆ๋‹ค.

 

์ด์ œ JSP์™€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ฐจ์ด์ ์ด ๋ญ”์ง€ ์•„์‹œ๊ฒ ์ฃ ?

 

๊ฐ€์‹œ๊ธฐ ์ „์— ๊ณต๊ฐ ๊พธ~~~~์šฑ ๋ˆŒ๋Ÿฌ์ฃผ๊ณ  ๊ฐ€์„ธ์š” ^-^

์ž๋ฐ”์—์„œ 8์ง„์ˆ˜ ์ˆซ์ž๋ฅผ ์‚ฌ์šฉ์ž(ํ‚ค๋ณด๋“œ)๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์•„์„œ 10์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.


import java.util.Scanner;

public class OctalToDex
{
  public static void main(String[] args)
  {
    Scanner sc = new Scanner(System.in);
    String b=sc.nextLine();
    int a=Integer.valueOf(b, 8);
    sc.close();
    System.out.println(a);
   }
} 


Java์—์„œ๋Š” Integerํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•˜์—ฌ 2์ง„์ˆ˜, 8์ง„์ˆ˜, 16์ง„์ˆ˜์˜ ์ˆซ์ž๋ฅผ 10์ง„์ˆ˜๋กœ ์‰ฝ๊ฒŒ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๊ทธ ๋ฐ˜๋Œ€๋กœ 10์ง„์ˆ˜๋ฅผ 2์ง„์ˆ˜,8์ง„์ˆ˜,16์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ์‰ฝ๊ฒŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฉ”์„œ๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
 10์ง„์ˆ˜ -> 2์ง„์ˆ˜ ๋ณ€ํ™˜ : Integer.toBinaryString(int);
 10์ง„์ˆ˜ -> 8์ง„์ˆ˜ ๋ณ€ํ™˜ : Integer.toOctalString(int);
10์ง„์ˆ˜ -> 16์ง„์ˆ˜ ๋ณ€ํ™˜ : Integer.toHexString(int);
 10์ง„์ˆ˜ -> 2์ง„์ˆ˜ ๋ณ€ํ™˜ : Integer.toBinaryString(int); 
 10์ง„์ˆ˜ -> 8์ง„์ˆ˜ ๋ณ€ํ™˜ : Integer.toOctalString(int);
 10์ง„์ˆ˜ -> 16์ง„์ˆ˜ ๋ณ€ํ™˜ : Integer.toHexString(int);โ€‹


์œ„ ๋ฉ”์„œ๋“œ๋Š” 10์ง„์ˆ˜ ์ˆซ์ž(int)๋ฅผ 2์ง„์ˆ˜/8์ง„์ˆ˜/16์ง„์ˆ˜ ์ŠคํŠธ๋ง์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 2์ง„์ˆ˜ -> 10์ง„์ˆ˜ ๋ณ€ํ™˜ : Integer.valueOf(string, 2);
 8์ง„์ˆ˜ -> 10์ง„์ˆ˜ ๋ณ€ํ™˜ : Integer.valueOf(string, 8);
16์ง„์ˆ˜ -> 10์ง„์ˆ˜ ๋ณ€ํ™˜ : Integer.valueOf(string, 10);
2์ง„์ˆ˜ -> 10์ง„์ˆ˜ ๋ณ€ํ™˜ : Integer.valueOf(string, 2);
8์ง„์ˆ˜ -> 10์ง„์ˆ˜ ๋ณ€ํ™˜ : Integer.valueOf(string, 8);
16์ง„์ˆ˜ -> 10์ง„์ˆ˜ ๋ณ€ํ™˜ : Integer.valueOf(string, 10);โ€‹


์œ„ ๋ฉ”์„œ๋“œ๋Š” 2์ง„์ˆ˜/8์ง„์ˆ˜/16์ง„์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž์ŠคํŠธ๋ง์„ 10์ง„์ˆ˜ int๋กœ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

์ž๋ฐ” ์›น์•ฑ์„ ๊ฐœ๋ฐœํ•  ๋•Œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํˆด์ด ์ดํด๋ฆฝ์Šค์™€ ์Šคํ”„๋ง๋ถ€ํŠธ์ž…๋‹ˆ๋‹ค.

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์ดํด๋ฆฝ์Šค์—์„œ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์Šคํ”„๋ง์„ ์„ค์น˜ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์šฐ์„ , ์ดํด๋ฆฝ์Šค๊ฐ€ ์„ค์น˜๋˜์–ด์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.

์ตœ์‹  ์ดํด๋ฆฝ์Šค๋Š” ์šฐ์ธก ๋งํฌ์—์„œ ๋ฐ›์œผ์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. -> https://www.eclipse.org/downloads/

 

์ดํด๋ฆฝ์Šค ์ƒ๋‹จ ๋ฉ”๋‰ด์—์„œ Help > Eclipse Marketplace... ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ฒ€์ƒ‰์ฐฝ์—์„œ "sts"๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

์œ„ ํ™”๋ฉด์ฒ˜๋Ÿผ ๋‚˜์˜ค๋ฉด "Spring Tools 4 - for Spring Boot (aka Spring Tool Suite 4)"๋ฅผ install ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

Spring Tools 4 ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์„ค์น˜ํ•  ๋•Œ ์–ด๋–ค ๊ฒƒ๋“ค์„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋ชฉ๋ก์„ ์กฐํšŒํ•ด ์˜ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ƒ์„ธ ๋ชฉ๋ก์„ ์œ„ ์‚ฌ์ง„์ฒ˜๋Ÿผ ๋ณด์—ฌ์ฃผ๋Š”๋ฐ, ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋‘ ์ฒดํฌ๊ฐ€ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ƒฅ "confirm" ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.

์†Œํ”„ํŠธ์›จ์–ด ๋ผ์ด์„ผ์Šค์— ๋™์˜ํ•˜๋Š”์ง€ ๋ฌป๋Š” ์งˆ๋ฌธ์—๋Š” "I accept the terms...."๋ฅผ ์„ ํƒํ•˜๊ณ  "Finish"๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

STS 4 ์„ค์น˜ ์ง„ํ–‰ ์ƒํƒœ

์šฐ์ธก ํ•˜๋‹จ์˜ "Progress" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด Progress ์ฐฝ์œผ๋กœ focus๊ฐ€ ๋งž์ถฐ์ง€๋ฉด์„œ ์„ค์น˜ ์ƒํƒœ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

์ธํ„ฐ๋„ท ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ฆด ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์„ธ๋กœ๋กœ ๋‚˜์—ด๋œ 5๊ฐœ์˜ ๋„ค์Šคํ”„๋ ˆ์†Œ ๋ฐฐ์—ด

์ด๋ฒˆ์—๋Š” ์ž๋ฐ”๋กœ 1์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
์ž๋ฐ”๋กœ ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
์šฐ์„  ์šฉ์–ด๋ถ€ํ„ฐ ์ •๋ฆฌํ•˜๊ณ  ๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค.
์ œ๊ฐ€ ๋งŒ๋“ ๋‹ค๋ผ๊ณ  ์–˜๊ธฐํ•œ ๊ฒƒ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์–˜๊ธฐํ•˜๋ ค๊ณ  ํ•œ ๊ฒƒ์ธ๋ฐ ์ด๋Š” ๋‘ ๊ฐ€์ง€ ์˜๋ฏธ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
๋ฐ”๋กœ "์„ ์–ธ"๊ณผ "์ดˆ๊ธฐํ™”"์ž…๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ "์„ ์–ธ"์ด๋ผ๋Š” ๊ฒƒ์€ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์„ ์ง€์–ด์ฃผ๋Š” ๊ฒƒ์„ ์–˜๊ธฐํ•˜๋ฉฐ,
"์ดˆ๊ธฐํ™”"๋ผ๋Š” ๊ฒƒ์€ ๊ทธ ๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
์ด ์šฉ์–ด๋“ค์„ ๋จธ๋ฆฟ์†์— ๋„ฃ๊ณ  ์ถœ๋ฐœํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฐฐ์—ด์„ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•

์ž๋ฐ”์—์„œ ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์ด 3๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

public static void main(String[] args) {

        // ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•
        int[] intArr1;
        int []intArr2;
        int intArr3[];
}

์œ„์—์„œ๋Š” int ํƒ€์ž…์˜ ๋ฐฐ์—ด์„ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์„ ์–ธํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.
intํƒ€์ž… ๋Œ€์‹  long, double, Integer, Long, Double, String ๋“ฑ ๋‹ค๋ฅธ ํƒ€์ž…์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
์ด๋ ‡๊ฒŒ ์„ ์–ธ์„ ํ•ด ๋†“์•„๋„ ๊ณต๊ฐ„(๋ฉ”๋ชจ๋ฆฌ)์„ ์ฐจ์ง€ํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค.
๋ฐฐ์—ด์€ ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜๋ฉด์„œ ์‚ฌ์ด์ฆˆ๋ฅผ ์ •ํ•ด์ค˜์•ผ ๊ณต๊ฐ„(๋ฉ”๋ชจ๋ฆฌ)์„ ์ฐจ์ง€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
๊ทธ๋Ÿผ ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”๋Š” ์–ด๋–ป๊ฒŒ ํ• ๊นŒ์š”?

๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•

๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ฉ๋‹ˆ๋‹ค.

public static void main(String[] args) {

        // ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•
        int[] intArr1;

        // ๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•
        intArr1 = new int[3];
}

์œ„ ์˜ˆ์ œ์—์„œ๋Š” intArr1์ด๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์ด์ฆˆ๊ฐ€ 3์ธ ๋ฐฐ์—ด๋กœ ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ด๋ ‡๊ฒŒ ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜๋ฉด int ํƒ€์ž…์˜ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’ 0์œผ๋กœ ์„ธํŒ…์ด ๋ฉ๋‹ˆ๋‹ค.

๋ฐฐ์—ด์˜ ์„ ์–ธ๊ณผ ์ดˆ๊ธฐํ™”๋ฅผ ๋™์‹œ์— ํ•˜๋Š” ๋ฐฉ๋ฒ•

์ž๋ฐ”์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐฐ์—ด์˜ ์„ ์–ธ๊ณผ ์ดˆ๊ธฐํ™”๋ฅผ ๋™์‹œ์— ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

public static void main(String[] args) {

        int[] intArr1 = new int[10];    // size 10, index range from 0 to 9
        int []intArr2 = new int[20];    // size 20, index range from 0 to 19
        int intArr3[] = new int[20];    // size 20, index range from 0 to 19
}

์œ„์—์„œ ์„ค๋ช…๋“œ๋ฆฐ ์„ ์–ธ๋ฌธ๊ณผ ์ดˆ๊ธฐํ™”๋ฌธ์„ ํ•œ ์ค„์— ๋ถ™์—ฌ ์“ฐ๋ฉด ๋™์‹œ์— ์„ ์–ธ๊ณผ ์ดˆ๊ธฐํ™”๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ž, ๊ทธ๋Ÿฐ๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด int ํƒ€์ž…์˜ ๋ฐฐ์—ด์—๋Š” ๋ชจ๋‘ 0์œผ๋กœ ๊ฐ’์ด ์„ธํŒ…์ด ๋ฉ๋‹ˆ๋‹ค.

์ „, ์ œ๊ฐ€ ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ์„ธํŒ…์„ ํ•˜๊ณ  ์‹ถ์€๋ฐ ๊ทธ๋ ‡๊ฒŒ๋„ ํ•  ์ˆ˜ ์žˆ๋‚˜์š”?

๋ฌผ๋ก ์ด์ฃ ~ ๊ทธ๋ ‡๊ฒŒ๋„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์•„๋ž˜์™€ ๊ฐ™์ด ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

public static void main(String[] args) {

        int[] intArr4 = {1, 2, 3, 4};   // size 4, index range from 0 to 3
        int[] intArr5 = {0, 0, 0, 0, 0, 0, 0};  // size 7, index range from 0 to 6
}

intArr4์—๋Š” [1, 2, 3, 4]๊ฐ€ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
intArr5์—๋Š” new int[7] ๋กœ ์ดˆ๊ธฐํ™”ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์ด 0์œผ๋กœ ๊ฐ’์ด ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.

๋ฐฐ์—ด ์ถœ๋ ฅํ•˜๊ธฐ

๊ทธ๋Ÿผ ์ด๋ ‡๊ฒŒ ๋ฐฐ์—ด์— ๋“ค์–ด๊ฐ„ ๊ฐ’์„ ์ถœ๋ ฅํ•ด๋ณด๊ณ  ์‹ถ์„ ๋• ์–ด๋–ป๊ฒŒ ํ• ๊นŒ์š”?
์ž๋ฐ”์—์„œ ์ฝ˜์†”์ฐฝ์— ์ถœ๋ ฅ์„ ํ•  ๋•Œ์—๋Š” System.out.println() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋‹ค๋“ค ์•Œ๊ณ  ๊ณ„์‹œ์ฃ ?
Hello World๋ฅผ ํ•ด๋ณด์…จ๋‹ค๋ฉด ๋‹ค๋“ค ์•Œ๊ณ  ๊ณ„์‹ค๊ฒ๋‹ˆ๋‹ค.
๊ทธ๋Ÿผ ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ๋ณ€์ˆ˜์ด๋ฆ„์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋ ๊นŒ์š”?
์•„๋‹™๋‹ˆ๋‹ค. ๋ฐฐ์—ด์ด ๋‹ด๊ธด ๋ณ€์ˆ˜๋ช…๋งŒ ๋„ฃ์–ด์ฃผ๋ฉด ์“ฐ๋ ˆ๊ธฐ๊ฐ’์ด ์ถœ๋ ฅ์ด ๋ฉ๋‹ˆ๋‹ค.
์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” Arrays.toString() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

public static void main(String[] args) {

        int[] intArr1 = new int[10];    // size 10, index range from 0 to 9
        int []intArr2 = new int[20];    // size 20, index range from 0 to 19
        int intArr3[] = new int[20];    // size 20, index range from 0 to 19
        int[] intArr4 = {1, 2, 3, 4};   // size 4, index range from 0 to 3
        int[] intArr5 = {0, 0, 0, 0, 0, 0, 0};  // size 7, index range from 0 to 6

        System.out.println(Arrays.toString(intArr1));
        System.out.println(Arrays.toString(intArr2));
        System.out.println(Arrays.toString(intArr3));
        System.out.println(Arrays.toString(intArr4));
        System.out.println(Arrays.toString(intArr5));
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 2, 3, 4]
[0, 0, 0, 0, 0, 0, 0]

์ด์ƒ์œผ๋กœ ๋ฐฐ์—ด์˜ ์„ ์–ธ๊ณผ ์ดˆ๊ธฐํ™”์— ๋Œ€ํ•œ ํฌ์ŠคํŒ…์„ ๋งˆ์น˜๊ฒ ์Šต๋‹ˆ๋‹ค.
๊ถ๊ธˆํ•œ ์ ์ด ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์‹œ๋ฉด ๋‹ต๋ณ€๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

2์ฐจ์› ๋ฐฐ์—ด์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ [Java] ์ž๋ฐ” 2์ฐจ์› ๋ฐฐ์—ด ์ƒ์„ฑ(์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™”)์—์„œ ๋” ๋‹ค๋ฃจ๊ณ  ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์‹œ๊ตฌ์š”~

๋„์›€์ด ๋˜์…จ๋‹ค๋ฉด ๊ณต๊ฐ ๊พธ~~~~์šฑ ๋ˆŒ๋Ÿฌ์ฃผ๊ณ  ๊ฐ€์„ธ์š” ^-^

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

์ง€๋‚œ ์‹œ๊ฐ„์—๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ž€ ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•ด ๊ฐ„๋žตํ•˜๊ฒŒ ์„ค๋ช…์„ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๋„ˆ๋ฌด ์ง€๋ฃจํ•œ ์ด๋ก ์ ์ธ ์ด์•ผ๊ธฐ ๋ณด๋‹ค๋Š” ๊ฐœ๋ฐœ์ž๋กœ ๊ฐœ๋ฐœ์„ ํ•˜๋ฉด์„œ ๋Š๋‚Œ์ ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ •์˜๋ฅผ ๋‚ด๋ ธ์—ˆ๋Š”๋ฐ

์ดํ•ดํ•˜๋Š”๋ฐ ๋„์›€์ด ๋˜์—ˆ์œผ๋ฆฌ๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. 

 

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋ฌด์ž‘์ • ๋”ฐ๋ผํ•˜๊ธฐ ์‹œ๊ฐ„์„ ๊ฐ€์ ธ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์šฐ์„  ์—ฌ๋Ÿฌ๋ถ„๋“ค ์ปดํ“จํ„ฐ์— ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒƒ๋“ค์„ ์ค€๋น„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

1. JDK (Java Development Kit)

2. ์ดํด๋ฆฝ์Šค IDE (Integrated Development Environment)

 

์„ค์น˜ํ•˜๊ธฐ ์ „์— ์ด์นœ๊ตฌ๋“ค์ด ๋ญ”์ง€๋ถ€ํ„ฐ ์•Œ๊ณ  ์„ค์น˜๋ฅผ ํ•ด์•ผ๊ฒ ์ฃ ? ํ•ดํ‚นํ”„๋กœ๊ทธ๋žจ์ด๋ฉด ํฐ ์ผ ๋‚  ํ…Œ๋‹ˆ๊นŒ์š”.

์šฐ์„ , JDK๋ผ๋Š” ์นœ๊ตฌ๋Š” "์ž๋ฐ” ๊ฐœ๋ฐœ ํˆด" ์ค„์—ฌ์„œ "์ž๊ฐœํˆด"์ด๋ผ๋Š” ๋…€์„์ด์ฃ . ์ž๋ฐ” ์–ธ์–ด ์‚ฌ์ „์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ด ๋…€์„์ด ์žˆ์–ด์•ผ ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ์ž‘์„ฑํ•œ ์ž๋ฐ”์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ปดํ“จํ„ฐ๊ฐ€ ์ดํ•ดํ•˜๋Š” ์–ธ์–ด๋กœ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๊ณ ,

์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํƒœ์ƒ๋ถ€ํ„ฐ ํ˜„์žฌ๊นŒ์ง€ ๋ฌด๋ฃŒ์ž…๋‹ˆ๋‹ค. (Java 8์˜ ํŠน์ • ๋ฒ„์ „ ๋ถ€ํ„ฐ๋Š” ์œ ๋ฃŒ๋ผ์ด์„ผ์Šค๋กœ ์ „ํ™˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค)

 

๋‹ค์Œ์œผ๋กœ ์ดํด๋ฆฝ์Šค๋ผ๋Š” ์นœ๊ตฌ๋Š” ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ๊ฐœ๋ฐœ์„ ์ข€ ๋” ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ์œ„ํ•ด

์˜คํ”ˆ์†Œ์Šค๋กœ ๊ณต๊ฐœ๋œ ๋ฌด๋ฃŒ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ "ํ†ตํ•ฉ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ"์„ ์ œ๊ณตํ•˜๋Š” ๋„๊ตฌ์ž…๋‹ˆ๋‹ค.

๊ฐœ๋ฐœ์ž๊ฐ€ ์†Œ์„ค์ž‘๊ฐ€๋ผ๋ฉด ์ดํด๋ฆฝ์Šค IDE๋Š” ์›Œ๋“œํ”„๋กœ์„ธ์„œ์™€ ๊ฐ™์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

 

์ž, ๊ทธ๋Ÿผ ์„ค์น˜๋ฅผ ํ•˜๋Ÿฌ ๊ฐ€๋ณผ๊นŒ์š”??

 

1. JDK 8 (Java SE Development Kit 8) ์„ค์น˜ -> ๋‹ค์šด๋กœ๋“œ ๋งํฌ

-> ํ˜น์‹œ ๋‹ค์šด๋กœ๋“œ ๋งํฌ ๋“ค์–ด๊ฐ”๋Š”๋ฐ cookie ๊ด€๋ จ ์•„๋ž˜์™€ ๊ฐ™์€ ์ฐฝ์ด ๋œฌ๋‹ค๋ฉด ๊ทธ๋ƒฅ I agree ํ•˜์ง€๋งˆ์‹œ๊ณ  ์•„๋ž˜์ฒ˜๋Ÿผ ๋”ฐ๋ผํ•ด์ฃผ์„ธ์š”.

1. ์ฃผํ™ฉ์ƒ‰ ๋ฒ„ํŠผ ํด๋ฆญ
2. Required Cookies ๋งŒ ์„ ํƒ๋˜๋„๋ก ํ•œ๋’ค Submit Preferences  ์„ ํƒ
3. Close ๋ฒ„ํŠผ ์„ ํƒ
4. Accept License Agreement ์„ ํƒ ํ›„ ๋ณธ์ธ ์šด์˜์ฒด์ œ์— ๋งž๋Š” ๋ฒ„์ „์„ ๋‹ค์šด๋กœ๋“œ

2. ์ดํด๋ฆฝ์Šค(๊ฐœ๋ฐœํˆด, IDE) ์„ค์น˜ -> ๋‹ค์šด๋กœ๋“œ ๋งํฌ

 

1. Download ์„ ํƒ

 

3. ์ด์ œ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์€ JDK ์„ค์น˜ ํŒŒ์ผ์„ "๋”๋ธ”ํด๋ฆญ"ํ•ด์„œ ์„ค์น˜ํ•ด์ฃผ์„ธ์š”.

-> ์„ค์น˜ ์‹œ ์„ธํŒ…(์„ค์น˜ ๊ฒฝ๋กœ ๋ณ€๊ฒฝ ๋“ฑ)์€ ํ•˜์ง€๋ง๊ณ  ๊ธฐ๋ณธ๊ฐ’ ๊ทธ๋Œ€๋กœ Next, Next ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ์œˆ๋„์šฐ์—์„œ๋Š” cmd์ฐฝ, Mac์—์„œ๋Š” ํ„ฐ๋ฏธ๋„์ฐฝ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž๋ฐ”์˜ ๋ฒ„์ „์„ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด๋ณด์„ธ์š”.

java -version

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋ฒ„์ „์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž๋ฐ” ๋ฒ„์ „ ํ™•์ธ

์—ฌ๊ธฐ์„œ ํ™•์ธํ•  ๊ฒƒ์€ ์„ธ ๋ฒˆ ์งธ ์ค„์— HotSpot์ด๋ผ๊ณ  ๋œจ๋Š”์ง€๊ฐ€ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

(์ž๋ฐ”๋Š” ์‹คํ–‰ํ™˜๊ฒฝ๋งŒ ์ œ๊ณตํ•˜๋Š” JRE์™€ ๊ฐœ๋ฐœํ™˜๊ฒฝ๊นŒ์ง€ ์ œ๊ณตํ•˜๋Š” JDK๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์ €ํฌ๋Š” JDK๋ฅผ ์„ค์น˜ํ•œ ๊ฒƒ์ด๊ณ  JDK์˜ ๋ฒ„์ „์—๋Š” ์ €๋ ‡๊ฒŒ HotSpot Server VM์ด๋ผ๋Š” ๊ฒŒ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.)

 

์—ฌ๊ธฐ๊นŒ์ง€ ์ž˜ ๋”ฐ๋ผ์˜ค์…จ๋‹ค๋ฉด ์ด์ œ ์ดํด๋ฆฝ์Šค๋ฅผ ์„ค์น˜ํ•ด์ฃผ์„ธ์š”.

์ดํด๋ฆฝ์Šค ์„ค์น˜ ํ™”๋ฉด

์šฐ๋ฆฌ๋Š” ์ดํด๋ฆฝ์Šค ์ธ์Šคํ†จ๋Ÿฌ์—์„œ ์œ„์—์„œ ๋‘ ๋ฒˆ์งธ ๊บผ๋ฅผ ์„ ํƒํ•ด์„œ ์„ค์น˜ํ• ๊ฒ๋‹ˆ๋‹ค. 

์„ค์น˜ ์œ„์น˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ธํŒ…์ด ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ INSTALL ๋ฒ„ํŠผ์„ ๊พน ๋ˆŒ๋Ÿฌ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ Java ๊ฒฝ๋กœ๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š๋Š”๋‹ค๋ฉด Java ์„ค์น˜๊ฐ€ ์ œ๋Œ€๋กœ ์•ˆ๋˜์—ˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ ํ™•์ธ์„ ํ•ด๋ณด์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์„ค์น˜๊ฐ€ ์ง„ํ–‰๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 

์ด๋Ÿฐ ์ €๋Ÿฐ ํŒŒ์ผ๋“ค์„ ์ธํ„ฐ๋„ท์—์„œ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„๊ฐ€๋ฉด์„œ ์„ค์น˜๋ฅผ ํ•ฉ๋‹ˆ๋‹ค.

 

์ค‘๊ฐ„์— ์ด๋Ÿฐ ๋ผ์ด์„ผ์Šค ๋™์˜ ํŒ์—…์ฐฝ์ด ๋œจ๋ฉด "Remember accepted licenses"์— ์ฒดํฌํ•ด์ฃผ๊ณ  "Accept"๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์ค๋‹ˆ๋‹ค.

์—ฌ๊ธฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. "Remember choice"์— ์ฒดํฌํ•ด์ฃผ์‹œ๊ณ , "Accept" ๊ณ ๊ณ ์‹ฑ~

์ด ํ™”๋ฉด์€ ์œˆ๋„์šฐ ์šด์˜์ฒด์ œ์—์„œ๋Š” ์•ˆ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์œผ๋‹ˆ, "์ด๋Ÿฐ ํ™”๋ฉด์€ ์™œ ์•ˆ๋‚˜์˜ค์ง€?"ํ•˜์‹ค ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

์œˆ๋„์šฐ์—์„œ๋Š” ๊ทธ๋ƒฅ next, next ํ•˜์‹œ๋ฉด ๋ ๊ฒ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ๋„ ์ฒดํฌํ•ด์ฃผ๊ณ  "Accept selected" ๊ณ ๊ณ ์‹ฑ~

์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด Launch ๋ฒ„ํŠผ์ด ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค.

์ดํด๋ฆฝ์Šค ์„ค์น˜ ์™„๋ฃŒ ํ™”๋ฉด

์ด์ œ Launch ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์‹คํ–‰์„ ์‹œ์ผœ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

workspace ๊ฒฝ๋กœ ์„ค์ •์ฐฝ

์‹คํ–‰ํ•˜๋ฉด ์œ„์™€ ๊ฐ™์€ ์ฐฝ์ด ๋œจ๋Š”๋ฐ ๋ฌด์‹œํ•˜๊ณ  ๊ทธ๋ƒฅ "Launch" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. (๊ฒฝ๋กœ๋Š” ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)

welcome ์ฐฝ

welcome ์ฐฝ์ด ๋– ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ์ €๋Ÿฐ ๋ฉ”๋‰ด๊ฐ€ ๋ณด์ด๊ณ  ๋ฐ”๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ฉ”๋‰ด๋„ ๋ณด์ž…๋‹ˆ๋‹ค.

์ด์ œ ๊ฐ„๋‹จํ•œ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฐ”๋กœ "์•ˆ๋…• ํ”„๋กœ์ ํŠธ"์ž…๋‹ˆ๋‹ค.

welcome ์ฐฝ์˜ ์šฐ์ธก์—์„œ "Tutorials"๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

"Create a Hello World application"์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

๋ญ”๊ฐ€ ๋ฐ”๋‘‘ํŒ์ฒ˜๋Ÿผ ์ƒ๊ธด ๋ณต์žกํ•ด๋ณด์ด๋Š” ํ™”๋ฉด์ด ๋‚˜์™”๋Š”๋ฐ ์‹ ๊ฒฝ๋„๊ณ  ์˜ค๋ฅธ์ชฝ์— ๋ณด์ด๋Š” "์›ฐ์ปด์ฐฝ"๋งŒ ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผํ•˜๊ธฐ์ฒ˜๋Ÿผ ๋˜์–ด์žˆ๋Š”๋ฐ ๋ฒ„ํŠผ๋งŒ ๋ˆ„๋ฅด๋ฉด ์•Œ์•„์„œ ์ด๊ฒƒ์ €๊ฒƒ ํ•ด์ค๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ ์ˆœ์„œ์ž…๋‹ˆ๋‹ค.

Hello World ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์ž‘์—… ์ˆœ์„œ๋ฅผ ํ•œ๋ฒˆ ์—ฐ์Šตํ•˜๊ณ 

๋‹ค์Œ์— ๋‹ค๋ฅธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค ๋•Œ ๋™์ผํ•œ ์ˆœ์„œ๋กœ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

๊ฐ€์šด๋ฐ ํŒŒ๋ž€์ƒ‰์œผ๋กœ "Click to Begin"์„ ํด๋ฆญํ•ด๋ด…์‹œ๋‹ค.

๋ฐ”๋กœ ์•„๋ž˜์ชฝ์— ์žˆ๋Š” "Open the Java perspective" ๋ฉ”๋‰ด๊ฐ€ ์—ด๋ฆฌ๋ฉด์„œ ์ด๋Ÿฐ์ €๋Ÿฐ ์„ค๋ช…์ด ๋˜ ๋‚˜์˜ต๋‹ˆ๋‹ค.

์„ค๋ช…์€ ์–ด๋ ต์ง€ ์•Š์œผ๋‹ˆ ํ•œ๋ฒˆ ์ฝ์–ด๋ณด๊ณ , "Click to perform"์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

Java Perspective๊ฐ€ ์„ ํƒ๋จ

๋ญ”๊ฐ€ ์‹คํ–‰ํ•˜๋Š” ๋“ฏ ํ•˜๋”๋‹ˆ ์ดํด๋ฆฝ์Šค ์ฐฝ ์šฐ์ƒ๋‹จ์— ์„ ํƒ๋œ ์•„์ด์ฝ˜์ด ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์›๋ž˜ ์ปคํ”ผ์ฝฉ ๋ชจ์–‘์ด ์žˆ๋Š” ์•„์ด์ฝ˜์ด ์„ ํƒ๋˜์–ด์žˆ์—ˆ๋Š”๋ฐ, ์ด์ œ J๋ผ๋Š” ๊ธ€์ž๊ฐ€ ์žˆ๋Š” ์•„์ด์ฝ˜์ด ์„ ํƒ๋˜์–ด์ ธ ์žˆ๋„ค์š”.

๊ทธ๋ฆฌ๊ณ  ์›ฐ์ปด์ฐฝ์—์„œ๋Š” ๋‹ค์Œ ๋‹จ๊ณ„์ธ "Create a Java project" ๋ฉ”๋‰ด๊ฐ€ ์—ด๋ ธ์Šต๋‹ˆ๋‹ค.

์„ค๋ช…์„ ํ•œ๋ฒˆ ์ฝ์–ด๋ณด๊ณ  ๋˜ "Click to perform" ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

์ž๋ฐ” ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์ฐฝ

์ž๋ฐ” ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์ฐฝ์ด ๋–ด๋Š”๋ฐ ์—ฌ๊ธฐ์„œ "Project name"๋งŒ ์ž…๋ ฅํ•ด์ฃผ๊ณ  "Finish"๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

์ขŒ์ธก์˜ "Project Explorer" ๋ทฐ์— ์ž…๋ ฅ๋งŒ ํ”„๋กœ์ ํŠธ๋ช…์ด ๋ณด์ด๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

์ƒ์„ฑ๋œ HelloWorld ํ”„๋กœ์ ํŠธ

์ด์ œ ๋‹ค์Œ ๋‹จ๊ณ„์ธ "Create your HelloWorld class" ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์„ค๋ช…์— ์žˆ๋Š”๋Œ€๋กœ ๋”ฐ๋ผํ•˜๊ฑฐ๋‚˜ "Click to perform"์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

HelloWorld ํด๋ž˜์Šค ์ƒ์„ฑ์ฐฝ

ํด๋ž˜์Šค ์ƒ์„ฑ์ฐฝ์ด ๋œจ๋Š”๋ฐ ์—ฌ๊ธฐ์„œ๋Š” "Name"์— "HelloWorld" ์ž…๋ ฅ ํ›„ ์•„๋ž˜์ชฝ์— "public static void main(String[] args)" ์˜†์˜ ์ฒดํฌ๋ฐ•์Šค๋ฅผ ์ฒดํฌํ•˜๊ณ  "Finish" ํ•ฉ๋‹ˆ๋‹ค.

์ƒ์„ฑ๋œ HelloWorld ํด๋ž˜์Šค

HelloWorld ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์ขŒ์ธก์˜ Package Explorer ์—๋„ ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ ,

๊ฐ€์šด๋ฐ ํ……ํ…… ๋น„์–ด์žˆ๋˜ ์ฐฝ์—๋„ ๋ญ”๊ฐ€๊ฐ€ ๋‚˜ํƒ€๋‚ฌ์Šต๋‹ˆ๋‹ค. 

์ด๊ฒƒ์ด ๋ฐ”๋กœ ์ž๋ฐ” ๋ฌธ๋ฒ•์— ๊ธฐ์ดˆํ•œ ์†Œ์Šค์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

์ž, ์ด์ œ ์ฒ˜์Œ์œผ๋กœ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณผ ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค.

์›ฐ์ปด์ฐฝ์˜ ์„ค๋ช…๋Œ€๋กœ ์•„๋ž˜์ฒ˜๋Ÿผ ์ถœ๋ ฅ๋ฌธ์„ ํ•˜๋‚˜ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

Hello world! ์ถœ๋ ฅ๋ฌธ ์ž‘์„ฑ

์œ„ ์ฒ˜๋Ÿผ ํ•œ์ค„์„ ์ถ”๊ฐ€ํ•˜๊ณ  ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

์ด์ œ ๋งˆ์ง€๋ง‰์œผ๋กœ ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์›ฐ์ปด์ฐฝ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์˜ ์„ค๋ช…์„ ๋”ฐ๋ผ์„œ ์‹คํ–‰ํ•ด๋ด…๋‹ˆ๋‹ค.

HelloWorld ์‹คํ–‰ํ•˜๊ธฐ
HelloWorld ์‹คํ–‰ ๊ฒฐ๊ณผ

HelloWorld์— ์ž‘์„ฑํ•œ ์ถœ๋ ฅ๋ฌธ์— ๋”ฐ๋ผ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ๊นŒ์ง€๊ฐ€ HelloWorld ํŠœํ† ๋ฆฌ์–ผ์˜ ๋์ž…๋‹ˆ๋‹ค.

 

์šฐ๋ฆฌ๋Š” ํ•˜๋‚˜๋งŒ ๋” ํ•ด๋ณด์ฃ .

System.out.println("Hello world!");

์—ฌ๊ธฐ์„œ "Hello world!" ๋ฅผ "์•ˆ๋…• ํ”„๋กœ์ ํŠธ ์„ฑ๊ณต" ์œผ๋กœ ๋ฐ”๊ฟ”์„œ ์ถœ๋ ฅํ• ๊ฒ๋‹ˆ๋‹ค.

์•„๋ž˜์ฒ˜๋Ÿผ ๋ณ€๊ฒฝํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

System.out.println("์•ˆ๋…• ํ”„๋กœ์ ํŠธ ์„ฑ๊ณต");

์ด์ œ ๋‹ค์‹œ ์‹คํ–‰ํ•ด๋ณด์„ธ์š”. 

์•„๋ž˜์ฒ˜๋Ÿผ ๊ฒฐ๊ณผ๊ฐ€ ์ž˜ ๋‚˜์˜ค๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

์—ฌ๊ธฐ๊นŒ์ง€ ์ž˜ ๋”ฐ๋ผ ์˜ค์…จ์Šต๋‹ˆ๋‹ค.

์—ฌ๋Ÿฌ๋ถ„์€ ์ด์ œ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 

 

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” JDK ์„ค์น˜์™€ ์ดํด๋ฆฝ์Šค IDE ์„ค์น˜ ๊ทธ๋ฆฌ๊ณ  ํ—ฌ๋กœ์›”๋“œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งŒ๋“ค๊ธฐ ๊นŒ์ง€

๋ฌด์ž‘์ • ๋”ฐ๋ผํ•ด๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์กŒ์Šต๋‹ˆ๋‹ค.

์ด์ œ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ๊ธฐ๋ณธ์ ์ธ ์ค€๋น„๋Š” ๋ชจ๋‘ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค.

 

๋‹ค์Œ ์‹œ๊ฐ„๋ถ€ํ„ฐ๋Š” ์ž๋ฐ”์–ธ์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ง์ ‘ ๋งŒ๋“ค์–ด๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ง€๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์†Œ์Šค ์ฝ”๋“œ ์ผ๋ถ€

์„œ๋ฌธ

๋‚˜๋Š” ์ด ๊ธ€์„ ์“ฐ๋Š” ์ง€๊ธˆ 7๋…„์ฐจ ์ž๋ฐ” ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž์ด๋‹ค.

๋‚˜๋Š” ์ž๋ฐ”๋ฅผ ์ œ๋Œ€๋กœ ๊ณต๋ถ€ํ•˜์ง€๋„ ์•Š๊ณ  ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜์—ˆ๊ณ , ๊ฐœ๋ฐœ์ž๊ฐ€ ๋œ์ง€ 5๋…„์ด ์ง€๋‚˜์„œ์•ผ ์ฑ…์„ ์ฝ์–ด๋ดค๋‹ค.

์ˆ˜๋งŽ์€ ์ฑ…๋“ค์ด ์žˆ์—ˆ๊ณ  ๊ทธ ์ค‘ ์ œ์ผ ์œ ๋ช…ํ•œ ๋‚จ๊ถ์„ฑ๋‹˜์˜ ์ฑ…๊ณผ ์œค์„ฑ์šฐ๋‹˜์˜  ์ฑ…์„ ์‚ฌ์„œ ํ›‘์–ด๋ณด์•˜๋‹ค.

์ฑ…์€ ๋„ˆ๋ฌด ๋‘๊บผ์› ๊ณ  ๋‹ค์‹œ๊ธˆ ๊นŒ๋จน๊ณ  ์žˆ๋˜ ๊ธฐ์ดˆ์ง€์‹์„ ํ™•์ธํ•˜๋Š” ๊ธฐํšŒ๊ฐ€ ๋˜์—ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ํ•œ ํ•™์ƒ์„ ๊ฐ€๋ฅด์น˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ ๋น„์ „๊ณต์ž๋ผ ์ฑ…์˜ ๋‚ด์šฉ์„ ์ดํ•ดํ•˜๋Š”๋ฐ ํž˜๋“ค์–ดํ–ˆ๋‹ค.

์–ธ์  ๊ฐ„ ๋‚˜๋„ ์ฑ…์„ ๋‚ด๋ด์•ผ์ง€๋ผ๋Š” ์ƒ๊ฐ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ํ„ฐ๋ผ ์ด์ฐธ์— ์ฑ…์˜ ๊ธฐ์ดˆ๊ฐ€ ๋ ๋งŒํ•œ ๊ฐ•์˜๋ฅผ ์—ฌ๊ธฐ์„œ ํ•ด๋ณผ๊นŒ ํ•œ๋‹ค.

์ผ๋ฐ˜์ธ๋“ค, ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๋ ค๋Š” ๋น„์ „๊ณต์ž๋“ค, ๊ทธ๋ฆฌ๊ณ  ์–ด๋ฆฐ ํ•™์ƒ๋“ค์ด ์ฝ์–ด๋„ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ๋‚ด์šฉ์œผ๋กœ ๊ฐ•์˜๋ฅผ ํ•˜๋ ค ํ•œ๋‹ค.

๋‚ด ์•„์ด๊ฐ€ ๋‚˜์ค‘์— ์ปค์„œ๋„ ์ž๋ฐ”๊ฐ€ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค๋ฉด ์ฝ์–ด๋ณด๋„๋ก ์ถ”์ฒœํ•  ์ˆ˜ ์žˆ์„ ์ •๋„์˜ ํ€„๋ฆฌํ‹ฐ๋ฅผ ๊ฐ–๋Š” ๋‚ด์šฉ์œผ๋กœ ๋ง์ด๋‹ค.

๊ทธ๋ž˜์„œ ์–ด๋–ป๊ฒŒ ์‰ฝ๊ฒŒ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์„์ง€ ๋งŽ์€ ๊ณ ๋ฏผ์„ ํ•ด์•ผํ•  ๊ฒƒ์ด๊ธฐ์— ๊ฐ•์˜๊ฐ€ ๋นจ๋ฆฌ ์ง„ํ–‰๋˜๊ธฐ๋Š” ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์œผ๋‚˜ ์กฐ๊ธˆ์”ฉ ๊พธ์ค€ํžˆ ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

 

 

์˜ค๋Š˜์€ ๊ทธ 1์ผ์ฐจ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ž€ ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•ด ์–˜๊ธฐ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์ž๋ฐ”๋ž€?

์„ธ์ƒ์—๋Š” ๋งŽ์€ ์–ธ์–ด๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๊ตญ์–ด, ์ค‘๊ตญ์–ด, ์˜์–ด, ์ผ๋ณธ์–ด, ์ŠคํŽ˜์ธ์–ด, ๋ถˆ์–ด, ๋…์–ด ๋“ฑ๋“ฑ ๋ง์ด์ฃ .

ํ•œ๊ตญ์‚ฌ๋žŒ๋“ค์€ ํ•œ๊ตญ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์ผ๋ณธ์‚ฌ๋žŒ๋“ค์€ ์ผ์–ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์–ด๋–ค ์‚ฌ๋žŒ๋“ค์€ 3๊ฐœ๊ตญ์–ด 4๊ฐœ๊ตญ์–ด๋ฅผ ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

์ผ๋ณธ์–ด๋Š” ํ•œ๊ตญ์–ด์™€ ๋ฌธ๋ฒ•์ด ์œ ์‚ฌํ•ด์„œ ์‰ฝ๊ณ , ์˜์–ด๋Š” ๋ฌธ๋ฒ•์ด ๋‹ฌ๋ผ ๋ฐฐ์šฐ๊ธฐ ์–ด๋ ต๋‹ค๊ณ ๋“ค ํ•ฉ๋‹ˆ๋‹ค.

์–ธ์–ด๋งˆ๋‹ค ํŠน์ • ํ‘œํ˜„์„ ํ•  ๋•Œ ์ข€๋” ๊ณต๊ฐ์ด ๊ฐ€๋Š” ๋ฌธ์žฅ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•œ๊ตญ์–ด๋กœ ์ง์—ญ ๋˜๋Š” ์˜์—ญํ•ด๋„ ์˜์–ด์˜ ํŠน์ • ๋ฌธ์žฅ์˜ ๋Š๋‚Œ์„ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌํ•˜๊ธฐ๋Š” ํž˜๋“ค ๋•Œ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ง์ด์ฃ .

 

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—๋„ ๊ทธ๋Ÿฌํ•œ ๋‚ด์šฉ์ด ๋”ฑ ๋“ค์–ด๋งž์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”๋Š” ์ˆ˜๋งŽ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด(์„ธ๊ณ„ ๊ฐ๊ตญ์˜ ์–ธ์–ด)๋“ค ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. 

์ž๋ฐ” ๊ฐœ๋ฐœ์ž(ํ•œ๊ตญ์ธ)๋“ค์€ ์ž๋ฐ”(ํ•œ๊ตญ์–ด)๋กœ ๊ฐœ๋ฐœ์„ ํ•ฉ๋‹ˆ๋‹ค.

๋ชจ๊ตญ์–ด๋Š” ํ•œ๊ตญ์–ด์ด์ง€๋งŒ 3๊ฐœ๊ตญ์–ด, 4๊ฐœ๊ตญ์–ด๋ฅผ ํ•  ์ค„ ์•„๋Š” ์‚ฌ๋žŒ๋“ค์ด ์žˆ๋“ฏ์ด,

์ž๋ฐ” ๊ฐœ๋ฐœ์ž๋“ค๋„ ์ž๋ฐ”๋งŒ ํ•˜๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํŒŒ์ด์ฌ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ, C# ๋“ฑ์˜ ์–ธ์–ด๋ฅผ ์ œ2์™ธ๊ตญ์–ด์ฒ˜๋Ÿผ ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

์ž๋ฐ”์™€ ๋ฌธ๋ฒ•์ด ์œ ์‚ฌํ•œ ์–ธ์–ด๋Š” ์ž๋ฐ”๊ฐœ๋ฐœ์ž๋“ค์—๊ฒŒ๋Š” learning curve๊ฐ€ ๋‚ฎ์ง€๋งŒ, ๋„ˆ๋ฌด ๋‹ค๋ฅธ ์–ธ์–ด๋Š” ์ ์‘ํ•˜๊ธฐ๊ฐ€ ํž˜๋“ค์ฃ .

๋งˆ์น˜ ํ•œ๊ตญ์–ด์™€ ์ผ๋ณธ์–ด, ํ•œ๊ตญ์–ด์™€ ์˜์–ด์˜ ์ฐจ์ด์ฒ˜๋Ÿผ ๋ง์ด์ฃ .

์ž๋ฐ”๋กœ๋Š” ๊ตฌํ˜„ํ•˜๊ธฐ ์‰ฌ์šด ๊ธฐ๋Šฅ์ด ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ๋Š” ๊ตฌํ˜„ํ•˜๊ธฐ ํž˜๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์‚ฌ๋žŒ๋“ค์€ ์–ธ์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค๊ณผ ์†Œํ†ต์„ ํ•ฉ๋‹ˆ๋‹ค.

์–ดํœ˜๋ ฅ์ด ์ข‹์€ ์‚ฌ๋žŒ์ด ์žˆ๊ณ  ์•ˆ์ข‹์€ ์‚ฌ๋žŒ๋„ ์žˆ์ฃ .

๋Œ€ํ™”์˜ ๋…ผ๋ฆฌ๊ฐ€ ํ™•์‹คํžˆ ์žกํ˜€์žˆ๋Š” ์‚ฌ๋žŒ๋„ ์žˆ๊ณ  ๋…ผ๋ฆฌ๋Š” ์—†๊ณ  ์•ž๋’ค์—†๋Š” ๋Œ€ํ™”๋ฅผ ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

๊ฐœ๋ฐœ์ž๋“ค์€ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์†Œํ†ต์„ ํ•ฉ๋‹ˆ๋‹ค.

์ž๋ฐ” ์–ธ์–ด์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฌธ๋ฒ•์„ ๋งŽ์ด ์•Œ๊ณ ์žˆ๋Š” ๊ฐœ๋ฐœ์ž๋„ ์žˆ๊ณ  ๊ธฐ์ดˆ์ ์ธ ๊ฒƒ๋งŒ ์•Œ๊ณ ์žˆ๋Š” ๊ฐœ๋ฐœ์ž๋„ ์žˆ์ฃ .

๋™์ผํ•œ ๋ฌธ์ œ๋ฅผ ํ’€๋•Œ์—๋„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๊ฐ„๊ฒฐํ•˜๊ณ  ๋ช…ํ™•ํ•œ ๊ฐœ๋ฐœ์ž๋„ ์žˆ๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฐœ๋ฐœ์ž๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฆ‰, ์ž๋ฐ”๋ผ๋Š” ์–ธ์–ด๋Š” ๊ฐœ๋ฐœ์ž๋“ค์ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ œ2์™ธ๊ตญ์–ด์™€ ๊ฐ™์€ ์กด์žฌ์ž…๋‹ˆ๋‹ค.

 

์†Œ์Šค์ฝ”๋“œ์™€ ์†Œ์„ค

์ œ ์™€์ดํ”„๋Š” ์ œ๊ฐ€ IDE(๊ฐœ๋ฐœํˆด)ํ™”๋ฉด์— ์ฝ”๋”ฉ์„ ํ•˜๋Š” ๋ชจ์Šต์„ ๋ณด๊ณ , "์ด๊ฒŒ ๋ญ์•ผ? ๋„๋Œ€์ฒด ๋ญ๋ผ๊ณ  ์“ฐ๋Š”๊ฑด์ง€ ํ•˜๋‚˜๋„ ๋ชจ๋ฅด๊ฒ ๋„ค. ์ด๋Ÿฐ๊ฒŒ ์ดํ•ด๊ฐ€ ๋˜?" ๋ผ๊ณ  ์งˆ๋ฌธ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋‹น์—ฐํžˆ ๊ทธ๋Ÿฐ ๋ฐ˜์‘์ด ๋‚˜์˜ฌ ์ˆ˜ ๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค. ์™€์ดํ”„ ์ž…์žฅ์—์„œ๋Š” ์ œ๊ฐ€ ์˜ค์ง€์˜ ์ธ๋””์–ธ์ด ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด๋กœ ๊ธ€์„ ์“ฐ๊ณ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋Š๊ปด์งˆ ํ…Œ๋‹ˆ๊นŒ์š”. 

๊ฐœ๋ฐœ์ž๋“ค์€ ์ž์‹ ์ด ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด(์ œ2์™ธ๊ตญ์–ด)๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž์‹ ๋“ค๋งŒ์˜ ๊ธ€์„ ์จ๋‚ด๋ ค ๊ฐ‘๋‹ˆ๋‹ค. ๊ฐ™์€ ๋‚ด์šฉ์— ๋Œ€ํ•ด์„œ ๊ฐœ๋ฐœ์„ ํ•œ๋‹คํ•ด๋„ ๊ฐœ๋ฐœ์ž๋“ค ์„ฑํ–ฅ์— ๋”ฐ๋ผ 1. ํšจ์œจ์„ฑ์€ ์กฐ๊ธˆ ๋–จ์–ด์ง€์ง€๋งŒ ๊ฐ€๋…์„ฑ์ด ์ข‹์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๊ณ , 2. ํšจ์œจ์„ฑ์€ ๊ทน๋Œ€ํ™”ํ–ˆ์ง€๋งŒ ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ํž˜๋“  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ €๋Š” ์ „์ž๋ฅผ ์ง€ํ–ฅํ•˜๊ณ  ํ›„๋ฐฐ๋“ค์—๊ฒŒ๋„ ์ „์ž๋ฅผ ์ง€ํ–ฅํ•˜๋„๋ก ์กฐ์–ธํ•ฉ๋‹ˆ๋‹ค.

 

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

 

 

Wrap Up

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ž€ ๊ฐœ๋ฐœ์„ ์œ„ํ•œ ์–ธ์–ด๋กœ ์ผ์–ด, ์ค‘๊ตญ์–ด, ์˜์–ด์™€ ๊ฐ™์€ ์ œ2์™ธ๊ตญ์–ด์˜ ํ•˜๋‚˜๋‹ค.

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—๋Š” ์—ฌ๋Ÿฌ ์–ธ์–ด๋“ค์ด ์กด์žฌํ•˜๋Š”๋ฐ ๊ทธ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์ž๋ฐ”(java)๋‹ค.

 

๊ณ ๊ธ‰๋‚ด์šฉ ๋ณด์ถฉ

์ž๋ฐ”๋Š” ๋‹ค๋ฅธ ์–ธ์–ด๋“ค๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ JVM(Java Virtual Machine)์ด๋ผ๋Š” ๊ฐ€์ƒ๋จธ์‹  ์œ„์—์„œ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ๋ฌธ์— ํ”Œ๋žซํผ(OS, ์šด์˜์ฒด์ œ)์— ์˜์กด์ ์ด์ง€ ์•Š์ฃ . Linux๊ณ„์—ด์—์„œ๋„ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•˜๊ณ , Windows์—์„œ๋„ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ์•ˆ๋“œ๋กœ์ด๋“œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๋„ ์ž๋ฐ”๋กœ ๊ฐœ๋ฐœํ•ฉ๋‹ˆ๋‹ค(์š”์ฆ˜์€ Kotlin์ด๋ผ๋Š” ์–ธ์–ด๋„ ๋งŽ์ด ์‚ฌ์šฉํ•˜์‹œ๋”๊ตฐ์š”). ๋˜ํ•œ, JVM์ด ์ง์ ‘ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋ฅผ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„๋กœ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. C์–ธ์–ด๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ๋ฉ”๋ชจ๋ฆฌ์— ์ ‘๊ทผ์„ ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ด€๋ฆฌ๋„ ์ง์ ‘ ํ•ด์•ผํ•˜์ฃ .

์ž๋ฐ”๋Š” ์›น๊ฐœ๋ฐœ์— ๋งŽ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. Python(ํŒŒ์ด์ฌ)์€ ๋น…๋ฐ์ดํ„ฐ ์˜์—ญ์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๊ณ , C, C++์€ ์ž„๋ฒ ๋””๋“œ ์˜์—ญ์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋ฉฐ, C#์€ ๊ฒŒ์ž„๊ฐœ๋ฐœ์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

 

2019๋…„ 6์›” 15์ผ ๋กœ๋˜ 1๋“ฑ ๋‹น์ฒจ๋ฒˆํ˜ธ

์ด๋ฒˆ์—๋Š” ์ž๋ฐ”๋กœ ๋กœ๋˜ ๋ฒˆํ˜ธ ์ž๋™ ์ƒ์„ฑ๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๊ธฐ์ „์— ์šฐ์„  ๋กœ๋˜์˜ ๊ทœ์น™๋ถ€ํ„ฐ ์•Œ์•„์•ผ ๊ฒ ์ฃ .

๋กœ๋˜์—๋Š” 1๋ถ€ํ„ฐ 45๊นŒ์ง€์˜ ์ˆซ์ž๊ฐ€ ์กด์žฌํ•˜๊ณ 

๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ํฌํ•จํ•˜์—ฌ ์ด 7๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋žœ๋คํ•˜๊ฒŒ ๋ฝ‘์Šต๋‹ˆ๋‹ค.

๊ฐ ์ˆซ์ž๋Š” ์ค‘๋ณต๋˜์–ด ์„ ํƒํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ํ‚ค ํฌ์ธํŠธ ๋‹จ์–ด๋Š” ๋ฐ”๋กœ ๋žœ๋ค๊ณผ ์ค‘๋ณต ์ž…๋‹ˆ๋‹ค.

์ž๋ฐ”์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋Š” ๊ฐ€์žฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€ Set์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

์ž, ๊ทธ๋Ÿผ ๋žœ๋คํ•˜๊ฒŒ ์ˆซ์ž๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ๋ถ€ํ„ฐ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”์—์„œ ๋žœ๋คํ•œ ์ˆซ์ž๋ฅผ ์–ป์„ ๋•Œ๋Š” Math.random() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์ด ๋ฉ”์„œ๋“œ๋Š” 0๋ถ€ํ„ฐ 1์‚ฌ์ด์˜ double ํ˜•ํƒœ์˜ ์†Œ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ด์ค๋‹ˆ๋‹ค.

์˜ค์ž‰?? ๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ 1๋ถ€ํ„ฐ 45๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์–ป๋Š”๋‹ค๋Š” ๊ฑธ๊นŒ์š”??

์ด์ œ๋ถ€ํ„ฐ ํŠธ๋ฆญ์„ ์•Œ๋ ค๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

Math.random()์€ 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ  1๋ณด๋‹ค ์ž‘์€ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

๋‚ด๊ฐ€ ์–ป๊ณ ์žํ•˜๋Š” max์ˆซ์ž๋ฅผ ๊ณฑํ•ด์ฃผ๋ฉด

0๋ถ€ํ„ฐ max๊ฐ’ ์‚ฌ์ด์˜ double ํ˜•ํƒœ์˜ ์ˆซ์ž๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ ์ด ์ˆซ์ž๋ฅผ ๋‹ค์‹œ intํ˜•ํƒœ๋กœ casting์„ ํ•ด๋ฒ„๋ฆฌ๋ฉด?

0<= ์›ํ•˜๋Š” ์ˆซ์ž < max ์‚ฌ์ด์˜ int ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ณด์—ฌ๋“œ๋ฆฌ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

public static void main(String[] args) {
  getRandomNumber();
}
	
private static void getRandomNumber() {
  final int max = 46;
  for(int i = 0; i < 10; i++) {
    int randomNumber = (int)(Math.random() * max);
    System.out.println(randomNumber);
  }
}

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

0
12
34
27
5
21
10
24
43

์œ„ ๊ฒฐ๊ณผ์—๋Š” ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์•˜์ง€๋งŒ

๋ช‡ ๋ฒˆ ์‹คํ–‰ํ•˜๋‹ค๋ณด๋ฉด ๋™์ผํ•œ ์ˆซ์ž๊ฐ€ ์—ฐ์†์œผ๋กœ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•ด์ฃผ๋Š” ๊ณผ์ •์ด ํ•„์š”ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด๋•Œ ํ•„์š”ํ•œ ๊ฒƒ์ด ๋ฐ”๋กœ Set์ž…๋‹ˆ๋‹ค.

Set์€ ์ž๋ฐ”์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๊ณ ์ž ํ•  ๋•Œ ์•„์ฃผ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ด๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.

Set์˜ ๊ตฌํ˜„์ฒด๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ ์ผ๋ฐ˜์ ์œผ๋กœ ๋งŽ์ด ์“ฐ์ด๋Š” ๊ฒƒ์€ HashSet์ž…๋‹ˆ๋‹ค.

์•„๋ž˜ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ํ•œ๋ฒˆ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

public static void main(String[] args) {
  removeDuplicate();
}
	
private static void removeDuplicate() {
  Set<Integer> noDuplicated = new HashSet<>();
  noDuplicated.add(30);
  noDuplicated.add(30);
  noDuplicated.add(31);
  System.out.println(noDuplicated);
}

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

[30, 31]

๋ณด์‹œ๋Š” ๋ฐ”์™€ ๊ฐ™์ด 30์„ ๋‘ ๋ฒˆ add ํ–ˆ๋Š”๋ฐ 1๊ฐœ๋งŒ ์ €์žฅ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์ด๊ฒƒ์„ ์ด์šฉํ•ด์„œ ์ด๋ฏธ ๋ฝ‘์€ ์ˆซ์ž๋Š” ์ €์žฅํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ๋ฒˆํ˜ธ๋ฅผ ๋ฝ‘๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜๋Š” ์™„์„ฑ๋œ ๋กœ๋˜ ๋ฒˆํ˜ธ ์ƒ์„ฑ๊ธฐ์˜ ์ƒ˜ํ”Œ์ž…๋‹ˆ๋‹ค.

private static void lotto() {
  final int min = 1;
  final int max = 45;
		
  Set<Integer> selected = new HashSet<>();
  while(true) {
    int randomNumber = (int)(Math.random() * (max - min +1) + min);
    if(selected.add(randomNumber)) {
      if(selected.size() == 6) {
        break;
      }
    }
  }
  System.out.println(selected.stream().sorted().collect(Collectors.toList()));
}

์œ„ ๋ฉ”์„œ๋“œ๋ฅผ main ํ•จ์ˆ˜์—์„œ ์‹คํ–‰์‹œํ‚ค๋ฉด

1~45๊นŒ์ง€์˜ ์ˆซ์ž๋“ค ์ค‘์—์„œ ์ค‘๋ณต๋˜์ง€ ์•Š์€ 6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

๋žœ๋ค ๋ฒˆํ˜ธ ์ƒ์„ฑํ•˜๋Š” ๋ถ€๋ถ„์ด ์กฐ๊ธˆ ์ˆ˜์ •๋œ ๊ฒƒ์„ ๋ˆˆ์น˜์ฑ„์…จ๋‚˜์š”?

์ € ์œ„์—์„œ ๋ณด์—ฌ๋“œ๋ฆฐ ๋กœ์ง์œผ๋กœ๋Š” 0์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ˆ˜์ •์„ ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ž๋ฐ”๋กœ ๋žœ๋ค ์ˆซ์ž ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋žœ๋ค ์ˆซ์ž ๊ตฌํ•˜๊ธฐ ์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์œ„ ๋ฉ”์„œ๋“œ๋ฅผ 10๋ฒˆ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

[5, 17, 18, 25, 29, 33]
[9, 13, 16, 26, 30, 34]
[21, 24, 27, 30, 31, 42]
[9, 15, 17, 34, 36, 42]
[2, 3, 15, 35, 38, 41]
[8, 16, 30, 31, 36, 39]
[1, 5, 11, 40, 43, 44]
[1, 3, 4, 18, 21, 39]
[4, 7, 19, 28, 38, 40]
[7, 9, 14, 18, 30, 41]

์ด์ƒ์œผ๋กœ ์ž๋ฐ”๋กœ ์‹ฌํ”Œํ•œ ๋กœ๋˜ ๋ฒˆํ˜ธ ์ž๋™ ์ƒ์„ฑ๊ธฐ ๋งŒ๋“ค๊ธฐ ํฌ์ŠคํŒ…์„ ๋งˆ์น˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ถ๊ธˆํ•œ ์ ์ด ์žˆ์œผ์‹œ๋ฉด ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์‹œ๊ณ ,

ํฌ์ŠคํŒ… ๋‚ด์šฉ์ด ๋„์›€์ด ๋˜์…จ๋‹ค๋ฉด ๊ณต๊ฐ ๊พน ๋ถ€ํƒํ•ด์š” ^-^

 

์Šคํ”„๋ง๋ถ€ํŠธ ์Šค์ผ€์ฅด๋Ÿฌ

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์Šคํ”„๋ง๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ์—์„œ ์Šค์ผ€์ฅด๋ง ์ž‘์—…์„ ๋“ฑ๋กํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ ์Šค์ผ€์ฅด๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์šฐ์„  ์•„๋ž˜์ฒ˜๋Ÿผ

@EnableScheduling ์–ด๋…ธํ…Œ์ด์…˜์„ @Configuration์ด ๋ถ™์€ ํด๋ž˜์Šค์— ๋“ฑ๋กํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์Šค์ผ€์ฅด๋ง ํ™œ์„ฑํ™”๋ฅผ ์œ„ํ•œ @EnableScheduling ์‚ฌ์šฉ

 

์œ„ ์ฒ˜๋Ÿผ ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ์ฃผ๋ฉด

์Šคํ”„๋ง์ด ๊ด€๋ฆฌํ•˜๋Š” ๋นˆ ์ค‘์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด

@Scheduled ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์–ด์žˆ๋Š” ๊ฒƒ๋“ค์„

์ฐพ์•„์„œ ํ™œ์„ฑํ™” ์‹œ์ผœ์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๊ฒŒ๋ฉ๋‹ˆ๋‹ค.

 

์œ„ ์ฒ˜๋Ÿผ @Scheduled ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ์ฃผ๊ณ  ์‹คํ–‰์ฃผ๊ธฐ๋ฅผ ํŠน์ •์ง€์–ด์ฃผ๋ฉด

ํ•ด๋‹น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋˜๋ฉด ์ž๋™์œผ๋กœ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค.

 

์‹คํ–‰์ฃผ๊ธฐ๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

fixedRate : ๋งค ํŠน์ • ๋ฐ€๋ฆฌ์ดˆ๋งˆ๋‹ค ๋™์ž‘

fixedDelay : ํ•ด๋‹น ๊ธฐ๋Šฅ์ด ์ข…๋ฃŒ๋œ ํ›„ ํŠน์ • ๋ฐ€๋ฆฌ์ดˆ ์ดํ›„์— ๋‹ค์‹œ ๋™์ž‘

cron : ํฌ๋ก ํ‘œํ˜„์‹์— ์ •ํ•ด์ง„ ๋‚ด์šฉ๋Œ€๋กœ ๋™์ž‘

 

์—ฌ๋Ÿฌ ๊ฐœ์˜ ์Šค์ผ€์ฅด ์žก์„ ๋“ฑ๋กํ•  ๋•Œ์—๋Š” ์ฃผ์˜ํ•  ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์œ„ ์ฒ˜๋Ÿผ ๋‘ ๊ฐœ์˜ ์Šค์ผ€์ฅด ์žก์ด ๋“ฑ๋ก๋˜์–ด์žˆ์„ ๊ฒฝ์šฐ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ์žก๋งŒ ๋™์ž‘ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

์ฆ‰, @Scheduled๊ฐ€ ๋ถ™์€ ๊ธฐ๋Šฅ๋“ค์ด ์‹คํ–‰๋˜๋Š” ์‹œ๊ฐ„์ด ์ค‘๋ณต๋  ๊ฒฝ์šฐ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ์ž‘์—…๋งŒ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ๋‘ ๊ฐœ ์ด์ƒ์˜ ๊ธฐ๋Šฅ์ด ํ•œ๊บผ๋ฒˆ์— ์‹คํ–‰๋˜์–ด๋„ ์„ฑ๋Šฅ์— ๋ฌธ์ œ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์—๋Š”

์•„๋ž˜์ฒ˜๋Ÿผ @Async์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

@Async ์ฒ˜๋ฆฌํ•œ ์Šค์ผ€์ฅด tasks

 

๋ฌผ๋ก , @Async ๊ฐ€ ์ ์šฉ๋˜๋ ค๋ฉด @Configuration์— @EnableAsync๋„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ๊นŒ์ง€ ์„ค์ •์ด ๋˜์—ˆ๋‹ค๋ฉด ์œ„ ๋‘ scheduled task๋“ค์€ ๋™์‹œ์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

 

์ถ”๊ฐ€๋กœ @Async๋ฅผ ๋ถ™์ด์ง€ ์•Š์€ task๋ฅผ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€ํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ• ๊นŒ์š”?

task๋ฅผ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€ํ•ด์„œ ๊ผญ!! ํ…Œ์ŠคํŠธํ•ด๋ณด์„ธ์š”.

 

์ด์ƒ์œผ๋กœ ์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ ์Šค์ผ€์ฅด๋ง ์žก์„ ๋“ฑ๋กํ•˜์—ฌ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ฐ„๋‹จํžˆ ํฌ์ŠคํŒ… ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์œ ์šฉํ–ˆ๋‹ค๋ฉด ์ข‹์•„์š” ๊พน!๊พน!๊พน! ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š” ^-^

์ธํ„ฐ๋„ท ์„œํ•‘์„ํ•˜๋‹ค๊ฐ€ ์•ฑ๋ฒ„์ „์ด ๋ณ„๋„๋กœ ์—†๋Š” ์‚ฌ์ดํŠธ๋ฅผ ํ™ˆํ™”๋ฉด์— ๋“ฑ๋กํ•˜์—ฌ 

์•ฑ์„ ์‹คํ–‰ํ•˜๋“ฏ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐ”๋กœ๊ฐ€๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ฐค๋Ÿญ์‹œ S8 & ํฌ๋กฌ(Chrome)๋ธŒ๋ผ์šฐ์ € ๊ธฐ์ค€์œผ๋กœ ์Šค์ƒท์„ ์ฒจ๋ถ€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ ๋กœ ๊ธฐ์ข…(์ •ํ™•ํžˆ๋Š” ์•ˆ๋“œ๋กœ์ด๋“œOS ๋ฒ„์ „) ๋ฐ 

์‚ฌ์šฉํ•˜์‹œ๋Š” ๋ธŒ๋ผ์šฐ์ €์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ์Šค์ƒท์˜ ๋‚ด์šฉ์ด ์ƒ์ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.



1. ์šฐ์„  ํฌ๋กฌ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ด์šฉํ•˜์—ฌ  ๋ฐ”๋กœ๊ฐ€๊ธฐ๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ์€ ์›น์‚ฌ์ดํŠธ๋กœ ์ด๋™์„ ํ•ฉ๋‹ˆ๋‹ค.

2. ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ € ์šฐ์ƒ๋‹จ์˜ ๋ฉ”๋‰ด๋ฒ„ํŠผ(์„ธ๋กœ๋กœ ๋‚˜์—ด๋œ ๋™๊ทธ๋ผ๋ฏธ ์„ธ๊ฐœ)์„ ํ„ฐ์น˜ํ•˜์—ฌ ๋ฉ”๋‰ด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.


3. ํ™ˆ ํ™”๋ฉด์— ์ถ”๊ฐ€๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.




4. ๋ฐ”๋กœ๊ฐ€๊ธฐ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜๊ณ  ์ถ”๊ฐ€๋ฅผ ํ„ฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.




5. ๋‹ค์‹œ ํ•œ๋ฒˆ ์ถ”๊ฐ€๋ฅผ ํ„ฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.




6. ์•„๋ž˜์ฒ˜๋Ÿผ ํ™ˆ ํ™”๋ฉด์— ์•„์ด์ฝ˜์ด ์ƒ๊ธด ๊ฒƒ์„ ํ™•์ธํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


7. ์ด์ œ ์ƒ์„ฑ๋œ ๋ฐ”๋กœ๊ฐ€๊ธฐ๋ฅผ ํ„ฐ์น˜ํ•˜์—ฌ ์‹คํ–‰ํ•ด๋ณด์„ธ์š”~


์ด์ƒ์œผ๋กœ ์•ˆ๋“œ๋กœ์ด๋“œ ํฐ์˜ ํ™ˆ ํ™”๋ฉด์— ์›น์‚ฌ์ดํŠธ ๋ฐ”๋กœ๊ฐ€๊ธฐ ๋งŒ๋“ค๊ธฐ ํฌ์ŠคํŒ…์„ ๋งˆ์นฉ๋‹ˆ๋‹ค.




์ŠคํŠธ๋ง ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘๊ธฐ

 

์ž๋ฐ”์—์„œ ์ŠคํŠธ๋ง์„ ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ด๋“œ๋ฆฝ๋‹ˆ๋‹ค.

 

์ž๋ฐ”์—๋Š” ๊ฐ์ข… array๋กœ๋ถ€ํ„ฐ String์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” StringBuilder๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ƒ์„ฑ์ž์— String์„ ๊ทธ๋Œ€๋กœ ๋„ฃ์–ด์„œ ์ƒˆ๋กœ์šด ์ŠคํŠธ๋ง์„ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ตฌํ˜„๋˜์–ด์žˆ๋Š” reverse ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹จ ํ•œ ์ค„๋กœ ์ŠคํŠธ๋ง์„ ๊ฑฐ๊พธ๋กœ ๋ฐฐ์—ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

String a = "abcdefg";

String b = new StringBuilder(a).reverse().toString();

System.out.println(a);
System.out.println(b);

์ถœ๋ ฅ ๊ฒฐ๊ณผ 

abcdefg

gfedcba

 

์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ’ป Programming/JSP

[JSP] Security ( ๋ณด์•ˆ )

JSP์™€ servlets ์€ Web ๊ฐœ๋ฐœ์ž๋“ค์„ ์œ„ํ•ด์„œ ๋ณด์•ˆ(์ธ์ฆ)์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.  

์˜ค๋Š˜์€ ๊ทธ ์ค‘ ๋‘๊ฐ€์ง€์— ๋Œ€ํ•ด์„œ๋งŒ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

์—ญํ•  ๊ธฐ๋ฐ˜ ์ธ์ฆ

servlet ์—์„œ ์ œ๊ณตํ•˜๋Š” ์—ญํ•  ๊ธฐ๋ฐ˜ ์ธ์ฆ์€ ์‚ฌ์šฉ์ž ๋ ˆ๋ฒจ์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ œํ•œํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์—ญํ• ์„ ๋ถ€์—ฌํ•˜๊ณ  ํŠน์ • ์—ญํ• ์„ ๊ฐ€์ง„ ๋ชจ๋“  ์‚ฌ์šฉ์ž์— ๋Œ€ํ•ด์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ œํ•œํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.  

ํ†ฐ์บฃ ํ™ˆ ๋””๋ ‰ํ† ๋ฆฌ์˜ conf๋””๋ ‰ํ† ๋ฆฌ์— tomcat-users.xml ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์•„๋ž˜์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ์„ธ์š”.

<?xml version='1.0' encoding='utf-8'?> 
<tomcat-users> 
	<role rolename="tomcat"/> 
	<role rolename="role1"/> 
	<role rolename="manager"/> 
	<role rolename="admin"/> 
	<user username="tomcat" password="tomcat" roles="tomcat"/> 
	<user username="role1" password="tomcat" roles="role1"/> 
	<user username="both" password="tomcat" roles="tomcat,role1"/> 
	<user username="admin" password="secret" roles="admin,manager"/> 
</tomcat-users>

์œ„ ์ฝ”๋“œ๊ฐ€ ๋ญ˜ ํ•˜๊ณ ์žˆ๋Š”์ง€๋Š” ๋Œ€์ถฉ ๋ด๋„ ์•„์‹œ๊ฒ ์ฃ ? ์—ญํ• ์„ ์ƒ์„ฑํ•˜๊ณ  ์‚ฌ์šฉ์ž๋ฅผ ๋งŒ๋“ค๋•Œ ์—ญํ• ์„ ๋ถ€์—ฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด์ œ  web.xmlํŒŒ์ผ์— <security-constraint> ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์•„๋ž˜์ฒ˜๋Ÿผ ๋„ฃ์–ด๋ณด์„ธ์š”. ๋ฌผ๋ก  url-pattern์€ ์—ฌ๋Ÿฌ๋ถ„์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์›น์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์‹ค์ œ๋กœ ์กด์žฌํ•˜๋Š” url์ด์–ด์•ผ๊ฒŒ์ฃ ? ๊ทธ๋ž˜์•ผ ํ…Œ์ŠคํŠธ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค~ 

 

<web-app> 
    ... 
    <security-constraint> 
    	<web-resource-collection> 
        	<web-resource-name> SecuredBookSite </web-resource-name> 
            <url-pattern>/secured/*</url-pattern> 
            <http-method>GET</http-method> 
            <http-method>POST</http-method> 
        </web-resource-collection> 
        <auth-constraint> 
        	<description> Let only managers use this app </description> 
            <role-name>manager</role-name> 
        </auth-constraint> 
    </security-constraint> 
    <security-role> 
    	<role-name>manager</role-name> 
    </security-role> 
    <login-config> 
    	<auth-method>BASIC</auth-method> 
    </login-config> 
    ... 
</web-app>

์œ„ ์†Œ์Šค์ฝ”๋“œ๊ฐ€ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด ๋ฌด์–ธ์ธ๊ฐ€ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

  • HTTP GET ์ด๋‚˜ POST ๋ฐฉ์‹์˜ ์š”์ฒญ์— ๋Œ€ํ•ด์„œ /secured/* URL์— ๋Œ€ํ•œ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ๋ณด์•ˆ์„ค์ •์˜ ์ œ์•ฝ์„ ๋ฐ›๊ฒŒ๋ฉ๋‹ˆ๋‹ค.

  • ๋งค๋‹ˆ์ € ์—ญํ• ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์‚ฌ์šฉ์ž์— ๋Œ€ํ•ด์„œ๋งŒ /secured/* ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

  • ๋งˆ์ง€๋ง‰์œผ๋กœ login-config ํƒœ๊ทธ๊ฐ€ ๊ธฐ๋ณธํ˜•ํƒœ์˜ ์ธ์ฆ์„ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 

์ด์ œ /security๋‚ด๋ถ€์˜ ํŽ˜์ด์ง€๋กœ ์ ‘๊ทผ์„ ํ•˜๋ ค๋ฉด ์‚ฌ์šฉ์ž๋ช…๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๋ผ๋Š” ์ฐฝ์ด ๋œจ๊ฒŒ๋ฉ๋‹ˆ๋‹ค. ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋Š” ์—ญํ• ์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๊ฑฐ๋‚˜ ์ธ์ฆ์— ์‹คํŒจํ•˜๋ฉด ์ ‘์†์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‚˜ ํŽ˜์ด์ง€๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด์ฃ . 

 

Form์„ ์ด์šฉํ•œ ์ธ์ฆ

FORM ์ธ์ฆ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์‚ฌ์šฉ์ž์—๊ฒŒ ๋กœ๊ทธ์ธ ํผ์„ ์ œ๊ณตํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.  

<html>

<body bgcolor="#ffffff">
    <form method="POST" action="j_security_check">
        <table border="0">
            <tr>
                <td>Login</td>
                <td><input type="text" name="j_username"></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="j_password"></td>
            </tr>
        </table> <input type="submit" value="Login!"> </center>
    </form>
</body>

</html>

์œ„ ์ฝ”๋“œ๋ฅผ login.jsp์— ๋„ฃ์–ด์ฃผ์„ธ์š”. <form> ํƒœ๊ทธ ์•ˆ์—์žˆ๋Š” action์˜ ๊ฐ’์€  j_security_check ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. POST ๋ฐฉ์‹์ด ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์•Œ๊ณ  ๊ณ„์‹œ๊ฒ ์ฃ ? ์ž ์ด์ œ web.xml์˜ <login-config> ํƒœ๊ทธ๋ฅผ FORM์„ ์ด์šฉํ•˜๋„๋ก ์ˆ˜์ •ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

<web-app> 
    ... 
    <security-constraint>
        <web-resource-collection>
            <web-resource-name> SecuredBookSite </web-resource-name>
            <url-pattern>/secured/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <description> Let only managers use this app </description>
            <role-name>manager</role-name>
        </auth-constraint>
    </security-constraint>
    <security-role>
        <role-name>manager</role-name>
    </security-role>
    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/login.jsp</form-login-page>
            <form-error-page>/error.jsp</form-error-page>
        </form-login-config>
    </login-config> 
    ...
</web-app>

์ด์ œ  /secured/* ์ฃผ์†Œ๋กœ ์ ‘์†์„ ์‹œ๋„ํ•ด ๋ณด์„ธ์š”. ์‚ฌ์šฉ์ž ID์™€ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜๋ผ๊ณ  ํ• ๊ฒ๋‹ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ๊ฐ€  "j_security_check" ์•ก์…˜์„ ๋งŒ๋‚˜๋ฉด์š”์ฒญ์„ ์ธ์ฆํ•˜๊ธฐ์œ„ํ•œ ๋‚ด๋ถ€ ๋ฉ”์นด๋‹ˆ์ฆ˜์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

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

๋กœ๊ทธ์ธ์ด ์‹คํŒจํ•˜๋ฉด ์„œ๋ฒ„๋Š” ์—๋ŸฌํŽ˜์ด์ง€๋ฅผ ์ „์†กํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.  

 

j_security_check ๊ฐ€ ํ†ฐ์บฃ ์ปจํ…Œ์ด๋„ˆ์—์„œ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ ๋” ์ž์„ธํ•œ ์ •๋ณด๋ฅผ ๋ณด์‹œ๋ ค๋ฉด Standard Realm Implementations ์ด๊ณณ์— ๊ฐ€๋ณด์„ธ์š”. 

 

Servlet/JSP์˜ ์ฝ”๋“œ๋กœ ๋ณด์•ˆ์ ์šฉํ•˜๊ธฐ

HttpServletRequest ๊ฐ์ฒด๊ฐ€ ๋ณด์•ˆ๊ด€๋ จํ•ด์„œ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

SN Method and Description
1 String getAuthType()
The getAuthType() method returns a String object that represents the name of the authentication scheme used to protect the Servlet.
2 boolean isUserInRole(java.lang.String role)
The isUserInRole() method returns a boolean value: true if the user is in the given role or false if they are not.
3 String getProtocol()
The getProtocol() method returns a String object representing the protocol that was used to send the request. This value can be checked to determine if a secure protocol was used.
4 boolean isSecure()
The isSecure() method returns a boolean value representing if the request was made using HTTPS. A value of true means it was and the connection is secure. A value of false means the request was not.
5 Principle getUserPrinciple()
The getUserPrinciple() method returns a java.security.Principle object that contains the name of the current authenticated user.

 

์˜ˆ๋ฅผ๋“ค์–ด ๋งค๋‹ˆ์ € ๊ถŒํ•œ์„ ๊ฐ–๊ณ ์žˆ๋Š” ์‚ฌ์šฉ์ž๋“ค์„ ์œ„ํ•œ ๋งํฌ๋ฅผ ์ œ๊ณตํ•˜๋Š” JavaServer Page ๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•ด์ฃผ์‹œ๋ฉด ๋˜๋Š”๊ฑฐ์ฃ  

<% if (request.isUserInRole("manager")) { %> 
    <a href="managers/mgrreport.jsp">Manager Report</a> 
    <a href="managers/personnel.jsp">Personnel Records</a> 
<% } %>

๊ทธ๋Ÿฌ๋ฉด ์‚ฌ์šฉ์ž ์—ญํ• ์„ ๊ฒ€์‚ฌํ•œ ๋’ค ๋งค๋‹ˆ์ €์—ญํ• ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ๋งŒ ๋งํฌ๋ฅผ ๋ณด์—ฌ์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋กœ๊ทธ์ธ ํผ์—์„œ ์ž…๋ ฅํ•œ ์‚ฌ์šฉ์ž๋ช…์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” getRemoteUser ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

 

Reference : http://www.tutorialspoint.com/jsp/jsp_security.htm 

 

 

 

 

 

 

์˜ค๋Š˜์€ JSP๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํƒ€๋ฒ ์ด์Šค์— ์ ‘์†ํ•˜๊ณ   SELECT, INSERT, DELETE, ๊ทธ๋ฆฌ๊ณ  UPDATE ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์šฐ์„  ๋ฐ์ดํƒ€๋ฒ ์ด์Šค๋ฅผ ์„ค์น˜๊ฐ€๋˜์–ด์žˆ๊ณ  emp๊ณ„์ •์ด ๋งŒ๋“ค์–ด์ ธ ์žˆ์œผ๋ฉฐ Employeesํ…Œ์ด๋ธ”์ด id, first, last, age ์ปฌ๋Ÿผ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. 

 

SELECT Operation:

JTSL์„ ์ด์šฉํ•˜์—ฌ JSP ํŽ˜์ด์ง€๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

<%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html>

<head>
    <title>SELECT Operation</title>
</head>

<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/TEST" user="emp"
        password="emp" />
    <sql:query dataSource="${snapshot}" var="result"> SELECT * from Employees; </sql:query>
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
            <tr>
                <td>
                    <c:out value="${row.id}" />
                </td>
                <td>
                    <c:out value="${row.first}" />
                </td>
                <td>
                    <c:out value="${row.last}" />
                </td>
                <td>
                    <c:out value="${row.age}" />
                </td>
            </tr>
        </c:forEach>
    </table>
</body>

</html>

์œ„ JSPํŽ˜์ด์ง€์— ์ ‘์†ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒ ์ฃ ? 

Emp ID

First Name

Last Name

Age

100

Zara

Ali

18

101

Mahnaz

Fatma

25

102

Zaid

Khan

30

103

Sumit

Mittal

28

 

INSERT Operation:

์—ญ์‹œ๋‚˜ JSTL์„ ์ด์šฉํ•œ JSP ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค. 

<%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html>

<head>
    <title>JINSERT Operation</title>
</head>

<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/TEST" user="emp"
        password="emp" />
    <sql:update dataSource="${snapshot}" var="result"> INSERT INTO Employees VALUES (104, 2, 'Nuha', 'Ali');
    </sql:update>
    <sql:query dataSource="${snapshot}" var="result"> SELECT * from Employees; </sql:query>
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
            <tr>
                <td>
                    <c:out value="${row.id}" />
                </td>
                <td>
                    <c:out value="${row.first}" />
                </td>
                <td>
                    <c:out value="${row.last}" />
                </td>
                <td>
                    <c:out value="${row.age}" />
                </td>
            </tr>
        </c:forEach>
    </table>
</body>

</html>

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ํ•œ์ค„์ด ์ถ”๊ฐ€๊ฐ€ ๋˜์–ด ๋‚˜์˜ค๊ฒ ์ฃ .

Emp ID

First Name

Last Name

Age

100

Zara

Ali

18

101

Mahnaz

Fatma

25

102

Zaid

Khan

30

103

Sumit

Mittal

28

104

Nuha

Ali

2

 

DELETE Operation:

<%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html>

<head>
    <title>DELETE Operation</title>
</head>

<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/TEST" user="emp"
        password="emp" />
    <c:set var="empId" value="103" />
    <sql:update dataSource="${snapshot}" var="count"> DELETE FROM Employees WHERE Id = ?
        <sql:param value="${empId}" />
    </sql:update>
    <sql:query dataSource="${snapshot}" var="result"> SELECT * from Employees; </sql:query>
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
            <tr>
                <td>
                    <c:out value="${row.id}" />
                </td>
                <td>
                    <c:out value="${row.first}" />
                </td>
                <td>
                    <c:out value="${row.last}" />
                </td>
                <td>
                    <c:out value="${row.age}" />
                </td>
            </tr>
        </c:forEach>
    </table>
</body>

</html>

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

Emp ID

First Name

Last Name

Age

100

Zara

Ali

18

101

Mahnaz

Fatma

25

102

Zaid

Khan

30

104

Nuha

Ali

2

 

UPDATE Operation:

<%@ page import="java.io.*,java.util.*,java.sql.*"%> <%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> <html>

<head>
    <title>DELETE Operation</title>
</head>

<body>
    <sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/TEST" user="emp"
        password="emp" />
    <c:set var="empId" value="102" />
    <sql:update dataSource="${snapshot}" var="count"> UPDATE Employees SET last = 'Ali'
        <sql:param value="${empId}" />
    </sql:update>
    <sql:query dataSource="${snapshot}" var="result"> SELECT * from Employees; </sql:query>
    <table border="1" width="100%">
        <tr>
            <th>Emp ID</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
        <c:forEach var="row" items="${result.rows}">
            <tr>
                <td>
                    <c:out value="${row.id}" />
                </td>
                <td>
                    <c:out value="${row.first}" />
                </td>
                <td>
                    <c:out value="${row.last}" />
                </td>
                <td>
                    <c:out value="${row.age}" />
                </td>
            </tr>
        </c:forEach>
    </table>
</body>

</html>

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜ค๊ฒ ์ฃ ?

 

Emp ID

First Name

Last Name

Age

100

Zara

Ali

18

101

Mahnaz

Fatma

25

102

Zaid

Ali

30

104

Nuha

Ali

2

 

 

 

๐Ÿ’ป Programming/JSP

[JSP] JSTL ( JSP Standard Tag Library )

JSTL ๋Š” ์œ ์šฉํ•œ JSP ํƒœ๊ทธ๋“ค์„ ๋ชจ์•„๋†“์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ JSTL tags์™€ ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋„ ์ œ๊ณตํ•˜๋ฏ€๋กœ ์•Œ์•„๋‘์‹œ๋ฉด ์ข‹์„๊ฑฐ์—์š”. 

JSTLํƒœ๊ทธ๋Š” ๊ธฐ๋Šฅ๋ณ„๋กœ ๋ถ„๋ฅ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.  

  • Core Tags

  • Formatting tags

  • SQL tags

  • XML tags

  • JSTL Functions


JSTL ์„ค์น˜ํ•˜๊ธฐ

Apache Tomcat container ๋ฅผ ์‚ฌ์šฉ์ค‘์ด๋ฉด ์•„๋ž˜ ๋‘๊ฐ€์ง€ ์Šคํ…์œผ๋กœ ์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋ฉ๋‹ˆ๋‹ค.

  • Apache Standard Taglib ์ด๊ณณ์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์••์ถ•์„ ํ’€์–ด์ฃผ์„ธ์š”.

  • ์••์ถ•์ด ํ’€๋ฆฐ ๋””๋ ‰ํ† ๋ฆฌ๋‚ด์˜ lib๋””๋ ‰ํ† ๋ฆฌ์˜ JAR ํŒŒ์ผ๋“ค์„ ์—ฌ๋Ÿฌ๋ถ„์ด ์‚ฌ์šฉํ•˜๋ ค๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ webapps\ROOT\WEB-INF\lib ๋””๋ ‰ํ† ๋ฆฌ์•ˆ์— ๋„ฃ์–ด์ฃผ์„ธ์š”. ( ์ด๊ฑฐ๋Š” ์ดํด๋ฆฝ์Šค์—์„œ ์ง์ ‘ ํ•˜์‹ค ์ˆ˜ ์žˆ๋Š”๊ฑด ์•„์‹œ์ฃ ? )  

ํƒœ๊ทธ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด <taglib> ๋””๋ ‰ํ‹ฐ๋ธŒ๋ฅผ JSP ํŽ˜์ด์ง€ ์ œ์ผ ์œ„์ชฝ์— ์„ ์–ธํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.  


Core Tags:

JSTL ํƒœ๊ทธ ์ค‘์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํƒœ๊ทธ์ž…๋‹ˆ๋‹ค.  

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

 

TagDescription
<c:out >Like <%= ... >, but for expressions.
<c:set >Sets the result of an expression evaluation in a 'scope'
<c:remove >Removes a scoped variable (from a particular scope, if specified).
<c:catch>Catches any Throwable that occurs in its body and optionally exposes it.
<c:if>Simple conditional tag which evalutes its body if the supplied condition is true.
<c:choose>Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>
<c:when>Subtag of <choose> that includes its body if its condition evalutes to 'true'.
<c:otherwise >Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'.
<c:import>Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'.
<c:forEach >The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality .
<c:forTokens>Iterates over tokens, separated by the supplied delimeters.
<c:param>Adds a parameter to a containing 'import' tag's URL.
<c:redirect >Redirects to a new URL.
<c:url>Creates a URL with optional query parameters


Formatting tags:

ํฌ๋งคํŒ… ๊ด€๋ จ ํƒœ๊ทธ๋“ค์ž…๋‹ˆ๋‹ค.๋‹ค๊ตญ์  Web ์‚ฌ์ดํŠธ๋ฅผ ๋งŒ๋“ค๋•Œ ์œ ์šฉํ•˜๋‹ค๊ณ  ํ•˜๋„ค์š”.

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

 

TagDescription
<fmt:formatNumber>To render numerical value with specific precision or format.
<fmt:parseNumber>Parses the string representation of a number, currency, or percentage.
<fmt:formatDate>Formats a date and/or time using the supplied styles and pattern
<fmt:parseDate>Parses the string representation of a date and/or time
<fmt:bundle>Loads a resource bundle to be used by its tag body.
<fmt:setLocale>Stores the given locale in the locale configuration variable.
<fmt:setBundle>Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable.
<fmt:timeZone>Specifies the time zone for any time formatting or parsing actions nested in its body.
<fmt:setTimeZone>Stores the given time zone in the time zone configuration variable
<fmt:message>To display an internationalized message.
<fmt:requestEncoding>Sets the request character encoding


SQL tags:

JSTL SQL ํƒœ๊ทธ๋Š” ๊ด€๊ณ„ํ˜• ๋ฐ์ดํƒ€๋ฒ ์ด์Šค ์‹œ์Šคํ…œ์„ ํ•ธ๋“ค๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( Oracle, MySQL, Microsoft SQL Server )

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

 

TagDescription
<sql:setDataSource>Creates a simple DataSource suitable only for prototyping
<sql:query>Executes the SQL query defined in its body or through the sql attribute.
<sql:update>Executes the SQL update defined in its body or through the sql attribute.
<sql:param>Sets a parameter in an SQL statement to the specified value.
<sql:dateParam>Sets a parameter in an SQL statement to the specified java.util.Date value.
<sql:transaction >Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction.


XML tags:

JSTL XML ํƒœ๊ทธ๋Š” JSP์—์„œ XML ๋ฌธ์„œ๋ฅผ ์ƒ์„ฑ ๋ฐ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ XML์„ ํŒŒ์‹ฑํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํƒ€๋ฅผ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ XPath ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ถ”๊ฐ€์ ์ธ ์ปค์Šคํ…€ ํƒœ๊ทธ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.  

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

์•„๋ž˜์˜ ๋‘ jarํŒŒ์ผ๋„ <Tomcat Installation Directory>\lib ์— ์ถ”๊ฐ€ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

 

TagDescription
<x:out>Like <%= ... >, but for XPath expressions.
<x:parse>Use to parse XML data specified either via an attribute or in the tag body.
<x:set >Sets a variable to the value of an XPath expression.
<x:if >Evaluates a test XPath expression and if it is true, it processes its body. If the test condition is false, the body is ignored.
<x:forEach>To loop over nodes in an XML document.
<x:choose>Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>
<x:when >Subtag of <choose> that includes its body if its expression evalutes to 'true'
<x:otherwise >Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'
<x:transform >Applies an XSL transformation on a XML document
<x:param >Use along with the transform tag to set a parameter in the XSLT stylesheet


JSTL Functions:

JSTL ์€ ํ‘œ์ค€ ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„ ์ŠคํŠธ๋ง๊ด€๋ จ ํ•จ์ˆ˜๋“ค์ด์ฃ .

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

 

FunctionDescription
fn:contains()Tests if an input string contains the specified substring.
fn:containsIgnoreCase()Tests if an input string contains the specified substring in a case insensitive way.
fn:endsWith()Tests if an input string ends with the specified suffix.
fn:escapeXml()Escapes characters that could be interpreted as XML markup.
fn:indexOf()Returns the index withing a string of the first occurrence of a specified substring.
fn:join()Joins all elements of an array into a string.
fn:length()Returns the number of items in a collection, or the number of characters in a string.
fn:replace()Returns a string resulting from replacing in an input string all occurrences with a given string.
fn:split()Splits a string into an array of substrings.
fn:startsWith()Tests if an input string starts with the specified prefix.
fn:substring()Returns a subset of a string.
fn:substringAfter()Returns a subset of a string following a specific substring.
fn:substringBefore()Returns a subset of a string before a specific substring.
fn:toLowerCase()Converts all of the characters of a string to lower case.
fn:toUpperCase()Converts all of the characters of a string to upper case.
fn:trim()Removes white spaces from both ends of a string.

 

 

 

Reference : http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm 

 

ํŽ˜์ด์ง€ ๋ฆฌ๋””๋ ‰์…˜์„ ํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ response ๊ฐ์ฒด์˜ sendRedirect() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋ž๋‹ˆ๋‹ค. 

public void response.sendRedirect(String location) throws IOException

์ด ๋ฉ”์†Œ๋“œ๋Š” ์ƒํƒœ์ฝ”๋“œ์™€ ์ƒˆ๋กœ์šด ํŽ˜์ด์ง€ ์ฃผ์†Œ๋ฅผ ํ•จ๊ป˜ response๊ฐ์ฒด์— ๋‹ด์•„์„œ ๋ธŒ๋ผ์šฐ์ €๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.  setStatus() ์™€ setHeader() ๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ธํŒ…์„ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์ฒ˜๋Ÿผ ๋ง์ด์ฃ . 

.... String site = "http://www.newpage.com" ; response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); ....

์˜ˆ์ œ

JSP ๋กœ ์–ด๋–ป๊ฒŒ ํŽ˜์ด์ง€ ๋ฆฌ๋””๋ ‰์…˜์„ ํ•˜๋Š”์ง€ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณผ๊นŒ์š”? 

<%@ page import="java.io.*,java.util.*" %> <html> <head> <title>Page Redirection</title> </head> <body> <center> <h1>Page Redirection</h1> </center> <% // New location to be redirected String site = new String("http://www.naver.com"); response.setStatus(response.SC_MOVED_TEMPORARILY); response.setHeader("Location", site); %> </body> </html>

์œ„ ์ฝ”๋“œ๋ฅผ PageRedirect.jsp ์— ๋„ฃ๊ณ  http://localhost:8080/PageRedirect.jsp ๋ฅผ ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญํ•ด๋ณด์„ธ์š”.   

 

http://www.naver.com ์‚ฌ์ดํŠธ๋กœ ๋ฆฌ๋””๋ ‰์…˜์ด ๋˜๋‚˜์š”?? ^____^ 

 

 

 

Reference : http://www.tutorialspoint.com/jsp/jsp_page_redirect.htm 

 

 

ํŒŒ์ผ ์—…๋กœ๋“œ ํผ ๋งŒ๋“ค๊ธฐ

 

๋‹ค์Œ์— ๋ณผ HTML ์ฝ”๋“œ๊ฐ€ ๋ฐ”๋กœ ํŒŒ์ผ ์—…๋กœ๋“œ ํผ์ž…๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์„ ๋ช‡๊ฐ€์ง€ ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค.

  • <form>์˜ method ์†์„ฑ์€ POST ์ž…๋‹ˆ๋‹ค. 

  • <form>์˜ enctype ์†์„ฑ์€ multipart/form-data ์ž…๋‹ˆ๋‹ค. 

  • <form>์˜ action ์†์„ฑ์€ ๋ฐฑ์—”๋“œ ์ชฝ์—์„œ ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ํ•ธ๋“ค๋งํ•  JSP ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ๋Š” UploadFile.jsp๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

  • ํ•œ๊ฐœ์˜ ํŒŒ์ผ๋งŒ ์—…๋กœ๋“œ ํ•  ๋•Œ๋Š”  <input .../> ํƒœ๊ทธ๋ฅผ type="file" ์†์„ฑ๊ณ  ํ•จ๊ป˜ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฉ€ํ‹ฐํ”Œ ํŒŒ์ผ์—…๋กœ๋”ฉ์„ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด ํ•˜๋‚˜ ์ด์ƒ์˜ inputํƒœ๊ทธ๋ฅผ ๊ฐ๊ธฐ ๋‹ค๋ฅธ name์œผ๋กœ ์„ค์ •ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.  

<html> <head> <title>File Uploading Form</title> </head> <body> <h3>File Upload:</h3> Select a file to upload: <br /> <form action="UploadServlet" method="post" enctype="multipart/form-data"> <input type="file" name="file" size="50" /> <br /> <input type="submit" value="Upload File" /> </form> </body> </html>

์œ„ ์†Œ์Šค๋ฅผ UploadFile.htm์— ๋„ฃ์–ด์ฃผ์„ธ์š”. ์œ„ ํŽ˜์ด์ง€๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜ฌ๊ฑฐ์—์š”. ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•ด๋ณด์„ธ์š” ^___^ ์ฐธ๊ณ ๋กœ ์œ„ ์ฝ”๋“œ๋Š” ๊ทธ๋ƒฅ dummy์ฝ”๋“œ๋ผ์„œ ์‹ค์ œ๋กœ ํŒŒ์ผ์„ ์–ด๋”˜๊ฐ€๋กœ ์—…๋กœ๋“œ ํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. 

 
 

๋ฐฑ์—”๋“œ JSP ์Šคํฌ๋ฆฝํŠธ ๋งŒ๋“ค๊ธฐ

 

์šฐ์„  ํŒŒ์ผ๋“ค์ด ์—…๋กœ๋“œ๋˜์—ˆ์„ ๋•Œ ์–ด๋””์— ์ €์žฅ๋ ์ง€ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด๋ณผ๊ฒŒ์š”. ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์†Œ์Šค์— ํ•˜๋“œ์ฝ”๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ณ  web.xmlํŒŒ์ผ์— context-param ํƒœ๊ทธ๋‚ด์— ์ง€์ •ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

<web-app> .... <context-param> <description>Location to store uploaded file</description> <param-name>file-upload</param-name> <param-value> c:\apache-tomcat-5.5.29\webapps\data\ </param-value> </context-param> .... </web-app>

์•„๋ž˜๋Š” ํ•œ๋ฒˆ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ๋Š” UploadFile.jsp์˜ ์†Œ์Šค์ฝ”๋“œ์—์š”. ์†Œ์Šค๋ถ€ํ„ฐ ๋ณด๊ธฐ์ „์— ์ ๊ฒ€ํ•ด์•ผํ•  ๊ฒƒ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

  • ์•„๋ž˜ ์˜ˆ์ œ๋Š” FileUpload ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์‹ ๋ฒ„์ „์˜ commons-fileupload.x.x.jar ํŒŒ์ผ์„ ํด๋ž˜์ŠคํŒจ์Šค์— ๋„ฃ์–ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ http://commons.apache.org/fileupload/ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์œผ์‹ค ์ˆ˜ ์žˆ์–ด์š”. 

  • ๋˜ํ•œ Commons IO ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.  commons-io-x.x.jar ํŒŒ์ผ ์—ญ์‹œ ํด๋ž˜์Šค ํŒจ์Šค์— ์„ค์ •์„ ํ•ด์ฃผ์…”์•ผ ๋˜์š”. ๋‹ค์šด๋กœ๋“œ๋Š” http://commons.apache.org/io/.

  • ์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ํ…Œ์ŠคํŠธํ•  ๋•Œ์—๋Š” maxFileSize ์— ์„ค์ •๋œ ํŒŒ์ผํฌ๊ธฐ๋ณด๋‹ค ์ž‘์€ ํŒŒ์ผ๋“ค๋งŒ ์—…๋กœ๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ต๋‹ˆ๋‹ค.

  • c:\temp ์™€ c:\apache-tomcat-5.5.29\webapps\data ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์‹œ๊ณ  ์—†์œผ๋ฉด ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”. 
     

<%@ page import="java.io.*,java.util.*, javax.servlet.*" %> <%@ page import="javax.servlet.http.*" %> <%@ page import="org.apache.commons.fileupload.*" %> <%@ page import="org.apache.commons.fileupload.disk.*" %> <%@ page import="org.apache.commons.fileupload.servlet.*" %> <%@ page import="org.apache.commons.io.output.*" %> <% File file ; int maxFileSize = 5000 * 1024; int maxMemSize = 5000 * 1024; ServletContext context = pageContext.getServletContext(); String filePath = context.getInitParameter("file-upload"); // Verify the content type String contentType = request.getContentType(); if ((contentType.indexOf("multipart/form-data") >= 0)) { DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory factory.setSizeThreshold(maxMemSize); // Location to save data that is larger than maxMemSize. factory.setRepository(new File("c:\\temp")); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // maximum file size to be uploaded. upload.setSizeMax( maxFileSize ); try{ // Parse the request to get file items. List fileItems = upload.parseRequest(request); // Process the uploaded file items Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>JSP File upload</title>"); out.println("</head>"); out.println("<body>"); while ( i.hasNext () ) { FileItem fi = (FileItem)i.next(); if ( !fi.isFormField () ) { // Get the uploaded file parameters String fieldName = fi.getFieldName(); String fileName = fi.getName(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); // Write the file if( fileName.lastIndexOf("\\") >= 0 ){ file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))) ; }else{ file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ; } fi.write( file ) ; out.println("Uploaded Filename: " + filePath + fileName + "<br>"); } } out.println("</body>"); out.println("</html>"); }catch(Exception ex) { System.out.println(ex); } }else{ out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No file uploaded</p>"); out.println("</body>"); out.println("</html>"); } %>

์ด์ œ http://localhost:8080/UploadFile.htm ์— ์ ‘์†ํ•ด์„œ ํ…Œ์ŠคํŠธ ํ•ด๋ณผ๊นŒ์š”? 

 

์ œ๋Œ€๋กœ ์ž‘๋™ํ•œ๋‹ค๋ฉด  c:\apache-tomcat-5.5.29\webapps\data\ ๋””๋ ‰ํ† ๋ฆฌ์— ํŒŒ์ผ์ด ์—…๋กœ๋“œ ๋˜์–ด์žˆ์„ ๊ฑฐ์—์š”~ 

 

 

 

Reference : http://www.tutorialspoint.com/jsp/jsp_file_uploading.htm 

 

 

๐Ÿ’ป Programming/JSP

[JSP] Session ( ์„ธ์…˜ )

์ด๋ฒˆ์—๋Š” JSP๋กœ ์„ธ์…˜์„ ๋‹ค๋ฃจ๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋ฐฐ์›Œ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. HTTP๋Š” "stateless" ํ”„๋กœํ† ์ฝœ์ž…๋‹ˆ๋‹ค.  ๋ฌด์Šจ ๋ง์ด๋ƒํ•˜๋ฉด ๋ง์ด์ฃ , ํด์•„์ด์–ธํŠธ(์›น๋ธŒ๋ผ์šฐ์ €)๊ฐ€ ์›นํŽ˜์ด์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ๋•Œ๋งˆ๋‹ค ํด๋ผ์ด์–ธํŠธ๋Š” ์›น์„œ๋ฒ„๋กœ ๋ณ„๋„์˜ ์ปค๋„ฅ์…˜์„ ๋งบ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์ „ ํด๋ผ์ด์–ธํŠธ์˜ ์ ‘์†์— ๋Œ€ํ•œ ์–ด๋– ํ•œ ์ •๋ณด๋„ ์ž๋™์œผ๋กœ ๊ธฐ๋กํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. 

์–ด์จŒ๋“  ์„ธ์…˜์ด๋ผ๋Š” ๊ฒƒ์€ ๋ฐ”๋กœ ์›น๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์›น์„œ๋ฒ„์™€ ์—ฐ๊ฒฐ์ด ๋ ๋•Œ ์ƒ๊ธฐ๋Š” ๊ฒƒ์ธ๋ฐ, ์ด ์„ธ์…˜์„ ์œ ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ต๋‹ˆ๋‹ค.  

1. Cookies

์ฟ ํ‚ค์— ์„ธ์…˜ID๋ฅผ ์ €์žฅํ•˜๋Š” ์‹์œผ๋กœ ์„ธ์…˜์„ ๊ด€๋ฆฌํ•  ์ˆ˜๋Š” ์žˆ๊ฒ ์ง€๋งŒ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ฟ ํ‚ค์‚ฌ์šฉ ์ œํ•œ ์„ค์ •์ด ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ๋•Œ๋ฌธ์— ์„ธ์…˜์„ ์ฟ ํ‚ค๋ฅผ ์ด์šฉํ•ด์„œ ๊ด€๋ฆฌํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์ข‹์€๋ฐฉ๋ฒ•์ด ์•„๋‹™๋‹ˆ๋‹ค.

 

 

2. Hidden Form Fields

 

์›น์„œ๋ฒ„๋Š” hidden HTML form field์— unique session ID๋ฅผ ๋„ฃ์–ด์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ์ „์†กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<input type="hidden" name="sessionid" value="12345">

form์ด ์ „์†ก๋  ๋•Œ ์„ธ์…˜ID์™€ ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋Š”๊ฑฐ์ฃ . ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ด ์ •๋ณด๋ฅผ ์›น์„œ๋ฒ„์— ๋ณด๋‚ด๋ฉด ์ด ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์›น์„œ๋ฒ„์—์„œ๋Š” ๋‹ค๋ฅธ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ ‘์†ํ•œ ๊ฒƒ์ธ์ง€๋ฅผ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ์—ญ์‹œ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ์ฃ . <a> ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด์„œ ๋งํฌ๋ฅผ ํƒ€๊ณ  ๋“ค์–ด์˜ค๋Š” ๊ฒฝ์šฐ์—๋Š” ์ € form๋ฐ์ดํƒ€๋ฅผ ์ „์†กํ•˜์ง€ ์•Š๊ฑฐ๋“ ์š”.

3. URL Rewriting

URL ๋์— ์„ธ์…˜์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์„œ๋ฒ„๋Š” ๊ทธ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์„ธ์…˜์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๊ฒ ์ฃ .

์˜ˆ๋ฅผ๋“ค๋ฉด, http://tutorialspoint.com/file.htm;sessionid=12345 ์ด๋ ‡๊ฒŒ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ์„œ๋ฒ„์—์„œ sessionid=12345๋ถ€๋ถ„์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์›น์„œ๋ฒ„์˜ ์„ธ์…˜์ •๋ณด์™€ ๋น„๊ต๋ฅผ ํ•˜๋Š”๊ฑฐ์ฃ .

URL rewriting ์€ ์„ธ์…˜์„ ๊ด€๋ฆฌํ•˜๊ธฐ์œ„ํ•œ ๋ฐฉ๋ฒ•๋“ค ์ค‘์—์„œ๋Š” ๊ทธ๋‚˜๋งˆ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๊ธด ํ•ฉ๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ฟ ํ‚ค์‚ฌ์šฉ์„ ์•ˆํ• ๋•Œ๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๋ฐฉ๋ฒ•์ด๋‹ˆ๊นŒ์š”. ํ•˜์ง€๋งŒ ์—ญ์‹œ ๊ฒฐ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธ์…˜ID๋ฅผ ๋™์ ์œผ๋กœ ๋งค๋ฒˆ ์ƒ์„ฑํ•ด์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ •์ ์ธ HTMLํŽ˜์ด์ง€๋ผ๋„ ๋ง์ด์ฃ .  


The session Object:

์œ„ ์„ธ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ด์™ธ์—๋„ JSP๋Š” HttpSession ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ JSPs๋Š” ์„ธ์…˜์„ ์ถ”์ ํ•˜๊ฒŒ ๋˜์–ด์žˆ์œผ๋ฉฐ ์ƒˆ๋กœ์šด HttpSession ๊ฐ์ฒด๊ฐ€ ์ƒˆ๋กœ์šด ํด๋ผ์ด์–ธํŠธ๋“ค์ด ์ ‘์†ํ•  ๋•Œ๋งˆ๋‹ค ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์„ธ์…˜ ์ถ”์  ๊ธฐ๋Šฅ์„ ๋„๋ ค๋ฉด page directive์˜ ์„ธ์…˜ ์†์„ฑ์„ false๋กœ ์„ธํŒ…ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฒ˜๋Ÿผ ๋ง์ด์ฃ .

<%@ page session="false" %>

JSP engine์€ JSP๊ฐœ๋ฐœ์ž๋“ค์—๊ฒŒ HttpSession ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด์„œ session์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. session ๊ฐ์ฒด๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœ์ž๋“ค์€ ๊ถ‚์ด ์„ธ์…˜๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ getSession() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์–ป์–ด์˜ฌ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

์›น์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ž‘์„ฑํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ๊ทธ๋ƒฅ ์„ธ์…˜์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์„œ ์“ฐ๋ฉด ๋˜๋Š” ๊ฒ๋‹ˆ๋‹ค. 

 

๋‹ค์Œ์€ ์„ธ์…˜ ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ๋ฉ”์†Œ๋“œ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. ์–ด๋–ค ๋ฉ”์†Œ๋“œ๋“ค์ด ์žˆ๋Š”์ง€ ํ•œ๋ฒˆ ์‚ดํŽด๋ณผ๊นŒ์š”~

 

S.N.Method & Description
1public Object getAttribute(String name)
This method returns the object bound with the specified name in this session, or null if no object is bound under the name.
2public Enumeration getAttributeNames()
This method returns an Enumeration of String objects containing the names of all the objects bound to this session.
3public long getCreationTime()
This method returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.
4public String getId()
This method returns a string containing the unique identifier assigned to this session.
5public long getLastAccessedTime()
This method returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT.
6public int getMaxInactiveInterval()
This method returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses.
7public void invalidate()
This method invalidates this session and unbinds any objects bound to it.
8public boolean isNew(
This method returns true if the client does not yet know about the session or if the client chooses not to join the session.
9public void removeAttribute(String name)
This method removes the object bound with the specified name from this session.
10public void setAttribute(String name, Object value) 
This method binds an object to this session, using the name specified.
11public void setMaxInactiveInterval(int interval)
This method specifies the time, in seconds, between client requests before the servlet container will invalidate this session.


Session Tracking Example:

๋‹ค์Œ ์˜ˆ์ œ๋Š” HttpSession ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ธ์…˜์˜ ์ƒ์„ฑ์‹œ๊ฐ„๊ณผ ๋งˆ์ง€๋ง‰ ์ ‘๊ทผ์‹œ๊ฐ„ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด์ฃผ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.  

<%@ page import="java.io.*,java.util.*" %> <% // Get session creation time. Date createTime = new Date(session.getCreationTime()); // Get last access time of this web page. Date lastAccessTime = new Date(session.getLastAccessedTime()); String title = "Welcome Back to my website"; Integer visitCount = new Integer(0); String visitCountKey = new String("visitCount"); String userIDKey = new String("userID"); String userID = new String("ABCD"); // ์ฒ˜์Œ ์ ‘์†ํ•œ ๋ฐฉ๋ฌธ์ž๋ผ๋ฉด ์„ธ์…˜์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. if (session.isNew()){ title = "Welcome to my website"; session.setAttribute(userIDKey, userID); session.setAttribute(visitCountKey, visitCount); } visitCount = (Integer)session.getAttribute(visitCountKey); visitCount = visitCount + 1; userID = (String)session.getAttribute(userIDKey); session.setAttribute(visitCountKey, visitCount); %> <html> <head> <title>Session Tracking</title> </head> <body> <center> <h1>Session Tracking</h1> </center> <table border="1" align="center"> <tr bgcolor="#949494"> <th>Session info</th> <th>Value</th> </tr> <tr> <td>id</td> <td><% out.print( session.getId()); %></td> </tr> <tr> <td>Creation Time</td> <td><% out.print(createTime); %></td> </tr> <tr> <td>Time of Last Access</td> <td><% out.print(lastAccessTime); %></td> </tr> <tr> <td>User ID</td> <td><% out.print(userID); %></td> </tr> <tr> <td>Number of visits</td> <td><% out.print(visitCount); %></td> </tr> </table> </body> </html>

 main.jspโ€‹์— ์œ„ ์ฝ”๋“œ๋ฅผ ๋„ฃ๊ณ  http://localhost:8080/main.jsp ๋ฅผ ํ˜ธ์ถœํ•ด๋ณด์„ธ์š”. ์„œ๋ฒ„ ์‹คํ–‰์‹œํ‚ค๋Š” ๊ฒƒ ์žŠ์–ด๋จน์ง€ ๋งˆ์‹œ๊ตฌ์š”~ ^___^ 

Welcome to my website

Session Infomation

Session infovalue
id0AE3EC93FF44E3C525B4351B77ABB2D5
Creation TimeTue Jun 08 17:26:40 GMT+04:00 2010
Time of Last AccessTue Jun 08 17:26:40 GMT+04:00 2010
User IDABCD
Number of visits0

 

 

์œ„์™€๊ฐ™์€ ํŽ˜์ด์ง€๊ฐ€ ๋‚˜์˜ค๋‚˜์š”??

 

๋‹ค์‹œํ•œ๋ฒˆ ํ˜ธ์ถœ ํ•ด ๋ณผ๊นŒ์š”??

 

Welcome Back to my website

Session Infomation

info typevalue
id0AE3EC93FF44E3C525B4351B77ABB2D5
Creation TimeTue Jun 08 17:26:40 GMT+04:00 2010
Time of Last AccessTue Jun 08 17:26:40 GMT+04:00 2010
User IDABCD
Number of visits1


์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋‚˜์š”? Welcome ๋ฉ”์‹œ์ง€๊ฐ€ Welcome Back ๋ฉ”์‹œ์ง€๋กœ ๋ฐ”๋€Œ์—ˆ๋„ค์š”. ^__^ 

 

 

์ด์ œ ์„ธ์…˜ ๋ฐ์ดํƒ€๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณผ๊นŒ์š”??โ€‹โ€‹

 

Deleting Session Data:

์‚ฌ์šฉ์ž์˜ ์„ธ์…˜ ๋ฐ์ดํƒ€๋กœ ํ•ด์•ผ๋˜๋Š” ์ž‘์—…์„ ๋ชจ๋‘ ์™„๋ฃŒํ•˜์˜€๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ž‘์—…์„ ๋งˆ์ง€๋ง‰์œผ๋กœ ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ํŠน์ • ์†์„ฑ ์‚ญ์ œ : removeAttribute(String name) ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์„ธ์…˜์˜ ํŠน์ • ์†์„ฑ๊ฐ’๋งŒ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

  • ์„ธ์…˜ ์‚ญ์ œ : invalidate() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์„ธ์…˜ ์ „์ฒด๋ฅผ ๋ฌดํšจํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธ์…˜์„ ๋Š๋Š”๋‹ค๊ณ  ํ‘œํ˜„ํ•˜์ฃ . 

  • ์„ธ์…˜ ํƒ€์ž„์•„์›ƒ ์„ค์ • : setMaxInactiveInterval(int interval) ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ธ์…˜๋ณ„๋กœ ํƒ€์ž„์•„์›ƒ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์‚ฌ์šฉ์ž ๋กœ๊ทธ์•„์›ƒ : servlets 2.4๋ฅผ ์ง€์›ํ•˜๋Š” ์„œ๋ฒ„๋ผ๋ฉด logout ์„ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉ์ž๋ฅผ ๋กœ๊ทธ์•„์›ƒ์‹œํ‚ค๊ณ  ๋ชจ๋“  ์„ธ์…˜์„ ๋ฌดํšจํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  

  • web.xml ์„ค์ • : Tomcat์„ ์‚ฌ์šฉ์ค‘์ด๋ผ๋ฉด web.xml ํŒŒ์ผ์—์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ํƒ€์ž„์•„์›ƒ ์‹œ๊ฐ„์„ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

<session-config> <session-timeout>15</session-timeout> </session-config>

์—ฌ๊ธฐ์„œ ํƒ€์ž„์•„์›ƒ ์‹œ๊ฐ„์˜ ๋‹จ์œ„๋Š” ๋ถ„ ๋‹จ์œ„์ด๋ฉฐ ํ†ฐ์บฃ์˜ ๊ธฐ๋ณธ ํƒ€์ž„์•„์›ƒ์ธ 30 ๋ถ„์„ ์˜ค๋ฒ„๋ผ์ด๋“œํ•˜์—ฌ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

์„œ๋ธ”๋ฆฟ์˜ getMaxInactiveInterval( )๋ฉ”์†Œ๋“œ๋Š” ์ดˆ๋‹จ์œ„๋กœ ํƒ€์ž„์•„์›ƒ ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ web.xml ํŒŒ์ผ์— 15 ๋ถ„์œผ๋กœ ์„ค์ •๋˜์–ด์žˆ๋‹ค๋ฉด  getMaxInactiveInterval( ) ๋ฉ”์†Œ๋“œ๋Š” 900 ( 15๋ถ„ * 60์ดˆ )๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค .

 

 

 

 

Reference : http://www.tutorialspoint.com/jsp/jsp_session_tracking.htm 

 

Cookies ๋Š” ๊ทธ๋ƒฅ ์ผ๋ฐ˜ text files ์ด์ฃ . ์•„๋งˆ JSP๋ฅผ ๊ณต๋ถ€ํ•˜์‹œ๋Š” ๋ถ„๋“ค์ด๋ผ๋ฉด ์ด์ •๋„๋Š” ์•Œ๊ณ  ๊ณ„์‹œ๊ฒ ์ฃ ? 

JSP๋กœ HTTP cookies ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ํ•œ๋ฒˆ ์•Œ์•„๋ณผํ…๋ฐ ๊ทธ ์ „์— ํ•˜๋‚˜๋งŒ ์งš๊ณ  ๋„˜์–ด๊ฐˆ๊นŒ์š”?

์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • ์‚ฌ์ดํŠธ์— ์žฌ์ ‘์† ํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ์›น์‚ฌ์ดํŠธ์—์„œ ์–ด๋–ป๊ฒŒ ์•Œ๊นŒ์š”?

3์ดˆ๋งŒ์— ๋‹ต์ด ์•ˆ๋‚˜์˜จ๋‹ค๋ฉด ์•„๋ž˜๋ฅผ ์ฝ์–ด๋ด…์‹œ๋‹ค.

 

์žฌ์ ‘์†ํ•˜๋Š” ์‚ฌ์šฉ์ž๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ณผ์ •์€ ์„ธ ๋‹จ๊ณ„๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.

  • ์„œ๋ฒ„์ชฝ์—์„œ ์ฟ ํ‚ค๋ฅผ ๋ธŒ๋ผ์šฐ์ €๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค.  

  • Browser๋Š” ์„œ๋ฒ„์—์„œ ๋ณด๋‚ธ ์ฟ ํ‚ค๋ฅผ ๋กœ์ปฌ์— ์ €์žฅ์„ ํ•˜๊ฒ ์ฃ .

  • ์ด์ œ ์‚ฌ์šฉ์ž๊ฐ€ ์ด ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ด์šฉํ•ด์„œ ํŠน์ •์‚ฌ์ดํŠธ์— ์ ‘์†ํ•  ๋•Œ ์ €์žฅ๋˜์–ด์žˆ๋Š” ์ฟ ํ‚ค์˜ ์ •๋ณด๊ฐ€ ์„œ๋ฒ„์ชฝ์œผ๋กœ ์ „๋‹ฌ๋˜๊ณ  ์„œ๋ฒ„์ชฝ์—์„œ๋Š” ์ด ์ •๋ณด๋ฅผ ์ด์šฉํ•ด์„œ ์‚ฌ์šฉ์ž๋ฅผ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์ฃ .  

์ฟ ํ‚ค๋Š” ๋‹ค๋ฅธ ์ •๋ณด๋กœ๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ฟ ํ‚ค๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฒ•์„ ํ•œ๋ฒˆ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ฟ ํ‚ค ์„ธํŒ…, ๋ฆฌ์…‹, ์ฝ๊ธฐ, ์‚ญ์ œํ•˜๊ธฐ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.  


Cookie ํ•ด๋ถ€ํ•˜๊ธฐ

 ์ฟ ํ‚ค๋Š” ๋ณดํ†ต HTTP header ์•ˆ์— ์„ธํŒ…๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ( JavaScript ๋Š” ์ง์ ‘ ๋ธŒ๋ผ์šฐ์ €์— ์ฟ ํ‚ค๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๋„ค์š” ). ์ฟ ํ‚ค๋ฅผ ์„ธํŒ…ํ•˜๋Š” JSP ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํ—ค๋”๋ฅผ ๋ณด๋‚ผ๊ฑฐ์—์š”. 

HTTP/1.1 200 OK Date: Fri, 04 Feb 2000 21:03:38 GMT Server: Apache/1.3.9 (UNIX) PHP/4.0b3 Set-Cookie: name=xyz; expires=Friday, 04-Feb-07 22:03:38 GMT; path=/; domain=tutorialspoint.com Connection: close Content-Type: text/html

์ด๋ฏธ request ๊ฐ์ฒด์‹œ๊ฐ„์— ํ•œ๋ฒˆ ๋ณธ์ ์ด ์žˆ๋˜ ๋‚ด์šฉ์ด์ฃ ? ๊ทธ๋Ÿฐ๋ฐ ์ถ”๊ฐ€๋œ๊ฒŒ ํ•˜๋‚˜ ์žˆ๋„ค์š”. Set-Cookie header ์ž…๋‹ˆ๋‹ค. ๋ณด์•„ํ•˜๋‹ˆ ์ด๋ฆ„, ๋งŒ๋ฃŒ์ผ์‹œ, ๊ฒฝ๋กœ, ๊ทธ๋ฆฌ๊ณ  ๋„๋ฉ”์ธ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋„ค์š”.  

๋ธŒ๋ผ์šฐ์ €์—์„œ ์ฟ ํ‚ค๋ฅผ ์ €์žฅํ•˜๋„๋ก ์„ค์ •์ด ๋˜์–ด์žˆ์œผ๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด๋Ÿฐ ์ •๋ณด๋“ค์„ ๋งŒ๋ฃŒ์ผ ์ „๊นŒ์ง€ ์ €์žฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.๋งŒ์•ฝ ์‚ฌ์šฉ์ž๊ฐ€ ์ฟ ํ‚ค์— ์„ค์ •๋œ ๊ฒฝ๋กœ๋กœ ์ ‘์†์„ ํ•˜๊ฒŒ๋˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ์„œ๋ฒ„๋กœ ๊ทธ ์ฟ ํ‚ค๋ฅผ ์žฌ์ „์†กํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๋ธŒ๋ผ์šฐ์ €์˜ ํ—ค๋”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. 

GET / HTTP/1.0 Connection: Keep-Alive User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc) Host: zink.demon.co.uk:1126 Accept: image/gif, */* Accept-Encoding: gzip Accept-Language: en Accept-Charset: iso-8859-1,*,utf-8 Cookie: name=xyz

์ด์ œ JSP script ๋Š” request method์ค‘ ํ•˜๋‚˜์ธ request.getCookies() ๋ฅผ ํ†ตํ•ด์„œ ์ฟ ํ‚ค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

Servlet Cookies ๋ฉ”์†Œ๋“œ

์•„๋ž˜ ๋ฉ”์†Œ๋“œ๋“ค์€ JSP์—์„œ ์ฟ ํ‚ค๋ฅผ ๋‹ค๋ฃฐ๋•Œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ชฉ๋ก ๋ฐ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

 

S.N.Method & Description
1public void setDomain(String pattern)
This method sets the domain to which cookie applies, for example tutorialspoint.com.
2public String getDomain()
This method gets the domain to which cookie applies, for example tutorialspoint.com.
3public void setMaxAge(int expiry)
This method sets how much time (in seconds) should elapse before the cookie expires. If you don't set this, the cookie will last only for the current session.
4public int getMaxAge()
This method returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist until browser shutdown.
5public String getName()
This method returns the name of the cookie. The name cannot be changed after creation.
6public void setValue(String newValue)
This method sets the value associated with the cookie.
7public String getValue()
This method gets the value associated with the cookie.
8public void setPath(String uri)
This method sets the path to which this cookie applies. If you don't specify a path, the cookie is returned for all URLs in the same directory as the current page as well as all subdirectories.
9public String getPath()
This method gets the path to which this cookie applies.
10public void setSecure(boolean flag)
This method sets the boolean value indicating whether the cookie should only be sent over encrypted (i.e. SSL) connections.
11public void setComment(String purpose)
This method specifies a comment that describes a cookie's purpose. The comment is useful if the browser presents the cookie to the user.
12public String getComment()
This method returns the comment describing the purpose of this cookie, or null if the cookie has no comment.


 JSP๋กœ ์ฟ ํ‚ค ์„ค์ •ํ•˜๊ธฐ

JSP๋กœ ์ฟ ํ‚ค๋ฅผ ์„ค์ •ํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์นฉ๋‹ˆ๋‹ค.

 

(1) Cookie ๊ฐ์ฒด ์ƒ์„ฑ : You call the Cookie constructor with a cookie name and a cookie value, both of which are strings.

Cookie cookie = new Cookie("key","value");

์ž๋ฐ”์—์„œ ์ฟ ํ‚ค ์ƒ์„ฑํ•˜๋Š”๊ฑฐ๋ž‘ ๋˜‘๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ name๊ณผ value ( "key"๊ฐ€ name์ธ๊ฑฐ๊ณ  "value"๊ฐ€ value๊ฐ€ ๋˜๊ฒ ์ฃ )์—๋Š” ์ ˆ๋Œ€๋กœ ์ŠคํŽ˜์ด์Šค(๋นˆ์นธ)์ด๋‚˜ ์•„๋ž˜์— ๋‚˜์—ด๋œ ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ์‚ฌ์šฉ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.  

[ ] ( ) = , " / ? @ : ;

(2) Max age ์„ค์ • : ์ดˆ ๋‹จ์œ„๋กœ max age๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ฟ ํ‚ค๊ฐ€ ์‚ด์•„์žˆ์„ ์ˆ˜ ์žˆ๋Š” ์‹œ๊ฐ„์„ ์„ค์ •ํ•˜๋Š”๊ฑฐ์ฃ . ์•„๋ž˜๋Š” 24 ์‹œ๊ฐ„๋™์•ˆ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋Š” ์ฟ ํ‚ค๋ฅผ ์„ค์ •ํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

cookie.setMaxAge(60*60*24);

(3) Cookie๋ฅผ HTTP response header์— ์ถ”๊ฐ€ํ•˜๊ธฐ : response.addCookie ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ถ”๊ฐ€ํ•˜ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.  

response.addCookie(cookie);

์˜ˆ์ œ

<% // Create cookies for first and last names. Cookie firstName = new Cookie("first_name", request.getParameter("first_name")); Cookie lastName = new Cookie("last_name", request.getParameter("last_name")); // Set expiry date after 24 Hrs for both the cookies. firstName.setMaxAge(60*60*24); lastName.setMaxAge(60*60*24); // Add both the cookies in the response header. response.addCookie( firstName ); response.addCookie( lastName ); %> <html> <head> <title>Setting Cookies</title> </head> <body> <center> <h1>Setting Cookies</h1> </center> <ul> <li><p><b>First Name:</b> <%= request.getParameter("first_name")%> </p></li> <li><p><b>Last Name:</b> <%= request.getParameter("last_name")%> </p></li> </ul> </body> </html>

 main.jsp ํŒŒ์ผ์— ์œ„ ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ์„ธ์š”. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ ์ฝ”๋“œ๋Š” hello.jspํŒŒ์ผ์— ๋„ฃ์–ด์ฃผ์„ธ์š”. 

<html> <body> <form action="main.jsp" method="GET"> First Name: <input type="text" name="first_name"> <br /> Last Name: <input type="text" name="last_name" /> <input type="submit" value="Submit" /> </form> </body> </html>

๋‘ jspํŒŒ์ผ์„ <Tomcat-installation-directory>/webapps/ROOT ๋””๋ ‰ํ† ๋ฆฌ์— ๋„ฃ์–ด์ฃผ์„ธ์š”.

์ด์ œ http://localhost:8080/hello.jsp ์— ์ ‘์†์„ ํ•ด๋ณด์„ธ์š”.

 

 

 

์ด๋Ÿฐ๊ฒŒ ๋‚˜์˜ค๋‚˜์š”? ์ด์ œ ์ด๋ฆ„์„ ๋„ฃ๊ณ  submit๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ๋ณด์„ธ์š”. 

๊ทธ๋Ÿผ ์˜ˆ์ „์— ๋ดค๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ํผ์ŠคํŠธ ๋„ค์ž„๊ณผ ๋ž˜์ŠคํŠธ ๋„ค์ž„์ด ์ถœ๋ ฅ์ด ๋ ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ์ด๋…€์„์ด ๋ชฐ๋ž˜ํ•˜๋Š” ์ž‘์—…์ด ๋˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ˆˆ์—๋Š” ๋ณด์ด์ง€ ์•Š์•˜์ง€๋งŒ first_name๊ณผ last_name์ด๋ผ๋Š” ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•œ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  submit๋ฒ„ํŠผ์ด ๋˜ ๋ˆŒ๋ฆฌ๊ฒŒ ๋  ๊ฒฝ์šฐ ๋ฐฉ๊ธˆ ๋งŒ๋“ค์–ด์ง„ ์ฟ ํ‚ค๋“ค์ด ์„œ๋ฒ„์ชฝ์œผ๋กœ ์ „์†ก๋˜๊ฒŒ ๋˜๊ฒ ์ฃ . 

์ž, ๊ทธ๋Ÿผ ์ด์ œ ์„œ๋ฒ„์ชฝ์œผ๋กœ ์ „์†ก๋˜๋Š” ์ฟ ํ‚ค์ •๋ณด๋ฅผ JSP์—์„œ ์ฝ์–ด์˜ค๋Š” ๊ฒƒ์„ ํ•œ๋ฒˆ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 


JSP๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฟ ํ‚ค์ •๋ณด ์ฝ์–ด์˜ค๊ธฐ

์ฟ ํ‚ค๋ฅผ ์ฝ์œผ๋ ค๋ฉด HttpServletRequest ์˜ getCookies( ) ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์†Œ๋“œ๋Š” Cookie[]๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ํ•œ๋ฒˆ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ฟ ํ‚ค๊ฐ€ ์ „์†ก๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด์ฃ . ๊ทธ๋Ÿผ ๋ฃจํ”„๋ฅผ ๋Œ๋ฉด์„œ ์ „์†ก๋œ ์ฟ ํ‚ค์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.  


์˜ˆ์ œ

<html> <head> <title>Reading Cookies</title> </head> <body> <center> <h1>Reading Cookies</h1> </center> <% Cookie cookie = null; Cookie[] cookies = null; // Get an array of Cookies associated with this domain cookies = request.getCookies(); if( cookies != null ){ out.println("<h2> Found Cookies Name and Value</h2>"); for (int i = 0; i < cookies.length; i++){ cookie = cookies[i]; out.print("Name : " + cookie.getName( ) + ", "); out.print("Value: " + cookie.getValue( )+" <br/>"); } }else{ out.println("<h2>No cookies founds</h2>"); } %> </body> </html>

์œ„ ์ฝ”๋“œ๋ฅผ main.jsp ํŒŒ์ผ์— ๋„ฃ๊ณ  ์ ‘์†ํ•ด๋ณด์„ธ์š”. ์ฟ ํ‚ค์— ์„ค์ •ํ•œ ๊ฐ’๋“ค์ด ๋‚˜์˜ค๊ฒ ์ฃ ? ์•„๋ž˜์ฒ˜๋Ÿผ ๋ง์ด์ฃ .


Found Cookies Name and Value

Name : first_name, Value: ํ™๊ธธ๋™์€
Name : last_name, Value: ํ”Œ๋ ˆ์ด๋ณด์ด


JSP๋ฅผ ์ด์šฉํ•œ ์ฟ ํ‚ค ์‚ญ์ œ

์ด๋ฒˆ์—๋Š” ์ฟ ํ‚ค๋ฅผ ์‚ญ์ œํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ญ ์ฟ ํ‚ค์‚ญ์ œ๋Š” ๋ธŒ๋ผ์šฐ์ €์—์„œ๋„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด๊ธด ํ•˜์ง€๋งŒ ๊ทธ๋ž˜๋„ ํ•œ๋ฒˆ ์ฝ์–ด๋‚˜ ๋ณด์„ธ์š”. ์›น๊ฐœ๋ฐœ์ž๋ผ๋ฉด ์ฟ ํ‚ค ๋‹ค๋ฃจ๋Š” ๋ฒ•์€ ์ž˜ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•  ํ…Œ๋‹ˆ๊นŒ์š”. 

  • ์ฟ ํ‚ค๋ฅผ ์ฝ์–ด์™€์„œ ์ฟ ํ‚ค๊ฐ์ฒด์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

  • ์ฟ ํ‚ค์˜ ๋‚˜์ด๋ฅผ 0์œผ๋กœ ๋งŒ๋“ค์–ด๋ฒ„๋ฆฌ์„ธ์š”. setMaxAge() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”๊ฑด ์•„์‹œ์ฃ ?

  • ์ด ์ฟ ํ‚ค๋ฅผ ๋‹ค์‹œ response์— ๋„ฃ์–ด ๋˜๋Œ๋ ค๋ณด๋‚ด์„ธ์š”.


์˜ˆ์ œ

์•„๋ž˜ ์˜ˆ์ œ๋Š” "first_name" ์ฟ ํ‚ค๋ฅผ ์‚ญ์ œํ•˜๋Š” main.jsp ์†Œ์Šค์ž…๋‹ˆ๋‹ค. ์‚ญ์ œ๋œ ํ›„์— ์ด ์ฟ ํ‚ค๋ฅผ ๋‹ค์‹œ ์ฝ์–ด์˜ค๋ ค๊ณ ํ•˜๋ฉด null์ด ๋‚˜์˜ฌ๊ฒ๋‹ˆ๋‹ค.

<html> <head> <title>Reading Cookies</title> </head> <body> <center> <h1>Reading Cookies</h1> </center> <% Cookie cookie = null; Cookie[] cookies = null; // Get an array of Cookies associated with this domain cookies = request.getCookies(); if( cookies != null ){ out.println("<h2> Found Cookies Name and Value</h2>"); for (int i = 0; i < cookies.length; i++){ cookie = cookies[i]; if((cookie.getName( )).compareTo("first_name") == 0 ){ cookie.setMaxAge(0); response.addCookie(cookie); out.print("Deleted cookie: " + cookie.getName( ) + "<br/>"); } out.print("Name : " + cookie.getName( ) + ", "); out.print("Value: " + cookie.getValue( )+" <br/>"); } }else{ out.println( "<h2>No cookies founds</h2>"); } %> </body> </html>

์ด์ œ main.jsp๋ฅผ ์š”์ฒญํ•ด๋ณด์„ธ์š”. http://localhost:8080/main.jsp ์ฃผ์†Œ๋กœ ์š”์ฒญํ•˜๋ฉด ๋˜๊ฒ ์ฃ ? ๊ทธ๋Ÿผ ์•„๋ž˜์ฒ˜๋Ÿผ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ ๊ฑฐ์—์š”.


Cookies Name and Value

Deleted cookie : first_name
Name : first_name, Value: John
Name : last_name, Value: Player

 

 http://localhost:8080/main.jsp ๋ฅผ ๋‹ค์‹œํ•œ๋ฒˆ ์š”์ฒญํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ฒŒ ๋  ๊ฑฐ์—์š”.

 

Found Cookies Name and Value

Name : last_name, Value: Player

 

first_name ์ฟ ํ‚ค๋Š” ์‚ญ์ œ๋˜์–ด์„œ ์•ˆ๋‚˜์˜ค๋„ค์š”.

 

์–ด๋•Œ์š”? ์‰ฝ์ฃ ์ž‰?  

 




 

 

Reference : http://www.tutorialspoint.com/jsp/jsp_cookies_handling.htm