Servlet-JSP

Servlet

voider 2020. 9. 10. 13:17

๐Ÿ“š ์ž๋ฐ” ์›น์„ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ ์„ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

Servlet์ด๋ž€?

์ด์ „์— ์ •์ ์ธ ์›น์„ ๋„ˆ๋จธ์„œ ๋™์  ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์œ„ํ•œ ์ž๋ฐ” ํด๋ž˜์Šค๋‹ค.

ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ๋”ฐ๋ผ ์„œ๋ฒ„์—์„œ ๋™์ ์œผ๋กœ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค

์„œ๋ธ”๋ฆฟ์€ ์ผ๋ฐ˜์ ์ธ ์ž๋ฐ”์™€ ๋‹ฌ๋ฆฌ ํ†ฐ์บฃ๊ณผ ๊ฐ™์€ JSP/Servlet ์ปจํ…Œ์ด๋„ˆ์—์„œ ์‹คํ–‰๋œ๋‹ค.


์„œ๋ธ”๋ฆฟ์˜ ๋™์ž‘ ๊ณผ์ •์ด๋‹ค.

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•˜๋ฉด web server๋Š” WAS์— ์œ„์ž„ํ•œ๋‹ค. WAS๋Š” ๊ฐ ์š”์ฒญ์— ํ•ด๋‹นํ•˜๋Š” ์„œ๋ธ”๋ฆฟ์„ ์‹คํ–‰ํ•œ ๋‹ค์Œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „์†กํ•œ๋‹ค.

ํŠน์ง•

  • ์„œ๋ฒ„ ์ชฝ์—์„œ ์‹คํ–‰๋˜๋ฉด์„œ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰

  • ์ •์  ์›น ๋ฌธ์ œ๋ฅผ ๋ณด์™„ํ•˜์—ฌ ๋™์  ๊ธฐ๋Šฅ ์ œ๊ณต

  • ์“ฐ๋ ˆ๋“œ ๋ฐฉ์‹์œผ๋กœ ์‹คํ–‰

  • ์ปจํ…Œ์ด๋„ˆ์—์„œ ์‹คํ–‰

    Servlet API ๊ณ„์ธต ๊ตฌ์กฐ

๋‘ ์ธํ„ฐํŽ˜์ด์Šค Servlet, ServletConfig๋ฅผ ์ถ”์ƒํด๋ž˜์Šค GenericServlet๊ฐ€ ๊ตฌํ˜„ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  GenericServlet์„ ์ƒ์†ํ•˜๋Š” HttpServlet์ด ์žˆ๋‹ค.

  • Servlet, ServletConfig
    Servlet๊ด€๋ จ ์ถ”์ƒ ๋ฉ”์„œ๋“œ๊ฐ€ ์„ ์–ธ ๋˜์–ด ์žˆ๋‹ค.

  • GenericServlet ํด๋ž˜์Šค
    ์œ„์˜ ๋‘ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์ผ๋ฐ˜์ ์ธ ์„œ๋ธ”๋ฆฟ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค.
    ์ด ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด ํ”„๋กœํ† ์ฝœ์— ๋”ฐ๋ผ service() ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•„์š”

  • HttpServlet ํด๋ž˜์Šค
    HTTPํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•˜๋Š” ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์„œ๋ธ”๋ฆฟ ๊ธฐ๋Šฅ ์ˆ˜ํ–‰
    ์›น ๋ธŒ๋ผ์šฐ์ € ๊ธฐ๋ฐ˜ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ์„œ๋ธ”๋ฆฟ ๋งŒ๋“ค ๋•Œ ์ƒ์† ๋ฐ›์•„ ์‚ฌ์šฉ
    ์š”์ฒญ ์‹œ service()๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด์„œ ๋ฐฉ์‹์— ๋”ฐ๋ผ doGet() ํ˜น์€ doPost()๊ฐ€ ์ฐจ๋ก€๋กœ ํ˜ธ์ถœ ๋จ

HttpServlet์˜ ๋ฉ”์„œ๋“œ

//์‚ญ์ œ ์š”์ฒญ ์ˆ˜ํ–‰
protected doDelete(HttpServletRequest req, HttpServletResponse resp);

//GET ์š”์ฒญ ์ˆ˜ํ–‰
protected doGet(HttpServletRequest req, HttpServletResponse resp);

