JiwonDev

#13 JSP (Java Server Pages)

by JiwonDev

# Web Server

์›น ํ‘œ์ค€ ํ”„๋กœํ† ์ฝœ(HTTP)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Clinet-Server ๊ตฌ์กฐ๋กœ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ๋”ฐ๋ผ ์ •์ ์ธ ๋ฐ์ดํ„ฐ (HTML, CSS, JS, ์ด๋ฏธ์ง€ ๋ฆฌ์†Œ์Šค ๋“ฑ)๋ฅผ ์‘๋‹ตํ•œ๋‹ค. ์‚ฌ์šฉ์ž์™€ ์ž…๋ ฅ๊ฐ’์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋Š” ๋™์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์‹ถ์–ด๋„ HTML, JS๋กœ๋Š” ๊ตฌํ˜„ํ•˜๊ธฐ๊ฐ€ ์–ด๋ ค์› ๋‹ค.

์—ฅ? ์š”์ฆ˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ๋Š” ๊ฐ€๋Šฅํ•˜์ง€ ์•Š๋‚˜์š”? SPA๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š”๋ฐ?

๋”๋ณด๊ธฐ

ECMA6 (2015)์ „์—๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์–ธ์–ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๊ธฐ์— ๋ฏผ๋งํ•œ ์ˆ˜์ค€์˜ ๊ตฌ๋ฆฐ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์ฃผ์—ˆ๋‹ค. ์š”์ฆ˜์—๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ง„ํ˜•์ด ๋งŽ์ด ๋ฐœ์ „ํ•˜๊ณ  ๋‹ค์–‘ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋‚˜์˜ค๋ฉด์„œ WAS(์›น์•ฑ, MPA๋ฐฉ์‹, Java) ๋Œ€์‹  SPA(Single page App, Js)๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒฝ์šฐ๋„ ๋งŽ์€๋ฐ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์žฅ๋‹จ์ ์ด ์žˆ๋‹ค.

 

 

1. ๊ธฐ์กด์˜ ์›น ์•ฑ(WAS, Java)๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ
= ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— [๊ฐ„๋‹จํ•œ ์ฝ๊ธฐ ์ „์šฉ ํด๋ผ์ด์–ธํŠธ]๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์ ์ ˆํ•˜๋‹ค.

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

WAS๋Š” ์›น์•ฑ ์„œ๋ฒ„์ด๊ณ , ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์„ MPA ๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค.

ํด๋ผ์ด์–ธํŠธ ์ธก์—๋Š” ๋ Œ๋”๋ง์ด ์™„๋ฃŒ๋œ ์ •์ ์ธ HTML ํŒŒ์ผ์„ ๋ฐ›๊ธฐ์— ๊ฒ€์ƒ‰์—”์ง„์ตœ์ ํ™”(SEO)๊ฐ€ ์‰ฝ๊ณ  ์„œ๋ฒ„์ธก์—์„œ ๋ชจ๋“  ๋กœ์ง์„ ๋‹ค ์ฒ˜๋ฆฌํ•œ ์ƒํƒœ๋กœ ๋ณด๋‚ด์ฃผ๊ธฐ์— ์ฒซ ํŽ˜์ด์ง€ ๋กœ๋”ฉ์ด ๋งค์šฐ ๋น ๋ฅด๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

(์‚ฌ์‹ค SPA ๊ฐœ๋…์ด ๋‚˜์˜จ๊ฒŒ ๋น„๊ต์  ์ตœ๊ทผ์ด๊ธฐ์— ๋Œ€๋ถ€๋ถ„์˜ ์˜› ์›น์‚ฌ์ดํŠธ๋“ค์€ ํด๋ผ์ด์–ธํŠธ์—์„œ ๋กœ์ง์„ ๊ฑฐ์˜ ์ฒ˜๋ฆฌํ•˜์ง€์•Š๊ณ , ์„œ๋ฒ„์—์„œ ์ฒ˜๋ฆฌํ•˜๊ณ  ํŽ˜์ด์ง€๋ฅผ ์ „์†กํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด์—ˆ๋‹ค.)

 


2. SPA (Single page app, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ)๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ

= ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ž์ฒด์—์„œ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ๊ฐ–์ถ˜ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ ์ ˆํ•˜๋‹ค.

