[ servlet의 라이프사이클(lifecycle) ]
해당 jsp 페이지 요청시 servlet java 파일로 변환을 하고 컴파일 해 servlet class로 만들고 해당 객체를 생성하는데
한번 객체가 생성되고 나면 이후 다른 Client 요청은 멀티 쓰레드로 처리하게 된다.
Servlet의 라이프 사이클)
Servlet 객체 생성 - 최초 한번 생성
|
@PostConstructor 호출 - 생성자 이후, init 메서드 이전에 호출
|
init() 메서드 호출 - 최초 한번 호출
|
doGet()/doPost() 호출 - 요청시마다 호출
|
destroy() 호출 - 마지막 한번(자원 해제)
|
@PreDestroy 호출 - destroy() 이후 호출
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | package com.jsp.study; 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; @WebServlet ( "/LifeCycleServlet" ) public class LifeCycleServlet extends HttpServlet { private static final long serialVersionUID = 1L; public LifeCycleServlet() { super (); } // 생성자 이후, init() 호출 직전에 호출된다. @PostConstruct public void postConstructMethod(){ System.out.println( "PostConstruct 호출" ); } // destroy() 호출 이후에 호출된다. @PreDestroy public void preDestroyMethod(){ System.out.println( "PreDestroy 호출" ); } /* * init() * 서블릿 객체가 생성되고 나서 <최초 한번만> 호출된다. */ @Override public void init() throws ServletException { super.init(); System.out.println("init() 호출"); } /* * destroy() * 서버 종료와 같은 최후에 마지막 한번 호출된다. 자원 해제 등의 작업을 해준다. */ @Override public void destroy() { super .destroy(); System.out.println( "destroy() 호출" ); } // doGet() // 요청시마다 호출된다. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println( "doGet() 호출" ); } // doPost() // 요청시마다 호출된다. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println( "doPost() 호출" ); } } |
'스터디 > JSP' 카테고리의 다른 글
[ servlet 초기화 파라미터, 컨텍스트 파라미터, 서블릿 컨텍스트 리스너] (0) | 2017.08.08 |
---|---|
5. Servlet에서의 HTML Form태그, servlet Parameter, 한글처리 (0) | 2017.08.08 |
3. servlet의 doGet(), doPost()와 PrintWriter 객체 (0) | 2017.08.08 |
2. [ Servlet Mapping에 관하여... ] (0) | 2017.08.06 |
1. [ JSP 웹 기본 지식 및 작동 원리 ] (0) | 2017.08.06 |