๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

JSP ๊ตฌ๋™ ์›๋ฆฌ

by Leica 2020. 2. 11.
๋ฐ˜์‘ํ˜•

JSP ๊ตฌ๋™ ์›๋ฆฌ

JSP๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์‹ค์ œ๋กœ๋Š” JSP๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋œ ์„œ๋ธ”๋ฆฟ์ด ์‹คํ–‰๋œ๋‹ค.

 

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

 

๊ฐ„๋‹จํ•œ JSP ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ์‹ค์ œ๋กœ ์ž๋ฐ” ํŒŒ์ผ๋กœ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•ด๋ณด๊ฒ ๋‹ค.

 

HelloWorld.jsp

1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
Hello World
</body>
</html>
cs

 

JSP ์—”์ง„์€ JSP๊ฐ€ ์‹คํ–‰๋  ๋•Œ ์„œ๋ธ”๋ฆฟ์„ ๋งŒ๋“ ๋‹ค. ์„œ๋ธ”๋ฆฟ ์†Œ์Šค๋ฅผ ํ™•์ธํ•  ๊ฒƒ์ด๋ฏ€๋กœ ๋จผ์ € JSP๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

 

์‹คํ–‰ ํ™”๋ฉด

 

์ด์ œ ์ด HelloWorld.jsp ํŒŒ์ผ์— ๋Œ€์‘ํ•˜๋Š” ์„œ๋ธ”๋ฆฟ์ด ์ƒ์„ฑ๋์„๊ฒƒ์ด๋‹ค.

 

ํƒ์ƒ‰๊ธฐ๋ฅผ ์ผœ๊ณ  IDE์˜ ์ž„์‹œ ๋ฐฐํฌ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™ํ•œ๋‹ค.

 

IDE ์ž„์‹œ ๋ฐฐํฌ ๋””๋ ‰ํ† ๋ฆฌ

์œ„์™€ ๊ฐ™์ด HelloWorld.jsp๋กœ๋ถ€ํ„ฐ ์ž๋ฐ” ์†Œ์ŠคํŒŒ์ผ HelloWorld_jsp.java์™€ ์ปดํŒŒ์ผ๋œ ํด๋ž˜์ŠคํŒŒ์ผ HelloWorld_jsp.class๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๋‹ค.

 

์ƒ์„ฑ๋œ ์ž๋ฐ” ํŒŒ์ผ์„ ์—ด์–ด๋ณด์ž.

 

์ƒ์„ฑ๋œ HelloWorld_jsp ํด๋ž˜์Šค๋Š” HttpJspBase๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค. HttpJspBase๋Š” ํ†ฐ์บฃ์— ํฌํ•จ๋œ ํด๋ž˜์Šค์ด๋ฉฐ ํ†ฐ์บฃ์˜ JSP ์—”์ง„์€ ์„œ๋ธ”๋ฆฟ ์†Œ์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ด ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. (์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋งˆ๋‹ค ๋‹ค๋ฅด๋‹ค.)

 

๋‹ค์‹œ HttpJspBase๋ฅผ ์—ด์–ด๋ณด๋ฉด

 

HttpJspBase๋Š” javax.servlet.jsp.HttpJspPage ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

HttpJspPage๋Š” Servlet -> JspPage -> HttpJspPage ์ˆœ์œผ๋กœ ์ƒ์†๋˜๊ธฐ ๋•Œ๋ฌธ์— HttpJspPage๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋Š” ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค์ด๋‹ค.

๊ทธ๋Ÿฌ๋ฏ€๋กœ HttpJspBase๋ฅผ ์ƒ์†๋ฐ›๋Š” HelloWorld_jsp ์—ญ์‹œ ์„œ๋ธ”๋ฆฟ์ด ๋œ๋‹ค.

 

HttpJspBase.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public final void init(ServletConfig config) throws ServletException {
    super.init(config);
    this.jspInit();
    this._jspInit();
}
 
public final void destroy() {
    this.jspDestroy();
    this._jspDestroy();
}
 
public final void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this._jspService(request, response);
}
cs

