본문 바로가기

Programming/Java

[Servlet] WAS 구동시 메모리 로드(ServletContextListener)

javax.servlet.ServletContextListener 인터페이스를 이용한
WAS 구동시 메모리 로드

web.xml
  
  	com.skmns.test.listener.ServiceListener
  
ServiceListener.java
package com.skmns.test.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServiceListener implements ServletContextListener {
	
	private ServletContext context = null;

	public void contextInitialized(ServletContextEvent event) {
		
		context = event.getServletContext();
		
		context.setAttribute("SERVICE_CACHE", "SERVICE_CACHE_INITIALIZED");
		
		System.out.println("=========================================");
		System.out.println("ServiceListener Initialized");
		System.out.println("=========================================");
		
	}

	public void contextDestroyed(ServletContextEvent event) {
		
		context = event.getServletContext();
		
		context.removeAttribute("SERVICE_CACHE");
		
		System.out.println("=========================================");
		System.out.println("ServiceListener Destroyed");
		System.out.println("=========================================");
	}
	
	public void contextReinitialized(HttpServletRequest request, HttpServletResponse response) {

		context = request.getSession().getServletContext();

		context.removeAttribute("SERVICE_CACHE");
		context.setAttribute("SERVICE_CACHE", "SERVICE_CACHE_REINITIALIZED");
		
		System.out.println("=========================================");
		System.out.println("ServiceListener Reinitialized");
		System.out.println("=========================================");
	}
}

ServiceListener.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="com.skmns.test.listener.ServiceListener" %>
<%
	String strMessage = "";

	strMessage = "" + request.getSession().getServletContext().getAttribute("SERVICE_CACHE");

	out.write(strMessage + "
"); // Reinitialized ServiceListener svcListener = new ServiceListener(); svcListener.contextReinitialized(request, response); strMessage = "" + request.getSession().getServletContext().getAttribute("SERVICE_CACHE"); out.write(strMessage + "
"); %>

'Programming > Java' 카테고리의 다른 글

[Interface] Google C2DM Sender  (0) 2012.08.14
[Secret] AES128 암/복호화  (0) 2012.08.14
[Servlet] HttpServletResponse를 통한 Javascript 수행  (0) 2011.10.06
[Beginner] Properties Util  (0) 2011.08.18
[Servlet] EncodingFilter  (0) 2010.03.14