์ด๋ฅผ ์‚ฌ์šฉ์ž(ํด๋ผ์ด์–ธํŠธ) ๊ด€์ ์—์„œ ๋‹ค์‹œ ๋งํ•˜์ž๋ฉด WAS์™€ ๋‹ฌ๋ฆฌ ์„œ๋ฒ„ ์ชฝ์—์„œ ์ „์ฒด HTML๋ฅผ ๋ฐ›์•„์˜ค๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ์ตœ์ดˆ์— HTML์„ ๊ฐ€์ง€๊ณ  ์ดํ›„ ํŽ˜์ด์ง€ ๋ฆฌ๋กœ๋”ฉ ์—†์ด ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ(.json)๋งŒ ์„œ๋ฒ„๋กœ ๋ถ€ํ„ฐ ๋ฐ›์•„์„œ ํ™”๋ฉด์„ ๊ฐฑ์‹ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ๊ฐฑ์‹ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋„ค์ดํ‹ฐ๋ธŒ ์•ฑ์— ๊ฐ€๊นŒ์šด ์ž์—ฐ์Šค๋Ÿฌ์šด ํŽ˜์ด์ง€ ์ด๋™๊ณผ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜(UX)์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.

์ตœ์ดˆ์˜ HTML ๋กœ๋”ฉ ์ดํ›„์—๋Š” ๋ฐ์ดํ„ฐ(json)๋ฅผ ๋ฐ›์•„์™€ ํด๋ผ์ด์–ธํŠธ ์•ฑ์„ ๊ฐฑ์‹ ํ•œ๋‹ค.

๋‹จ์ˆœ์ด ์„ค๋ช…๋งŒ ์ฝ์–ด๋ณด๋ฉด ๊ธฐ์กด์˜ WAS๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• (* MPA, Multi Page App)๋ณด๋‹ค ์žฅ์ ๋ฐ–์— ์—†๋Š” ๊ฒƒ ๊ฐ™์ง€๋งŒ ๊ทธ๋งŒํผ ๊ธฐ์กด์— ์„œ๋ฒ„์—์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋กœ์ง๋“ค์ด ํด๋ผ์ด์–ธํŠธ(JS)์— ์ถ”๊ฐ€๋˜์–ด ์ฝ”๋“œ๊ฐ€ ์ƒ๋‹นํžˆ ๋ณต์žกํ•ด์ง€๊ณ  HTML์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•ด์ ธ ๊ฒ€์ƒ‰์—”์ง„์ตœ์ ํ™”(SEO)์— ์ทจ์•ฝํ•˜๋ฉฐ ์ตœ์ดˆ ์‹คํ–‰ ์‹œ ์ดˆ๊ธฐ๋ Œ๋”๋ง ์†๋„๊ฐ€ ๋Š๋ ค์ง„๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

์‚ฌ์‹ค ๊ธฐ์กด์˜ WAS๋ฅผ ์‚ฌ์šฉํ•˜๋Š” [MPA ๋ฐฉ์‹] ์ด๋ผ๋Š” ์šฉ์–ด๋Š” SPA๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉด์„œ ๊ตฌ๋ถ„์ง“๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ์šฉ์–ด์ด๊ธฐ๋„ ํ•˜๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ง„ํ˜•์—์„œ๋Š” ์ด๋Ÿฌํ•œ SPA์˜ ๋‹จ์ ๋“ค์„ ๊ทน๋ณตํ•˜๋ ค๊ณ  ํ”„๋กœ์ ํŠธ ์ฝ”๋“œ๋ฅผ ๋ฒˆ๋“ค๋งํ•ด์ฃผ๋Š” ๋„๊ตฌ (webpack), SPA๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด MPA์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋Š” Next.js(React), Nuxt.js(Vue) ๊ฐ™์€ ์„œ๋ฒ„์‚ฌ์ด๋“œ ๋ Œ๋”๋ง, [SSR ํ”„๋ ˆ์ž„์›Œํฌ]๊ฐ€ ๋“ฑ์žฅํ•˜๊ณ  ์žˆ๋‹ค.

 


# WAS (Web App Server)

Servlet, JSP๋ฅผ ์‹คํ–‰ ํ•  ์ˆ˜ ์žˆ๋Š” ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ์›น์ปจํ…Œ์ด๋„ˆ ๋˜๋Š” ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

์„œ๋ธ”๋ฆฟ์ด ๋ญ์ฃ ?

