[JSP/Servlet] Servlet ์ƒ๋ช…์ฃผ๊ธฐ

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

Servlet์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด init(), service(), destroy() ๋ฉ”์†Œ๋“œ๋ฅผ ์•Œ์•„์•ผ ํ•œ๋‹ค.

init()

  • WAS๊ฐ€ ์„œ๋ธ”๋ฆฟ ์š”์ฒญ์„ ๋ฐ›์•˜์„ ๋•Œ, ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์ด ๋ฉ”๋ชจ๋ฆฌ์— ์—†๋‹ค๋ฉด, ๊ทธ ์„œ๋ธ”๋ฆฟ์„ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ฆฐ๋‹ค.
  • ์ด๋•Œ ์ตœ์ดˆ ํ•œ ๋ฒˆ init() ๋ฉ”์†Œ๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

service()

  • ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์ด ์š”์ฒญ์„ ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค service() ๋ฉ”์†Œ๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

destroy()

  • ์„œ๋ธ”๋ฆฟ์˜ ๋‚ด์šฉ์ด ๊ฐฑ์‹ ๋˜์„œ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๊ฐฑ์‹ ๋˜๊ฑฐ๋‚˜, WAS๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ destroy() ๋ฉ”์†Œ๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

์ƒ๋ช… ์ฃผ๊ธฐ์— ๊ด€ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ด๋ณด์ž.



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("/LifeCycleExample")
public class LifeCycleExample extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public LifeCycleExample() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public void init() throws ServletException {
    	System.out.println("init() ๋ฉ”์†Œ๋“œ");
    }

    @Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("service() ๋ฉ”์†Œ๋“œ");
	}

    @Override
    public void destroy() {
    	System.out.println("destroy() ๋ฉ”์†Œ๋“œ");
    }

}

์œ„์˜ ์†Œ์Šค๋ฅผ ์„œ๋ฒ„๋กœ ์‹คํ–‰ํ•œ ๋’ค, ์ฝ˜์†” ์ฐฝ์„ ํ™•์ธํ•ด ๋ณด๋ฉด ๋ฉ”์†Œ๋“œ๊ฐ€ ์‹คํ–‰๋ฌ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

init() ๋ฉ”์†Œ๋“œ
service() ๋ฉ”์†Œ๋“œ

destroy()์˜ ์‹คํ–‰ ์‹œ์ ์„ ํ™•์ธํ•˜๊ธฐ์œ„ํ•ด,
System.out.println("destroy() ๋ฉ”์†Œ๋“œ")๋ฅผ System.out.println("destroy() ๋ฉ”์†Œ๋“œ!!") ์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜์—ฌ ์ €์žฅํ•˜๋ฉด, ์„œ๋ธ”๋ฆฟ์˜ ๋‚ด์šฉ์ด ๊ฐฑ์‹ ๋˜๊ณ , ์ฝ˜์†” ์ฐฝ์„ ํ™•์ธํ•ด๋ณด๋ฉด

destroy() ๋ฉ”์†Œ๋“œ

์™€ ๊ฐ™์€ ๋กœ๊ทธ๊ฐ€ ๋‚˜์˜ด์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

doGet(), doPost()

service()๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ๋ฐฉ์‹์— ์ƒ๊ด€์—†์ด ์„œ๋ธ”๋ฆฟ์„ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ์ž‘๋™ํ•˜์ง€๋งŒ, doGet()์€ get ๋ฐฉ์‹์œผ๋กœ์˜ ์š”์ฒญ์—๋งŒ, doPost()๋Š” post ๋ฐฉ์‹์œผ๋กœ์˜ ์š”์ฒญ์—๋งŒ ๋™์ž‘ํ•œ๋‹ค.
doGet(), doPost()๋Š” ๋ชจ๋‘ service() ์•ˆ์— ๋‚ด์žฅ๋˜์–ด ์žˆ๊ณ , ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•œ ์ƒํƒœ์—์„œ, ๋งž๋Š” ๋ฐฉ์‹์œผ๋กœ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•˜๋ฉด ์„œ๋ธ”๋ฆฟ์˜ service()์—์„œ doGet()๊ณผ doPost() ์ค‘ ๋งž๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰์‹œ์ผœ์ค€๋‹ค.
๋งŒ์•ฝ, doGet()์— ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ž…๋ ฅ๋ฐ›์€ ์ •๋ณด๋ฅผ post ๋ฐฉ์‹์œผ๋กœ ๊ฐ™์€ ์ฃผ์†Œ๋กœ ์ „๋‹ฌํ•ด ์ฃผ๋Š” ๊ฒƒ์„ ๊ตฌํ˜„ํ•˜๊ณ  doPost()์— ๊ทธ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์„ ๊ตฌํ˜„ํ•ด ์ค€๋‹ค๋ฉด,
ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ฒ˜์Œ์— ์ฃผ์†Œ์ฐฝ์— get ๋ฐฉ์‹์œผ๋กœ ์„œ๋ธ”๋ฆฟ๊ณผ ๋งคํ•‘๋œ ์ฃผ์†Œ๋กœ ์ ‘์†ํ•˜๋ฉด doGet()์˜ ํŽ˜์ด์ง€๊ฐ€, ํŽ˜์ด์ง€์—์„œ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์ „๋‹ฌํ•˜๋ฉด ๊ฐ™์€ ์ฃผ์†Œ ์•ˆ์—์„œ doPost()์˜ ํŽ˜์ด์ง€๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.
๋”ฐ๋ผ์„œ, doGet()๊ณผ doPost()๋ฅผ ๋ถ„๋ฆฌํ•ด์„œ ์„ค๊ณ„ํ•˜๋ฉด ํ•œ ์„œ๋ธ”๋ฆฟ์—์„œ ๋‘ ๊ฐ€์ง€ ํŽ˜์ด์ง€๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.