JSP + Servlet | Package javax.servlet ์˜ Interfaces

ํŒŒ๊ณผยท2022๋…„ 7์›” 22์ผ
0

JSP + Servlet

๋ชฉ๋ก ๋ณด๊ธฐ
30/33
post-custom-banner

์ •๋ฆฌ์— ์ฐธ๊ณ ํ•œ ๋ธ”๋กœ๊ทธ

๐Ÿ”— javax.servlet ํŒจํ‚ค์ง€

๊ณต์‹ ๋ฌธ์„œ

๐Ÿ”— docs.oracle.com์˜ javax.servlet


javax.servlet ํŒจํ‚ค์ง€

์„œ๋ธ”๋ฆฟ ๊ด€๋ จ ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค๋“ค์„ ํฌํ•จํ•œ๋‹ค.

Servlet
ServletConfig
ServletContext
ServletRequest
ServletResponse
RequestDispatcher


1. Interface Servlet

๋ชจ๋“  ์„œ๋ธ”๋ฆฟ์ด ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•œ๋‹ค.
์›น ์„œ๋ฒ„ ์•ˆ์—์„œ ๋Œ์•„๊ฐ€๋Š” ์ž‘์€ ์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ. ์›น ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์š”์ฒญ์„ ๋ฐ›๊ณ  ์‘๋‹ตํ•œ๋‹ค - ๋ณดํ†ต HTTP๋ฅผ ํ†ตํ•ด์„œ.

์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด javax.servlet.GenericServlet ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ์„œ๋ธ”๋ฆฟ ํ˜น์€ javax.servlet.http.HttpServlet ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ์„œ๋ธ”๋ฆฟ์„ ์ž‘์„ฑํ•œ๋‹ค.

์ด ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์„œ๋ธ”๋ฆฟ์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ , ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ์„œ๋ธ”๋ฆฟ์„ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์ œ๊ฑฐํ•˜๋Š” ์„œ๋ธ”๋ฆฟ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•œ๋‹ค.
์ด ๋ฉ”์„œ๋“œ๋“ค์€ ๋ชจ๋‘ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ˜ธ์ถœํ•จ์œผ๋กœ์จ ์‹คํ–‰๋œ๋‹ค.

void init(ServletConfig config)

์ดˆ๊ธฐํ™”. ์„œ๋น„์Šคํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐฐ์น˜๋œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธ.
์„œ๋ธ”๋ฆฟ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“  ํ›„ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋”ฑ ํ•œ ๋ฒˆ ํ˜ธ์ถœํ•œ๋‹ค. ์„œ๋ธ”๋ฆฟ์ด ์š”์ฒญ์„ ๋ฐ›๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ init()์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋˜์–ด์•ผ ํ•œ๋‹ค.
ServletConfig ๊ฐ์ฒด๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค.

void service(ServletRequest req, ServletResponse res)

์„œ๋ธ”๋ฆฟ์ด ์š”์ฒญ์— ์‘๋‹ตํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.
init() ๋ฉ”์„œ๋“œ๊ฐ€ ์™„๋ฒฝํžˆ ๋๋‚œ ํ›„์—๋งŒ ํ˜ธ์ถœ๋œ๋‹ค.

void destroy()

์„œ๋ธ”๋ฆฟ์ด ๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š์„ ๋•Œ ํ˜ธ์ถœ๋œ๋‹ค.


2. Interface ServletConfig

์„œ๋ธ”๋ฆฟ์ด ์ดˆ๊ธฐํ™”๋  ๋•Œ ์„œ๋ธ”๋ฆฟ์— ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด.

ServletContext getServletContext()

ํ˜ธ์ถœ์ž๊ฐ€ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ์™€ interactํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ServletContext ๊ฐ์ฒด ๋ฐ˜ํ™˜.


3. Interface ServletContext

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

Object getAttribute(String name)

ํ•ด๋‹น ์ด๋ฆ„์˜ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ ์†์„ฑ์„ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜, ํ•ด๋‹น ์ด๋ฆ„์˜ ์†์„ฑ์ด ์—†์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

void SetAttribute(String name, Object object)

ํ•ด๋‹น ์ด๋ฆ„์— ๊ฐ์ฒด๋ฅผ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.


4. Interface ServletRequest

์„œ๋ธ”๋ฆฟ์— ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•œ๋‹ค. ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ServletRequest ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ , ์„œ๋ธ”๋ฆฟ์˜ service ๋ฉ”์„œ๋“œ์˜ ์ธ์ž๋กœ ์ „๋‹ฌํ•œ๋‹ค.
ServletRequest ๊ฐ์ฒด๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜parameter์˜ ์ด๋ฆ„๊ณผ ๊ฐ’, ์†์„ฑattribute, ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์„ ํฌํ•จํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
ServletRequest๋ฅผ ์ƒ์†๋ฐ›๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ถ”๊ฐ€์ ์ธ ํ”„๋กœํ† ์ฝœ๋ณ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค(์˜ˆ๋ฅผ ๋“ค์–ด HttpServletRequest๋Š” HTTP๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•œ๋‹ค).

Object getAttribute(String name)

ํ•ด๋‹น ์ด๋ฆ„์˜ ์†์„ฑ ๊ฐ’์„ ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค - ์—†์œผ๋ฉด null ๋ฐ˜ํ™˜.