๋”๋ณด๊ธฐ

Server + let (์›น ์„œ๋ฒ„์—์„œ ์‹คํ–‰๋˜๋Š” ์ž‘์€ ํ”„๋กœ๊ทธ๋žจ)

์ž๋ฐ”์—์„œ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋Œ€ํ•ด ๋™์ ์œผ๋กœ ์ž‘๋™ํ•˜๋Š” ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปดํฌ๋„ŒํŠธ์ด๋‹ค. ์ข€ ๋” ์‰ฝ๊ฒŒ๋งํ•˜๋ฉด ์ž๋ฐ”์—์„œ ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์œ„ํ•ด ์ œ๊ณตํ•˜๋Š” Servlet Class ๊ตฌํ˜„ ๊ทœ์น™์„ ์ง€ํ‚จ ์ž๋ฐ” ์•ฑ์„ ๋งํ•œ๋‹ค.

  • html์„ ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญ์— ์‘๋‹ตํ•œ๋‹ค.
  • Java Thread๋ฅผ ์ด์šฉํ•˜์—ฌ ๋™์ž‘ํ•œ๋‹ค.
  • MVC ํŒจํ„ด์—์„œ Controller๋กœ ์ด์šฉ๋œ๋‹ค.
  • HTTP ํ”„๋กœํ† ์ฝœ ์„œ๋น„์Šค๋ฅผ ์ง€์›ํ•˜๋Š” javax.servlet.http.HttpServlet ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค.
  • UDP๋ณด๋‹ค ์ฒ˜๋ฆฌ ์†๋„๊ฐ€ ๋Š๋ฆฌ๋‹ค.
  • HTML ๋ณ€๊ฒฝ ์‹œ Servlet์„ ์žฌ์ปดํŒŒ์ผํ•ด์•ผ ํ•˜๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

์ด๋Ÿฌํ•œ ์„œ๋ธ”๋ฆฟ์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์„œ๋ธ”๋ฆฟ ํ”„๋กœ๊ทธ๋žจ์˜ ์š”์ฒญ์— ๋”ฐ๋ผ ์Šค๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋Š” ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ์›น ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ํ•œ๋‹ค. ์›น ์ปจํ…Œ์ด๋„ˆ์—์„œ๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ(Requset)๋ฅผ ๋ฐ›์•„ ์›น ์„œ๋ฒ„์™€ ํ†ต์‹ ํ•˜๊ณ , ์š”์ฒญ์— ํ•„์š”ํ•œ ์„œ๋ธ”๋ฆฟ์„ ์ด์šฉํ•ด ๊ฒฐ๊ณผ(Response)๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์•„ ๋‹ค์‹œ ์›น ์„œ๋ฒ„์— ์ „๋‹ฌํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ œ๊ณตํ•ด์ค€๋‹ค.

์‰ฝ๊ฒŒ๋งํ•ด Servlet์€ ์ž๋ฐ” ์ฝ”๋“œ๋กœ ๋งŒ๋“ค์–ด์ง„ ์ž‘์€ ์•ฑ์ด๋‹ค.

[์›น ์„œ๋ฒ„]์™€ [์›น ์ปจํ…Œ์ด๋„ˆ]๋ฅผ ํ•ฉ์ณ์„œ WAS๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ผ์ข…์˜ ๋ฏธ๋“ค์›จ์–ด๋กœ์„œ ์›น ์„œ๋ฒ„ ๋‹จ๋…์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๋Š” DB์ ‘๊ทผ์ด๋‚˜ ๋ณต์žกํ•œ ๋กœ์ง์ด ํฌํ•จ๋œ ๋™์ ์ธ ์ปจํ…์ธ ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•œ ์ฒ˜๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

 

  • ํด๋ผ์ด์–ธํŠธ(์‚ฌ์šฉ์ž)  →  Web Server  →  DB
  • ํด๋ผ์ด์–ธํŠธ(์‚ฌ์šฉ์ž)  →  WAS → DB
  • ํด๋ผ์ด์–ธํŠธ(์‚ฌ์šฉ์ž)  →  Web Server  →  WAS  →  DB

 


# ์›น ์ปจํ…Œ์ด๋„ˆ (์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ)

์ „์ฒด์ ์ธ ๊ทธ๋ฆผ

