1 / 26

임지희 ambomis@kunsan.ac.kr 군산대학교 통계컴퓨터과학과 정보과학기술 연구실 2012.07.31

J S P CH6 . 서블릿의 라이프 사이클. 임지희 ambomis@kunsan.ac.kr 군산대학교 통계컴퓨터과학과 정보과학기술 연구실 2012.07.31. 목차. 서블릿의 라이프 사이클 서블릿 클래스의 init 메서드와 destroy 메서드 JSP 페이지의 jspInit 메서드와 jspDestroy 메서드 서블릿 환경을 표현하는 ServerContext 객체. IST (Information Sciences & Technology) Laboratory. 서블릿의 라이프 사이클.

ingrid-fry
Download Presentation

임지희 ambomis@kunsan.ac.kr 군산대학교 통계컴퓨터과학과 정보과학기술 연구실 2012.07.31

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. J S P CH6 . 서블릿의 라이프 사이클 임지희 ambomis@kunsan.ac.kr 군산대학교 통계컴퓨터과학과 정보과학기술 연구실 2012.07.31

  2. 목차 • 서블릿의 라이프 사이클 • 서블릿 클래스의 init 메서드와destroy 메서드 • JSP 페이지의 jspInit메서드와jspDestroy메서드 • 서블릿 환경을 표현하는 ServerContext객체 IST (Information Sciences & Technology) Laboratory

  3. 서블릿의 라이프 사이클 웹 컨테이너 서블릿 클래스 로드 서블릿 클래스 ①인스턴스화 서블릿 객체 ② 초기화 작업 웹 브라우저로부터의 호출 처리 서블릿 ③ 마무리 작업 더 이상 사용되지 않는 서블릿 제거 IST (Information Sciences & Technology) Laboratory

  4. 서블릿 클래스의 init 메서드와destroy 메서드 • 서블릿 클래스의 init메서드: 서블릿의 초기화 작업이 수행 될 때 자동으로 호출 destroy메서드: 서블릿의마무리 작업이 수행될 때 자동으로 호출 • Init 메서드의 작성 방법 public void init() throws ServletException { } IST (Information Sciences & Technology) Laboratory

  5. 서블릿 클래스의 init 메서드와destroy 메서드 • Init 메서드의 작성 방법 WEB-INF/calsses에 컴파일 결과 저장 WEB-INF에 web.xml 파일 저장 웹 컨테이너가 시작될 때 서블릿이 초기화 되도록 만드는 엘리먼트 Web.xml http://localhost:8080/brain06/fibonacci?NUM=15 FibonacciServlet.java IST (Information Sciences & Technology) Laboratory

  6. 서블릿 클래스의 init 메서드와destroy 메서드 • destroy 메서드의 작성 방법 public void destroy() { } IST (Information Sciences & Technology) Laboratory

  7. 서블릿 클래스의 init 메서드와destroy 메서드 • destroy 메서드의 작성 방법 YourName.html 여러번실행 C:\ data 톰캣 종료 web.xml GreetingServlet.java

  8. 서블릿 클래스의 init 메서드와destroy 메서드 • 서블릿의 초기화 파라미터 • 서블릿의 초기화 파라미터란? 서블릿의 초기화 작업에 필요한 데이터 • Web.xml에 기재 • 두 개 이상 초기화 파라미터를 넘겨주려면 <init-parm> 엘리먼트를 여러 개 씀 • 초기화 파라미터값 가져오기 : getInitParameter() <servlet> <servlet-name>agreement-servlet</servlet-name> <servlet-class>AgreementServlet</servlet-class> <init-param> <param-name> FILE_NAME</param-name> <param-value>agreement.txt</param-valuee> </init-param> </servlet> 파라미터 이름 파라미터 값 서블릿의 초기화 파라미터 String filename = getInitParameter(“FILE_NAME”) ; IST (Information Sciences & Technology) Laboratory

  9. 서블릿 클래스의 init 메서드와destroy 메서드 • 서블릿의 초기화 파라미터 로그파일 열기 파라미터 이름 파라미터 값 web.xml 로그파일 기록 로그파일 닫기 GreetingServlet.java IST (Information Sciences & Technology) Laboratory

  10. JSP 페이지의 jspInit메서드와jspDestroy메서드 • jspInit메서드와jspDestroy메서드의 작성 방법 • jspInit() • jspDestroy() public void jspInit() { } public void jspDestroy() { } IST (Information Sciences & Technology) Laboratory

  11. JSP 페이지의 jspInit메서드와jspDestroy메서드 • jspInit메서드와jspDestroy메서드의 작성 방법 로그파일 열기 로그파일 기록 로그파일 닫기 DateTime.jsp IST (Information Sciences & Technology) Laboratory

  12. JSP 페이지의 jspInit메서드와jspDestroy메서드 • jspInit페이지의 초기화 파라미터 • Web.xml에 기재 <web-app> <servlet> <servlet-name>winners-jsp</servlet-name> <jsp-file>/winners.jsp</jsp-file> </servlet> <init-param> <param-name>FILE_NAME</param-name> <param-value>winner-list.txt</param-value> </ init-param > <servlet-mapping> <servlet-name>winners-jsp</servlet-name> <url-pattern>/winners</url-pattern> </servlet-mapping> </web-app> JSP 페이지가 있는 URL 경로명 초기화 파라미터를 지정하는 엘리먼트 JSP 페이지에 부여할 새로운 URL String filename = getInitParameter(“FILE_NAME”) ; IST (Information Sciences & Technology) Laboratory

  13. JSP 페이지의 jspInit메서드와jspDestroy메서드 • jspInit페이지의 초기화 파라미터 URL-pattern 에 기술한 URL경로명 DBName.jsp JSP페이지 초기화 파라미터 Web.xml IST (Information Sciences & Technology) Laboratory

  14. 서블릿의 환경을 표현하는 ServletContext객체 • 서블릿의 환경 정보를 가져오는 방법 • Javax.servlet.ServletContext의 인테페이스 타입의 객체를 이용해 관련 정보를 얻음 • 객체를 구하기 위한 메서드: getServletContext • 리턴한객체를 이용한 메서드 호출 예 ServletContext context = getServletContext(); ServletContext객체를 리턴하는메서드 String str = context.getServerInfo(); 웹서버의종류 를리턴하는메서드 Int num1 = context.getMajorVersion(); 서블릿 메이저 버전을 가져오는 메서드 Int num2 = context.getMinorVersion(); 서블릿 마이너버전을 가져오는 메서드 IST (Information Sciences & Technology) Laboratory

  15. 서블릿의 환경을 표현하는 ServletContext객체 • 서블릿의 환경 정보를 가져오는 방법 context 객체 리턴 메서드 호출 ServerInfoServlet.java JSP 페이지의 application 내장변수를 이용 Application 내장변수에는 getContext메서드의 호출과 동일한 객체가 들어있어 간단해 진다 !! IST (Information Sciences & Technology) Laboratory ServerInfo.jsp

  16. 서블릿의 환경을 표현하는 ServletContext객체 • 웹 애플리케이션의초기화파라미터 값을 가져오는 getInitParameter • 웹애플리케이션 전체에 속하는 초기화 파라미터 Web.xml 파일이 속한 웹 애플리케이션 디렉터리의 모든 웹 컴포넌트에서 읽을 수 있음 • 서블릿 클래스 안에서 읽는 방법 getServletContext메서드를 통해 객체를 구한 뒤, 그 객체에 대해 getInitParameter메서드 호출 • JSP 페이지 안에서 읽는 방법 application 내장변수에 대해 getInitParameter메서드 호출 <web-app … > <context-param> <param-name>DB_NAME</param-name> <param-value>malldb</param-value> </context-param> </web-app> String str = application.getInitParameter(“DB_NAME”) ; IST (Information Sciences & Technology) Laboratory

  17. 서블릿의 환경을 표현하는 ServletContext객체 • 웹 애플리케이션의초기화파라미터 값을 가져오는 getInitParameter Web.xml AppParamTest.jsp IST (Information Sciences & Technology) Laboratory

  18. 서블릿의 환경을 표현하는 ServletContext객체 • 로그 메시지를 기록하는 log 메서드 로그 파일을 열고 닫는 코드를 작성하지 않고도 바로 로그 메시지를 기록할 수 있어 편리 • log 메서드 호출 log메서드는파라미터로 넘겨준 메시지를 톰캣의 설치 디렉터리 아래에 있는 log라는 이름의 서브디렉터리 안에 있는 localhost.yyyy-mm-dd.log라는 이름의 파일에 기록함 application.log(“인사하기 JSP 페이지가 호출되었습니다.”) ; IST (Information Sciences & Technology) Laboratory

  19. 서블릿의 환경을 표현하는 ServletContext객체 • 로그 메시지를 기록하는 log 메서드 Hello.jsp http://localhost:8080/brain06/Hello.jsp?NAME=임지희 IST (Information Sciences & Technology) Laboratory

  20. 서블릿의 환경을 표현하는 ServletContext객체 • 같은 웹 애플리케이션에 속하는 웹 컴포넌트들끼리 데이터를 주고 받는 방법 • 메서드 setAttribute : 웹애플리케이션에 할당된 공유 데이터 영역에 데이터를 저장 getAttribute : 그 영역에 있는 데이터를 읽어옴 removeAttribute : 그 영역의 데이터를 삭제 application.setAttribute(“ID”, “lee77”) ; String str = (String) application.getAttribute(“ID”) ; application.removeAttribute(“ID”) ; IST (Information Sciences & Technology) Laboratory

  21. 서블릿의 환경을 표현하는 ServletContext객체 • 같은 웹 애플리케이션에 속하는 웹 컴포넌트들끼리 데이터를 주고 받는 방법 http://localhost:8080/brain06/StoreName.jsp?NAME=jihui 데이터 저장 http://localhost:8080/brain06/ReadName.jsp StoreName.jsp http://localhost:8080/brain06/DeleteName.jsp 데이터 읽어옴 ReadName.jsp 데이터 삭제 http://localhost:8080/brain06/ReadName.jsp DeleteName.jsp IST (Information Sciences & Technology) Laboratory

  22. 서블릿의 환경을 표현하는 ServletContext객체 • 로그 웹 애플리케이션에 관련된 파일 경로명을 가져오는 메서드 • 메서드 getContextPath : 웹 애플리케이션의 URL 경로명 리턴 getRealPath : 웹 애플리케이션 디렉터리 내에 있는 파일의 경로명을 파일시스템 전체에 대한 절대 경로명으로 바꾸어서 리턴 String appPath = application.getContextPath() ; String absolutePath = application.getRealPath(“/sub1/Intro.html”) ; IST (Information Sciences & Technology) Laboratory

  23. END 감사합니다 임지희 ambomis@kunsan.ac.kr

More Related