//HEAD ์š”์ฒญ ์ˆ˜ํ–‰
protected doHead(HttpServletRequest req, HttpServletResponse resp);

//POST ์š”์ฒญ ์ˆ˜ํ–‰
protected doPost(HttpServletRequest req, HttpServletResponse resp);

//ํ‘œ์ค€ HTTP request๋ฅผ public service()์—์„œ ์ „๋‹ฌ ๋ฐ›์•„ doXXX()ํ˜ธ์ถœ
protected service(HttpServletRequest req, HttpServletResponse resp);

//ํด๋ผ์ด์–ธํŠธ์˜ request๋ฅผ protected service()์—๊ฒŒ ์ „๋‹ฌ
public service (HttpServletRequest req, HttpServletResponse resp)

๊ณผ์ •์€ public service() -> protected service() -> doXXX()

์„œ๋ธ”๋ฆฟ ์ƒ๋ช…์ฃผ๊ธฐServlet Lifecycle

์„œ๋ธ”๋ฆฟ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ดˆ๊ธฐํ™”๋ฅผ ๊ฑฐ์ณ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ธ์Šคํ„ด์Šค์— ์˜ฌ๋ผ๊ฐ€๊ณ , ์ž‘์—…์„ ๋งˆ์น˜๋ฉด ์†Œ๋ฉธํ•œ๋‹ค.
์ด ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์น  ๋•Œ๋งˆ๋‹ค ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ์ดˆ๊ธฐํ™”, DB์—ฐ๋™, ๋งˆ๋ฌด๋ฆฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๊ฐ ๊ณผ์ •์—์„œ ํ˜ธ์ถœ๋˜์–ด ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์ด ์„œ๋ธ”๋ฆฟ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๋‹ค.

๋งํ•˜์ž๋ฉด ์‹คํ–‰ ๋‹จ๊ณ„๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” (์ฝœ๋ฐฑ)๋ฉ”์„œ๋“œ๋ฅผ ๋งํ•œ๋‹ค.

์ƒ๋ช…์ฃผ๊ธฐ ๋‹จ๊ณ„ ํ˜ธ์ถœ ๋ฉ”์„œ๋“œ ํŠน์ง•
์ดˆ๊ธฐํ™” init() - ์„œ๋ธ”๋ฆฟ ์š”์ฒญ ์‹œ ๋งจ ์ฒ˜์Œ ํ•œ ๋ฒˆ๋งŒ ํ˜ธ์ถœ
- ์„œ๋ธ”๋ฆฟ ์ƒ์„ฑ ์‹œ ์ดˆ๊ธฐํ™” ์ž‘์—… ์ˆ˜ํ–‰
์ž‘์—… ์ˆ˜ํ–‰ doGet()
doPost()
- ์„œ๋ธ”๋ฆฟ ์š”์ฒญ ์‹œ ๋งค๋ฒˆ ํ˜ธ์ถœ
- ์‹ค์ œ๋กœ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•˜๋Š” ์ž‘์—… ์ˆ˜ํ–‰
์ข…๋ฃŒ destroy() - ์„œ๋ธ”๋ฆฟ์ด ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์†Œ๋ฉธ๋  ๋•Œ ํ˜ธ์ถœ
- ์„œ๋ธ”๋ฆฟ์˜ ๋งˆ๋ฌด๋ฆฌ ์ž‘์—… ์ˆ˜ํ–‰

์ƒํ™ฉ์— ๋”ฐ๋ผ destroy()๋Š” ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, doGet()์ด๋‚˜ doPost()๋Š” ํ•ต์‹ฌ๊ธฐ๋Šฅ์„ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

ํ…Œ์ŠคํŠธ

package com.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/first")
public class FirstServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        System.out.println("init.......");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doGet().............");
    }

}

/*
doGet() 4ํšŒ ํ˜ธ์ถœ

init.......
doGet().............
doGet().............
doGet().............
doGet().............

์—ฌ๋Ÿฌ ๋ฒˆ ํ˜ธ์ถœํ•ด๋„ init()์€ ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
*/

'Servlet-JSP' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Connection Pool  (0) 2020.09.10
ํฌ์›Œ๋“œ  (0) 2020.09.10
JDBC  (0) 2020.09.10
GET & POST  (0) 2020.09.10
Request - Response  (0) 2020.09.10