์›น ์ปจํ…Œ์ด๋„ˆ๋Š” ๊ธฐ์กด์˜ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์„œ๋ฒ„ Socket์„ ๋งŒ๋“ค๊ณ , ํฌํŠธ๋ฅผ ์—ฐ๊ฒฐํ•˜๊ณ  ๋„คํŠธ์›Œํฌ ์ŠคํŠธ๋ฆผ์„ ์ƒ์„ฑํ•˜๋Š” ๋ณต์žกํ•œ ์ž‘์—…์„ ๋Œ€์‹  ๋งก์•„์„œ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ์†Œํ”„ํŠธ์›จ์–ด์ด๋‹ค. ์›น ์ปจํ…Œ์ด๋„ˆ๋‹จ์—์„œ ํ†ต์‹  API๋ฅผ ์‚ฌ์šฉํ•ด ์›น ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋ฏ€๋กœ ๊ฐœ๋ฐœ์ž๋Š” ์„œ๋ธ”๋ฆฟ์— ๊ตฌํ˜„ํ•  ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฝ”๋“œ๋งŒ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

  1. ์›น ์„œ๋ฒ„๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์˜ Servlet ์š”์ฒญ์„ ๋ฐ›๋Š”๋‹ค.
  2. Container๋Š” ์„œ๋ธ”๋ฆฟ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ์‘๋‹ต๊ฐ์ฒด (HttpServletRequest, HttpServletResponse)๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
    Container๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋‚ธ ์š”์ฒญ url์„ ๋ถ„์„ํ•˜์—ฌ ์–ด๋–ค ์„œ๋ธ”๋ฆฟ ์š”์ฒญ์ธ์ง€ ํŒ๋‹จํ•˜๊ณ  web.xml(๋ฐฐํฌ์ •๋ณด)๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ ์Šค๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑ ํ›„ ๋งŒ๋“ค์–ด์ง„ Response, Requset ๊ฐ์ฒด๋ฅผ ์Šค๋ ˆ๋“œ์— ์ „๋‹ฌํ•œ๋‹ค.
  3. Container๋Š” ์Šค๋ ˆ๋“œ์™€ ๊ฐ์ฒด ์ƒ์„ฑ์ด ์™„๋ฃŒ๋˜๋ฉด Servlet์˜ .service() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
  4. ํ˜ธ์ถœ๋œ Servlet์€ ์Šค๋ ˆ๋“œ์˜ ์š”์ฒญ์— ๋”ฐ๋ผ doGet() ์ด๋‚˜ doPost()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. (๊ธฐ๋ณธ๊ฐ’์€ doGet()์œผ๋กœ ๊ฐ„์ฃผ)
  5. ํ˜ธ์ถœ๋œ doGet() doPost() ๋ฉ”์„œ๋“œ๋Š” ๋™์  ํŽ˜์ด์ง€ ์ƒ์„ฑํ•˜๊ณ  Response ๊ฐ์ฒด์— ์‹ค์–ด Container์— ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  6. Conainer๋Š” ์ „๋‹ฌ๋ฐ›์€ Response ๊ฐ์ฒด๋ฅผ HttpResponseํ˜•ํƒœ๋กœ ์ „ํ™˜ํ•˜์—ฌ ์›น ์„œ๋ฒ„๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ณด๋‚ธ๋‹ค. ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉดํ•ด๋‹น ์Šค๋ ˆ๋“œ๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  ํ•„์š”์—†์–ด์ง„ ์š”์ฒญ ๋ฐ ์‘๋‹ต ๊ฐ์ฒด๋ฅผ ์†Œ๋ฉธ์‹œํ‚จ๋‹ค.
// ์„œ๋ธ”๋ฆฟ ์ฝ”๋“œ ์˜ˆ์‹œ
package myServletTest;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@WebServlet("/myServlet/*")
public class MyServlet extends HttpServlet{
    
    private static final long serialVersionUID = 1L;
    String msg = "default";
    
