본문 바로가기

웹 프로그래밍/Back-End

Servlet 라이프사이클(생명주기), 선처리와 후처리

반응형

Servlet 라이프사이클 ( 생명주기 ), 선처리와 후처리

1. Servlet 객체 생성 -> 최초 한번

▶ 선처리 : @PostConstruct

2. Init() 호출 -> 최초 한번

3. service(), doGet(), doPost() 호출 -> 요청시 매번

4. destroy() 호출 -> 마지막 한번 ( 자원 해제 : servlet 수정, 서버 재가동 ... )

▶ 후처리 : @PreDestroy

package com.javalec.ex;

import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LifeCycleEx
 */
 
@WebServlet("/LC")
public class LifeCycleEx extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LifeCycleEx() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("doGet");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("doPost");
	}
	
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("Init");
	}
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("Destroy");
	} 

	@PostConstruct
	private void initPostConstruct() {
		// TODO Auto-generated method stub
		System.out.println("initPostContruct");
	}
	
	@PreDestroy
	private void destroyPreDestroy() {
		// TODO Auto-generated method stub
		System.out.println("destroyPreDestroy");
	}
}
반응형

'웹 프로그래밍 > Back-End' 카테고리의 다른 글

Django - SQL  (0) 2020.09.10
Django - CRUD  (0) 2020.09.03
Django 기초  (0) 2020.08.28
Servlet Parameter 와 한글처리  (0) 2020.03.11
web.xml 에 서블릿 맵핑  (0) 2019.12.08