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 | package com.jsp.method; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/L03LifeCycle") public class L03LifeCycle extends HttpServlet { private static final long serialVersionUID = 1L; int initCount = 1; //init()함수 호출 시 증가 int serviceCount = 1; //service()함수 호출 시 증가 int doGetCount = 1; //doGet()함수 호출 시 증가 int destroyCount = 1; //destroy()함수 호출 시 증가 @Override public void init() throws ServletException { System.out.println("init()호출"+(initCount++)); //서버가 시작되고 페이지를 최초 호출할 때 한 번 호출된다. } @Override public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { System.out.println("service()호출"+(serviceCount++)); //doGet의 요청을 빼돌려서 매번 호출된다. } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("doGet()Call"+(doGetCount++)); //service가 있으면 요청을 빼앗긴다. } @Override public void destroy() { System.out.println("destroy()호출"+(destroyCount++)); //서버가 종료될 때 한 번 호출된다. //서버를 강제종료하면 안되고 페이지를 수정하고 저장하면 서버를 잠시 종료 했다가 다시 실행. } } |
L03ServletMethod 프로젝트의 src/com.jsp.method 아래에
L03LifeCycle 서블릿을 생성한다.
위 예제는 페이지가 호출될 때 어떠한 함수가 호출되는지 확인하기 위한 예제이다.
서버가 시작되고 페이지가 최초 호출될 때 init()함수가 호출된다.
최초 호출과 이후 새로고침, 다른 브라우저에서의 호출이 있을 경우 doGet()함수가 호출된다.
그러나 이 때, service()함수가 있다면 doGet()함수는 요청을 빼앗기며, service()함수가 호출된다.
서버가 종료될 때에는 destroy()함수가 호출된다.
'JSP > 기본다지기' 카테고리의 다른 글
JSP 4일차 필기 (0) | 2016.10.21 |
---|---|
JSP 3일차 필기 (jsp 데이터타입, 임포트) (0) | 2016.10.20 |
JSP 3일차 필기 (간단한 회원가입 양식) (0) | 2016.10.20 |
JSP 2일차 필기 (get방식, post방식으로 호출하기) (0) | 2016.10.19 |
JSP 2일차 필기 (JSP 들어가기) (0) | 2016.10.19 |