    // The servlet is initialized by calling the init() method.
    public void init() throws ServletException {
        msg = "init ๋ฉ”์†Œ๋“œ Call";
    }
    
    
    // The servlet calls service() method to process a client's request.
    // service () ๋ฉ”์†Œ๋“œ๋Š” HTTP ์š”์ฒญ ์œ ํ˜• (GET, POST, PUT, DELETE ๋“ฑ)์„ ํ™•์ธํ•˜๊ณ  
    // ์ ์ ˆํ•˜๊ฒŒ doGet, doPost, doPut, doDelete ๋“ฑ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ
    @Override
    public void service(ServletRequest request, ServletResponse response) 
            throws ServletException, IOException {
        String text = "Service ๋ฉ”์†Œ๋“œ Call";
        if(!"default".equals(msg)){
            text = msg +"<br>"+text;
            msg = "default";
        }
        response.setContentType("text/plain");  
        response.setCharacterEncoding("UTF-8"); 
        response.getWriter().write(text);       
    }
    
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String text = "doGet ๋ฉ”์†Œ๋“œ Call";
        commonMethod(text, response);
    }
    
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String text = "doPost ๋ฉ”์†Œ๋“œ Call";
        commonMethod(text, response);
    }
 
    // The servlet is terminated by calling the destroy() method.
    @Override
    public void destroy() {
        // Finalization code...
        System.out.println("destroy method call");
    }
    
 
    private void commonMethod(String text, HttpServletResponse response) 
            throws ServletException, IOException{
        if(!"default".equals(msg)){
            text = msg +"<br>"+text;
            msg = "default";
        }
        response.setContentType("text/plain");  
        response.setCharacterEncoding("UTF-8"); 
        response.getWriter().write(text);   
    }
}

 


# Servlet์˜ ์ƒ๋ช…์ฃผ๊ธฐ

๋”๋ณด๊ธฐ

 

  1. ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ์ปจํ…Œ์ด๋„ˆ๋Š” ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์ด ๋ฉ”๋ชจ๋ฆฌ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ์—†์„ ๊ฒฝ์šฐ init() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ์— ์ ์žฌํ•œ๋‹ค. init() ๋ฉ”์„œ๋“œ๋Š” ์ฒ˜์Œ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰๋œ๋‹ค.
  2. ์‹คํ–‰๋„์ค‘ ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์ด ๋ณ€๊ฒฝ ๋  ๊ฒฝ์šฐ, ๊ธฐ์กด ์„œ๋ธ”๋ฆฟ์„ ์†Œ๋ฉธ์‹œํ‚ค๊ณ  init() ๋ฉ”์„œ๋“œ๋ฅผ ๋‹ค์‹œ ํ˜ธ์ถœํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋‹ด์€ ์„œ๋ธ”๋ฆฟ์„ ๋ฉ”๋ชจ๋ฆฌ์— ์ ์žฌํ•œ๋‹ค.
  3. ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋”ฐ๋ผ ์ปจํ…Œ์ด๋„ˆ๋Š” ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์˜ sevice() ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์„œ๋ธ”๋ฆฟ์—๊ฒŒ HttpServletRequest, HttpServletResponse ๋ฅผ ํ†ตํ•ด ์‘๋‹ต ๊ฐ์ฒด๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
  4. ์ž‘์—…์„ ๋งˆ์น˜๋ฉด ์ปจํ…Œ์ด๋„ˆ๋Š” ์„œ๋ธ”๋ฆฟ์˜ destroy() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. init() ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰๋˜๋ฉฐ ์ข…๋ฃŒ์‹œ์— ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š” ์ž‘์—…๋“ค์€ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•œ๋‹ค.

 


# JSP (Java Server Pages)

Java ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€์žˆ๋Š” ์›น (HTML) ์ฝ”๋“œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

๊ธฐ์กด์—๋Š” ์›นํŽ˜์ด์ง€๋ฅผ '๊ฐœ๋ฐœ์ž'๊ฐ€ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ, ์ ์  ์›นํŽ˜์ด์ง€๊ฐ€ ๋ณต์žกํ•ด์ง€๊ณ  ๊ณ ๋„ํ™”๋˜๋ฉฐ ๊ฐœ๋ฐœ์ž์™€ ๋””์ž์ด๋„ˆ๊ฐ€ ๋ถ„์—…ํ™” ๋˜๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค. ๊ธฐ์กด์˜ ์„œ๋ธ”๋ฆฟ ๋ฐฉ์‹์€ ์ „๋ถ€ ์ž๋ฐ”์ฝ”๋“œ๋กœ HTML์ด ์ „์†ก๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋””์ž์ด๋„ˆ๊ฐ€ ์ˆ˜์ •ํ•˜๊ธฐ ์–ด๋ ค์› ๋Š”๋ฐ, ์ด๋ฅผ ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ณ ์ž ๋“ฑ์žฅํ•œ ๊ฐœ๋…์ด JSP์ด๋‹ค.

 