HttpJspBase ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด, init()์€ jspInit()์„, destroy()๋Š” jspDestroy()๋ฅผ, service๋Š” _jspService๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

๋งŒ์•ฝ JSP์—์„œ init()์ด๋‚˜ destroy()๋ฅผ overrideํ•  ์ผ์ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ๋ฉ”์†Œ๋“œ ๋Œ€์‹  jspInit()๊ณผ jspDestroy()๋ฅผ overrideํ•œ๋‹ค.

 

HelloWorld_jsp.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public void _jspService(final HttpServletRequest request, final HttpServletResponse response)
    throws java.io.IOException, ServletException {
    // ์ค‘๋žต
    final PageContext pageContext;
    HttpSession session = null;
    final ServletContext application;
    final ServletConfig config;
    JspWriter out = null;
    final Object page = this;
 
    response.setContentType("text/html;charset=UTF-8");
    pageContext = _jspxFactory.getPageContext(this, request, response,
              nulltrue8192true);
    _jspx_page_context = pageContext;
    application = pageContext.getServletContext();
    config = pageContext.getServletConfig();
    session = pageContext.getSession();
    out = pageContext.getOut();
    _jspx_out = out;
 
    out.write("\r\n");
    out.write("\r\n");
    out.write("<html>\r\n");
    out.write("<head>\r\n");
    out.write("    <title>Title</title>\r\n");
    out.write("</head>\r\n");
    out.write("<body>\r\n");
    out.write("Hello World\r\n");
    out.write("</body>\r\n");
    out.write("</html>\r\n");
    out.write("\r\n");
    // ์ค‘๋žต
}
cs

์ƒ์„ฑ๋œ ์ž๋ฐ” ํŒŒ์ผ์˜ _jspService()์— JSP๊ฐ€ ์ˆ˜ํ–‰ํ•  ์ž‘์—…์ด ๋“ค์–ด๊ฐ„๋‹ค.

JSP ํŒŒ์ผ์— ๊ฐ„๋‹จํžˆ Hello World๋ผ๊ณ  ์ž‘์„ฑํ•œ ๊ฒƒ์ด JSP ์—”์ง„์— ์˜ํ•ด ์œ„์™€ ๊ฐ™์ด ๋ณ€ํ™˜๋œ๊ฒƒ์ด๋‹ค.

์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ service()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด service() ๋‚ด๋ถ€์—์„œ _jspService()๋ฅผ ํ˜ธ์ถœํ•จ์œผ๋กœ์จ JSP ํŽ˜์ด์ง€์— ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ๋“ค์ด ์‹คํ–‰๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

JSP ๋‚ด์žฅ ๊ฐ์ฒด(JSP Implicit Objects)

request, response, pageContext, session, application, config, out, page

_jspService()์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” HttpServlet์˜ doGet(), doPost()์™€ ๋™์ผํ•œ HttpServletRequest์™€ HttpServletResponse ๊ฐ์ฒด์ด๋‹ค. ๋‹จ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์€ ๋ฐ˜๋“œ์‹œ request, response์—ฌ์•ผ ํ•œ๋‹ค.

 

_jspService()์˜ ๋กœ์ปฌ ๋ณ€์ˆ˜ ์ค‘ pageContext, session, application, config, out, page๋Š” ๋ฐ˜๋“œ์‹œ ์ด ์ด๋ฆ„์œผ๋กœ ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค.

 

์ด๋ ‡๊ฒŒ _jspServce()์— ์„ ์–ธ๋œ request, response, pageContext, session, application, config, out, page ๊ฐ์ฒด๋“ค์€ ์ด ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋ฐ˜๋“œ์‹œ ์ค€๋น„๋˜๋Š” ๊ฐ์ฒด๋“ค์ด๊ธฐ ๋•Œ๋ฌธ์— 'JSP ๋‚ด์žฅ ๊ฐ์ฒด(Implicit Objects)'๋ผ ํ•œ๋‹ค.

์ด ๊ฐ์ฒด๋“ค์€ ๋ณ„๋„์˜ ์„ ์–ธ ์—†์ด JSP์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€