서블릿의 init 메소드와 destroy메소드와 비슷하게 jsp에도 jspInit메소드와 jspDestroy메소드로 구현 할 수 있습니다.
위와 같이 DateTime.jsp 파일 하나를 구현해봅시다.
DateTime.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" import="java.io.*, java.util.*"%>
<%!
//선언문 안에 기술
private PrintWriter logFile;
public void jspInit(){
String filename = "C:\\Users\\kch\\Documents\\datelog.txt";
try{
logFile = new PrintWriter(new FileWriter(filename, true));
}catch(IOException e){
System.out.printf("%TT - %s 파일 열기 필해 %n", new GregorianCalendar(), filename);
}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
GregorianCalendar now = new GregorianCalendar();
String date = String.format("%TY년 %Tm월 %Te일", now, now, now);
String time = String.format("%TI시 %TM분 %TS초", now, now, now);
out.println(date + "<BR>");
out.println(time + "<BR>");
if(logFile != null)//로그파일에 쓰기
logFile.printf("%TF %TT에 호출됨 %n", now, now);
%>
</body>
</html>
<%!
//선언문 안에 기술
public void jspDestroy(){
if(logFile != null)
logFile.close();
}
%>
실행 화면
날짜와 시간이 잘 나옴을 알 수 있습니다.
값도 다음과 같이 들어감을 알 수 있습니다.
'프로그래밍 > JSP Servlet' 카테고리의 다른 글
JSP/Servlet - setAttribute, getAttribute, removeAttribute 메소드에 사용법 (0) | 2016.01.05 |
---|---|
JSP/Servlet - 웹애플리케이션 초기화 파라미터 (0) | 2016.01.05 |
JSP/Servlet - 서버 및 서블릿 환경 정보 가져오기(ServletContext) (0) | 2016.01.05 |
JSP/Servlet - jsp 페이지 초기화 파라미터 설정하기 (0) | 2016.01.04 |
JSP/Servlet - 서블릿 초기화 파라미터 (0) | 2016.01.04 |
JSP/Servlet - init 메소드와 destroy 메소드 사용하기 (0) | 2016.01.03 |
JSP/Servlet - 익셉션(exception) 타입별로 에러페이지 등록하기 (0) | 2016.01.02 |
JSP/Servlet - 서블릿 클래스에서 에러페이지 호출하기 (0) | 2015.12.30 |