์„œ๋ธ”๋ฆฟ์ด ์ž๋ฐ” ์†Œ์Šค ์ฝ”๋“œ์†์— HTML ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด ์ž‘์„ฑํ•œ๋‹ค๋ฉด, JSP๋Š” ์ด์™€ ๋ฐ˜๋Œ€๋กœ HTML์— ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ๋„ฃ๋Š” ๊ตฌ์กฐ์ด๋‹ค. ์‰ฝ๊ฒŒ๋งํ•ด ๋‹จ์ˆœํžˆ HTML ์ฝ”๋“œ์— <% ์†Œ์Šค์ฝ”๋“œ %> , <%=์†Œ์Šค์ฝ”๋“œ=%> ์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ž๋ฐ”์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•œ ๊ฒƒ ๋ฟ์ด๋‹ค.

< JSP ํ˜•์‹>
์ง€์‹œ์–ด : <%@ ... %>
์„ ์–ธ : <%! ... %>
์‹(ํ‘œํ˜„) : <%= ... %>
scriplet : <% ... %>
์•ก์…˜ํƒœ๊ทธ : <jsp: ... />
<%@page import="java.util.Date"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP Test</title>
</head>
<body>
     jsp ์—ฐ์Šต
     <script type="text/javascript">
          var a = 10; // ๊ธฐ์กด์˜ HTML script ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•.(Js์ฝ”๋“œ)
          document.write("a:", a);
     </script>
     
     <%
          int b = 10; // JSP ๋ฐฉ์‹ (Java์ฝ”๋“œ)
          out.println("b:" + b);
     %>
     
     <%
         int a = 0, sum = 0; // ์ง€์—ญ ๋ณ€์ˆ˜ ์„ ์–ธ
         do{
              a++;
              sum += a;
         }while(a <10);
         out.println("ํ•ฉ์€" + sum);
     %>
     
     <br>
     <%= "ํ•ฉ์€ " +sum %>
     <%= ir + "๋‹˜์˜ ์ „ํ™”๋ฒˆํ˜ธ:" + tel %>
    
     <%! String tel = "111-1111"; %> <!-- ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ํ•„๋“œ ์„ ์–ธ (=์ „์—ญ ์ ‘๊ทผ) -->
     <%!
          public int add(int m, int n) { // ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ ๋ฉ”์†Œ๋“œ ์„ ์–ธ
          return m + n;
          }    
     %>
     
    <br>
    <% out.println(add(10,20)); %>
    <br>
    <%= add(100, 200) %>
</body>
</html>

 

HTML ๋‚ด๋ถ€์— ์‚ฝ์ž…๋˜์–ด์ง„ ์ž๋ฐ”์ฝ”๋“œ๋Š” WAS์— ์˜ํ•ด ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋กœ ๋ณ€ํ™˜๋˜์–ด ์‚ฌ์šฉ๋˜์–ด์ง„๋‹ค. ์ฆ‰ JSP์— ์ž‘์„ฑ๋œ Java ์ฝ”๋“œ์ž์ฒด๊ฐ€ ์›น ๋ธŒ๋ผ์šฐ์ €๋กœ ๋ณด๋‚ด์ง€๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์›น ์ปจํ…Œ์ด๋„ˆ์—์„œ ํ•ด๋‹น Java ์ฝ”๋“œ๋ฅผ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์ปดํŒŒ์ผํ•˜์—ฌ (ํ˜น์€ ์ด๋ฏธ ์ปดํŒŒ์ผ ๋œ ์„œ๋ธ”๋ฆฟ์„ ๋กœ๋“œํ•˜์—ฌ) HTML์„ ์ƒ์„ฑํ•œ๋‹ค.

 