void SetAttribute(String name, Object o)

์ด ์š”์ฒญ์— ์†์„ฑ์„ ์ €์žฅํ•œ๋‹ค.


5. Interface ServletResponse

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


6. Interface RequestDispatcher

์š”์ฒญRequest์„ ํŒŒ์ผ์—์„œ ํŒŒ์ผ๋กœ ์ „๋‹ฌํ•˜๋Š” ์—ญํ• . ๋ง ๊ทธ๋Œ€๋กœ dispatcher.

ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์š”์ฒญ์„ ๋ฐ›๊ณ  ๊ทธ ์š”์ฒญ์„ ์„œ๋ฒ„์˜ ์—ฌ๋Ÿฌ ๋ฆฌ์†Œ์Šค(resource - ์„œ๋ธ”๋ฆฟ, htmlํŒŒ์ผ, jspํŒŒ์ผ ๋“ฑ)์œผ๋กœ ๋ณด๋‚ด๋Š” ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•œ๋‹ค. ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ RequestDispatcher ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋ฉด ์„œ๋ฒ„ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ฐ์‹ธ๋Š” wrapper๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ์„œ๋ธ”๋ฆฟ์˜ wrapper๋กœ ์˜๋„๋œ ์ธํ„ฐํŽ˜์ด์Šค์ง€๋งŒ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋Š” RequestDispatcher ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ์–ด๋–ค ๋ฆฌ์†Œ์Šค๋“  wrapํ•  ์ˆ˜ ์žˆ๋‹ค.

void forward(ServletRequest request, ServletResponse response)

์„œ๋ธ”๋ฆฟ์œผ๋กœ๋ถ€ํ„ฐ ์„œ๋ฒ„์˜ ๋‹ค๋ฅธ ๋ฆฌ์†Œ์Šค(์„œ๋ธ”๋ฆฟ, jspํŒŒ์ผ, htmlํŒŒ์ผ)๋กœ ์š”์ฒญ์„ ์ „๋‹ฌํ•œ๋‹ค.
์ด๋•Œ ์š”์ฒญ๊ณผ ํ•จ๊ป˜ response์‘๋‹ต์— ๋Œ€ํ•œ ์ฑ…์ž„๋„ ํ•จ๊ป˜ ๋‹ค๋ฅธ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์•„์˜ˆ ๋„˜๊ธด๋‹ค.

void include(ServletRequest request, ServletResponse response)

์‘๋‹ต์— ๋ฆฌ์†Œ์Šค์˜ ๋‚ด์šฉ(์„œ๋ธ”๋ฆฟ, jspํŒŒ์ผ, htmlํŒŒ์ผ)์„ ํฌํ•จํ•œ๋‹ค.
์ด๋•Œ๋Š” ์š”์ฒญ์„ ๋‹ค๋ฅธ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋„˜๊ฒผ๋‹ค๊ฐ€ ๋‹ค์‹œ ๋Œ๋ ค๋ฐ›๋Š”๋‹ค. ๋”ฐ๋ผ์„œ response์— ๋Œ€ํ•œ ์ฑ…์ž„(์ œ์–ด๊ถŒ)์€ ์ฒ˜์Œ ๊ณ ๊ฐ์ด ์š”์ฒญํ•œ ์„œ๋ธ”๋ฆฟ์ด ๋‹ค์‹œ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋œ๋‹ค.

RequestDispatcher ๊ฐ์ฒด๋Š” ServletContext๋‚˜ ServletRequest์˜ getRequestDispatcher()๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

์ฐธ๊ณ : https://dololak.tistory.com/502

ServletRequest๋ฅผ ํ†ตํ•ด ์–ป์„ ๊ฒฝ์šฐ

JSPํŽ˜์ด์ง€์—๋Š” ServletRequest์˜ instance์ธ request ๋‚ด์žฅ ๊ฐ์ฒด๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ request๋กœ ์‰ฝ๊ฒŒ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
HttpServletRequest์—์„œ๋Š” URL๊ฒฝ๋กœ๋ฅผ ํ†ตํ•ด์„œ๋งŒ ๋Œ€์ƒ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ServletContext์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์ ˆ๋Œ€๊ฒฝ๋กœ ๋ฐ ์ƒ๋Œ€๊ฒฝ๋กœ ๋ชจ๋‘ ์ง€์ • ๊ฐ€๋Šฅํ•˜๋‹ค.

RequestDispatcher dispatcher = request.getRequestDispatcher("/login.do");

ServletContext๋ฅผ ํ†ตํ•ด ์–ป์„ ๊ฒฝ์šฐ

web.xml์— ์ง€์ •ํ•œ ์„œ๋ธ”๋ฆฟ ์ด๋ฆ„ ๋˜๋Š” URL๊ฒฝ๋กœ(์ ˆ๋Œ€ ๊ฒฝ๋กœ๋กœ๋งŒ ๊ฐ€๋Šฅ)๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

๋Œ€์ƒ์„ ์ง€์ •ํ•œ RequestDispatcher ๊ฐ์ฒด๋กœ forward, include ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ํ•ด๋‹น ์ž์›(resource)์œผ๋กœ ์š”์ฒญ์„ ๋„˜๊ธฐ๊ฒŒ ๋œ๋‹ค.

post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€