์›๋ž˜ ์„œ๋น„์Šค ๋™์ž‘์„ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ์ž๋ฐ”์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์ปดํŒŒ์ผ ํ•ด์•ผ ํ–ˆ์ง€๋งŒ JSP๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋งŒ๋“ค์–ด์ง„ [JSP ํŽ˜์ด์ง€]๋ฅผ ์„œ๋ฒ„ ๋””๋ ‰ํ† ๋ฆฌ์— ์ถ”๊ฐ€์‹œํ‚ค๊ธฐ๋งŒ ํ•˜๋ฉด WAS์—์„œ ํ•ด๋‹น Java ์ฝ”๋“œ๋ฅผ ์„œ๋ธ”๋ฆฟ(.class)์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค. ์ฆ‰ ์›น ๊ฐœ๋ฐœ์ž๊ฐ€ ์ฝ”๋“œ๋ฅผ ์ •๋ง ์‰ฝ๊ฒŒ ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฐ”๋€Œ์—ˆ๋‹ค. ๋˜ํ•œ ์ฒ˜์Œ์—๋„ ์–ธ๊ธ‰ํ–ˆ์ง€๋งŒ, JSP๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์„œ ๋””์ž์ด๋„ˆ์™€ ๊ฐœ๋ฐœ์ž์˜ ์˜์—ญ์„ ๋ถ„๋ฆฌ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ๋„ ์žˆ๋‹ค. 


# Tomcat (์•„ํŒŒ์น˜ WAS)

์•„ํŒŒ์น˜ ์žฌ๋‹จ์˜ ์›น ์ปจํ…Œ์ด๋„ˆ. JSP์™€ ์ž๋ฐ” Servlet์„ ์›น ์„œ๋ฒ„์™€ ์—ฐ๋™ํ•˜์—ฌ ์‹คํ–‰ ํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฐ” ๋Ÿฐํƒ€์ž„ํ™˜๊ฒฝ์„ ์ œ๊ณตํ•œ๋‹ค.

ํ†ฐ์บฃ์€ ์›น ์„œ๋ฒ„(HTTP ์„œ๋ฒ„)๋ฅผ ๋‚ด์žฅํ•˜๊ณ  ์žˆ๊ณ , ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ž๋ฐ” ํ™˜๊ฒฝ(JRE 1.1 ์ด์ƒ ์„ค์น˜)๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค.

http://tomcat.apache.org/

์™ผ์ชฝ์— Download ๋ฉ”๋‰ด๋ฅผ ํ†ตํ•ด ์„ค์น˜ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

Apache ์›น์„œ๋ฒ„์™€ ํ†ฐ์บฃ์˜ ์ฐจ์ด์ ์€ ๋ญ์ฃ ?

๋”๋ณด๊ธฐ

์•„ํŒŒ์น˜(apache) ์›น์„œ๋ฒ„

์•„ํŒŒ์น˜ ์žฌ๋‹จ์—์„œ ๋งŒ๋“  HTTP ์›น ์„œ๋ฒ„๋กœ ๊ตฌ์ถ•์ด ์‰ฝ๊ณ  ์ค‘์†Œ๊ธฐ์—…๋“ค์—๊ฒŒ ๋ฌด๋ฃŒ๋กœ ์ œ๊ณตํ•ด์ฃผ์–ด ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.

 

ํ†ฐ์บฃ(Tomcat)

์•„์ฐจํ”ผ ์žฌ๋‹จ์—์„œ ๋งŒ๋“  ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„(WAS)๋กœ ์ž๋ฐ” ์„œ๋ธ”๋ฆฟ์„ ์‹คํ–‰์‹œํ‚ค๊ณ  JSP ์ฝ”๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ์›น ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค. WAS ๋‚ด๋ถ€์— ์›น ์„œ๋ฒ„๋ฅผ ํฌํ•จํ•˜๊ณ ์žˆ์œผ๋ฉฐ, ์›น ์„œ๋ฒ„์—์„œ ๋„˜์–ด์˜จ ๋™์ ์ธ ํŽ˜์ด์ง€๋ฅผ ์ฝ์–ด ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ HTML๋กœ ๋งŒ๋“ค์–ด ์•„ํŒŒ์น˜ ์›น ์„œ๋ฒ„์— ๋Œ๋ ค์ค€๋‹ค.

 


 

'๐ŸŒฑBackend > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

#15 JSP ์ฟ ํ‚ค์™€ ์„ธ์…˜  (0) 2021.07.16
#14. JSP์™€ ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ  (0) 2021.07.16
#12. Optional API (Null๊ฐ’ ์ฒ˜๋ฆฌ)  (0) 2021.07.12
#11 Stream API  (0) 2021.07.12
#10 Modern Java (Java8~)  (0) 2021.07.12

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