Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


ServletÀÇ °¢Á¾ Listener »ç¿ë¹æ¹ý ¹× »ùÇÃ
 
ServletÀÇ °¢Á¾ Listener¿¡ ´ëÇÑ ³»¿ªÀ» ¾Ë¾Æº¸°í ÇÑ°¡Áö ¸®½º³Ê¸¦ ´ë»óÀ¸·Î »ç¿ë¹æ¹ý ¹× »ùÇÃÄڵ带 º¸¿©ÁÖ°í ¼Ò½ºµµ °øÀ¯Çϵµ·Ï ÇÑ´Ù. ( 2003/03/08 ) 323
Written by ienvyou - ÃÖÁö¿õ
1 of 1
 

¢º ServletÀÇ °¢Á¾ Listener »ç¿ë¹æ¹ý ¹× »ùÇÃ

ÀÚ, À̹ø ±Û¿¡¼­´Â ¼¼¼Ç¿¡ ´ëÇÑ bindingÀÌ ÀÌ·ç¾îÁ³À»¶§ ¼­ºí¸´ ¿£ÁøÀÌ ¾î¶°ÇÑ ¹æ½ÄÀ¸·Î
ÇØ´ç ¼¼¼ÇÀ» controlÇÒ ¼ö ÀÖ´ÂÁö¿¡ ´ëÇÏ¿© ¿¹Á¦¿Í ÇÔ²² »ìÆ캸µµ·Ï ÇÏÀÚ.

ÀϹÝÀûÀ¸·Î ¼¼¼ÇÀ̶óÇÔÀº ÄíÅ°¿Í °°Àº ¼Ó¼ºÀ¸·Î ºÐ·ù°¡ µÇ¾îÁú ¼ö Àִµ¥ º¸Åë »ý°¢Çϱâ´Â
¼­¹öÃø¿¡ ÀúÀåµÇ´Â µ¥ÀÌÅÍÁ¤º¸¶ó°í µé¾úÀ» °ÍÀÌ´Ù. »ç½Ç ¾ö¹ÐÇÏ°Ô ¸»ÇÏÀÚ¸é
¼¼¼Ç¶ÇÇÑ Å¬¶óÀ̾ðÆ® ºê¶ó¿ìÁ®¿¡ ¼Ó¼ºÀ» ÀÌ¿ëÇÏ°Ô µÇ´Â °ÍÀÌÁö¸¸ ¼³¸íÇϸé
´õ º¹ÀâÇØÁö±â¸¸ Çϱ⠶§¹®¿¡ ±×³É ¼­¹ö¿¡ ÀÖ´Â ¼­¹öÀÇ ¸Þ¸ð¸®¸¸Å­ÀÇ ¿µ¿ªÀ» Â÷ÁöÇÒ 
¼ö ÀÖ´Â °´Ã¼·Î¼­¸¸ ¹Ù¶óº¸µµ·Ï ÇÏÀÚ.

¿ì¼± Enterprise APIÀÇ ¸®½º³ÊÀÇ Á¾·ù¸¦ ¸ÕÀú º¸µµ·Ï ÇÏÀÚ. ÀÌ·¯ÇÑ ¸®½º³ÊµéÀº APIÀÇ util.EventListener¸¦
ÀÌ¿ëÇÏ¿© ¿£ÁøÀÌ ½ÇÁ¦ 󸮸¦ ÇØÁÖ°Ô µÇ´Âµ¥ °£´ÜÇÏ°Ô ¼³¸íÇؼ­ ÇÊÅ͸µ °°Àº ¼­ºñ½º¸¦ ÇÑ´Ù°í 
º¸¸é ¹«¹æÇÒ µíÇÏ´Ù. ÇÊÅÍ°¡ ¹«¾ùÀÎÁö ¸ð¸¥´Ù¸é, ±×³É °£´ÜÇÏ°Ô Á¤¼ö±â¶ó°í »ý°¢ÇÏÀÚ.
¼öµµ¹°À» ±×³É ¸ÔÀ» ¼ö ¾ø±â ¶§¹®¿¡ Á¤¼ö±â¸¦ ÅëÇÏ¿© À̹°ÁúÀ» °É·¯³»´Â °Íó·³
ÀÚ¹Ù¿¡¼­µµ ¿ì¸®°¡ ½ÇÁ¦ ÄÚµùÇÑ ¼­ºí¸´ÃøÀ¸·Î requestµéÀÌ ³Ñ¾î¿À±â Àü¿¡ ¿£ÁøµéÀÌ ¹«¾ð°¡¸¦ 
°É·¯³»°Ô ÇÒ ¼ö ÀÖ´Â ¼Ó¼ºÀ» °¡Áö°í ÀÖ´Ù. ±× ´ëÇ¥ÀûÀÎ ¿¹°¡ ÇÊÅÍ, ¸®½º³ÊÀ̸ç
JDK1.2ºÎÅÍ ¼³Á¤µÈ event delegation¼Ó¼ºÀ» ÀÌ¿ëÇÏ¿© ó¸®ÇÑ´Ù.

¾Æ·¡ÀÇ ¸®½º³ÊµéÀº ¹Ýµå½Ã deployment descriptor¿¡ ¼³Á¤µÇ¾î ÀÖ¾î¾ß ÇÑ´Ù. DD°¡ ±Ã±ÝÇϸé
¼­ºí¸´ ¿£ÁøÀÇ web.xml¿¡ ¼³Á¤ÀÌ µÇ¾îÁ®¾ß ÇÑ´Ù´Â À̾߱âÀÌ´Ù.

¢º javax.servlet.ServletContextAttributeListener
ÀÌ ¸®½º³Ê´Â ÄÁÅؽºÆ®¿¡ ÀúÀåµÈ ¾ÖÆ®¸®ºäÀÇ º¯È­°¡ ÀÖ¾úÀ» ¶§ ¼³Á¤µÈ À̺¥Æ®¸¦ ¿£ÁøÀÌ ÀÚµ¿À¸·Î
¹ß»ý½ÃÅ°µµ·Ï ÇÑ´Ù. À̺¥Æ® °¨Áö¸¦ ¾ò¾î³»±â À§Çؼ­´Â ¿©·¯ºÐµéÀÌ Á÷Á¢ ÀÛ¼ºÇÑ ¸®½º³ÊÀÇ
implementionŬ·¡½º¸¦ ÅëÇØ¾ß Çϴ°ÍÀº ¹°·ÐÀ̸ç, ¾Æ·¡ÀÇ ¸®½º³Ê ¸ðµÎ µ¿ÀÏÇÏ°Ô ¼³Á¤µÈ´Ù.

¢º javax.servlet.ServletContextListener
´ç½ÅÀÌ ÀÛ¼ºÇÑ À¥¾ÖÇø®ÄÉÀ̼ǿ¡¼­ ÄÁÅؽºÆ®¸¦ ÀÌ¿ëÇÏ¿© ¹«¾ð°¡¸¦ ÀúÀåÇÏ°í ÀÌ¿ëÇÒ ¼ö Àִµ¥ 
ÀÌ ¸®½º³ÊÀÇ ÀÛµ¿Àº ±×·¯ÇÑ ÄÁÅؽºÆ®¿¡ ´ëÇÑ º¯°æÀÌ ÀÌ·ç¾îÁ³À» ¶§ ÀÚµ¿À¸·Î ¿£ÁøÀÌ °¨ÁöÇÏ°í
¿©·¯ºÐµéÀÌ ÀÛ¼ºÇÑ Å¬·¡½ºÀÇ À̺¥Æ®¸¦ ¹ß»ý½ÃŲ´Ù.

¢º javax.servlet.http.HttpSessionActivationListener 
request.getSession(true)°°Àº °æ¿ìó·³ Session¿¡ ´ëÇÑ ³»¿ëÀÌ »õ·Î »ý¼ºµÇ¾î activateµÇ¾îÁ³À» ¶§
¹ß»ýÇÏ´Â À̺¥Æ®¸¦ °¨ÁöÇÏ´Â ¸®½º³ÊÀÌ´Ù.

¢º javax.servlet.http.HttpSessionAttributeListener 
HttpSession¿¡ ´ëÇÑ ¾ÖÆ®¸®ºäÆ®ÀÇ º¯°æ½Ã ¿¬°áµÇ¾îÁö´Â ¸®½º³ÊÀÌ´Ù.

¢º javax.servlet.http.HttpSessionBindingListener 
HttpSession¿¡ ´ëÇÏ¿© Ŭ¶óÀ̾ðÆ® ¼¼¼ÇÁ¤º¸¿¡ ´ëÇÑ ¹ÙÀεùÀÌ ÀÌ·ç¾îÁ³À» °æ¿ì °¨ÁöµÇ´Â ¸®½º³ÊÀ̸ç
À̹ø ¿¹Á¦¿¡¼­ »ç¿ëµÉ ¸®½º³ÊÀ̱⵵ ÇÑ´Ù. HttpSessionBindingEvent°´Ã¼¸¦ ½ÇÁ¦ ±¸ÇöŬ·¡½º·Î 
³Ñ°ÜÁÖ°Ô µÈ´Ù.

À§ÀÇ ¸®½º³Ê ¸ðµÎ servlet2.3ºÎÅÍ ³ª¿À±â ½ÃÀÛÇßÀ¸¸ç, ÇöÀç »ç¿ëµÇ´Â °ÅÀÇ ¸ðµç ¿£ÁøµéÀÌ Áö¿øÀ»
ÇÏ°í ÀÖÀ¸¹Ç·Î »ç¿ëÇϴµ¥ ´ëÇÏ¿© ¹«¸®°¡ ¾øÀ¸¸®¶ó º»´Ù.

ÀÚ, ±×·¯¸é ÀÌÁ¦ ¿¹Á¦¸¦ Çϳª ¸¸µé¾îº¸µµ·Ï ÇÏÀÚ.
³î»õ°¡ ¸¸µé¾îº¼ ¿¹Á¦´Â HttpSessionBindingListener¶ó°í ÇÏ¿´´Ù. 
ÀÌ°ÍÀ» ¿Ö ¸¸µé¾ú´ÂÁöºÎÅÍ »ìÆ캸¾Æ¾ß ÇÏÁö ¾ÊÀ»±î? ÀÌÀ¯´Â °£´ÜÇß´Ù. ¿¹Àü ÇÁ·ÎÁ§Æ®ÀÇ °æ¿ì
Ŭ¶óÀ̾ðÆ® Á¢¼Ó¿¡ ´ëÇÏ¿© »ç¿ëÀÚÁ¤º¸¸¦ ¼¼¼Ç¿¡ ´ãµµ·Ï ÇÏ°í ÀÖ´Â ±× ´ã±ä ¼¼¼Ç¿¡ ´ëÇÏ¿©
Á¤º¸¸¦ ¾ò¾î³»·Á ÇÏ¿´´Ù. »ç¿ëÀÚ Á¤º¸ ¹× ÀúÀåµÈ ¼¼¼Ç¿¡ ´ëÇÏ¿© collection¿¡ ÇöÀç ÀÌ¿ëÁßÀÎ 
»ç¿ëÀÚ ¹× ±× »ç¿ëÀÚ Á¤º¸µéÀ» ¾îµå¹Î °ü¸®Â÷¿ø¿¡¼­ º¸°í ½Í¾ú´ø °ÍÀÌ´Ù.

°¢Á¾ ¸®½º³ÊµéÀ» ÀÌ¿ëÇÑ´Ù¸é ±×·¯ÇÑ Á¤º¸µéÀ» ½±°Ô ¾ò¾î³¾ ¼ö Àִ Ư¡À» °¡Áö°í ÀÖ¾ú´Ù.

¢º¼¼¼ÇÀÌ ¿¬°áµÈ°É °¨ÁöÇÑ´Ù¸é ²÷¾îÁö´Â °Íµµ °í°´ÀÌ ºê¶ó¿ìÁ®¸¦ ´Ý´Â ¼ø°£ °¨ÁöÇس¾¼ö Àִ°¡?

¾î¶»°Ô »ý°¢Çϴ°¡? ÀÌ·ÐÀûÀ¸·Îµµ °¡´ÉÇÒÁö ¸ð¸£Áö¸¸ ½ÇÁ¦´Â ºÒ°¡´ÉÇÏ´Ù. httpÇÁ·ÎÅäÄÝÀÇ Æ¯¼ºÀ»
¾Ë¾Æ¾ß ÇÏ°í ¸¸¾à ¼û±è ¾ÖÇø´°°Àº °ÍÀ¸·Î ºê¶ó¿ìÁ® À̺¥Æ®¸¦ °¨ÁöÇÏ¿©, ¼­¹öÃøÀ¸·Î º¸³½´Ù °¡Á¤Çصµ
´ç½ÅÀÌ Á÷Á¢ ±¸ÇöÇÒ ¼ö Àִ°¡?

±×·¯¸é ¾î¶»°Ô ÇØ¾ß ÇÒ±î? °á±¹ ¼¼¼Ç½Ã°£¸¸·á°¡ µÇ¾îÁ® µé¾î¿À´Â À̺¥Æ®¸¦ °¨ÁöÇÏ´Â ¹æ¹ýÀÌ ÀÖ°ÚÁö¸¸
±×·¸°Ô ÇÑ´Ù¸é ¼¼¼ÇŸÀӾƿôÀ» ´Ü±â°£À¸·Î ¼³Á¤À» ÇØ¾ß ÇÒÅ×°í....

À½, ¿©ÀüÈ÷ ¹®Á¦·Î ³²¾ÆÀÖ±â´Â ÇÏ´Ù. ±×°Ç ¿©·¯ºÐµéµµ Çѹø »ý°¢Çغ¸½Ãµµ·Ï ÇÏ°í.
»ç½Ç ¿¹Àü¿¡ ±¸ÇöÇß´ø°ÍÀº ÇÑ»ç¶÷ÀÌ À¥»çÀÌÆ®¿¡ Áߺ¹À¸·Î ·Î±×ÀÎÀ» ÇÏ´Â °ÍÀ» ¹æÁöÇÏ´Â °ÍÀ» ¸¸µçÀûÀº
ÀÖ´Ù. À§ÀÇ ¸®½º³Ê¸¦ ¿ä¸®Á¶¸® ÀÌ¿ëÇؼ­ ¸»ÀÌ´Ù.


¢º  ÀÚ, ÀÌÁ¦ ¿¹Á¦¸¦ ¸¸µé¾îº¸µµ·Ï ÇÏÀÚ.

´ç±Ù ½Ã³ª¸®¿ÀºÎÅÍ »ìÆìºÁ¾ß°Ú´Ù. 
1. »ç¿ëÀÚ´Â ·Î±×ÀÎÀ» ÇÑ´Ù. 
2. ·Î±×Àμ­ºí¸´ÀÌ ±× ³»¿ëÀ» ¹Þ¾Æ ¼¼¼Ç¿¡ ÀúÀåÇÑ´Ù.
3. ¸µÅ©ÆäÀÌÁö¸¦ ÅëÇØ ¸ÞÀÎÆäÀÌÁö·Î À̵¿ÇÑ´Ù.

¿ì¼± ·Î±×ÀÎÆäÀÌÁöºÎÅÍ ¸¸µéµµ·Ï ÇÏÀÚ.

<html>
<body>
<center>
<b>Login Page</b>
<hr>
<form action="/servlet/LoginServlet" method="POST">
<table border=0>
<tr><td>Name : </td><td><input type=text name=name></td></tr>
<tr><td>Password : </td><td><input type=password name=pword></td></tr>
<tr><td><input type=submit></td><td><input type=reset></td></tr>
</table>
</form>
</center>
</body>


À§¿¡¼­´Â °£´ÜÇÏ°Ô name, pword¶ó´Â input typeÀ» Á¤ÀÇÇÏ¿© ·Î±×Àμ­ºí¸´ÀÇ requets·Î ´øÁø´Ù.
¼­ºí¸´¿¡¼­ 󸮵Ǵ Äڵ带 º¸µµ·Ï ÇÏÀÚ.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet {
	private SessionListener listener;

	public void init() {
		listener = new SessionListener();
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res) 
	throws ServletException, IOException {
		res.setContentType("text/html");
		
		String name=req.getParameter("name");
		String pword=req.getParameter("pword");
		String data[]={name,pword};
		HttpSession sess=req.getSession(true);
		sess.setAttribute("Listener",listener); 
        // SessionListener¸¦ ¼¼¼Ç¿¡ ÀúÀåÇÑ´Ù.
		sess.setAttribute("user.data",data);
		PrintWriter out = res.getWriter();
		
		out.println("Login is Successed<br>");
		out.println("<a href=/servlet/LinkPage>LinkPage</a>");
		out.close();
	}
}

À§ÀÇ Äڵ忡¼­ ´«¿©°Ü º¸¾Æ¾ß ÇÒ°ÍÀº bold(±½Àº±Û¾¾)·Î ¾²¿©Áø ºÎºÐÀÌ´Ù.
»ç½Ç 3¶óÀÎÀÇ Äڵ忡¼­´Â ¸®½º³Ê 2°³°¡ µ¿ÀÛÇÏ°Ô µÈ´Ù. 
HttpSessionActivationListener, HttpSessionBindingListenerÀε¥ ±× ÁßÀÇ ÇÑ µ¿ÀÛÀÎ 
HttpSessionBindingListener¸¦ º¸µµ·Ï ÇÏÀÚ.

¿©·¯ºÐµéÀº AWT³ª SwingŬ·¡½º¸¦ ÀÛ¼ºÇÏ¿© À̺¥Æ®¸¦ ¹ß»ý½ÃÄÑ º»ÀûÀÌ Àִ°¡?
±×·¸´Ù¸é ¾Æ·¡ÀÇ Äڵ带 ÀÌÇØÇÏ´Â µ¥ ¹«¸®°¡ ¾øÀ» °ÍÀÌ°í Àß ¸ð¸£´Â ºÐÀ» À§ÇÏ¿©
ÄÚµå¾Æ·¡¿¡ ¼³¸íÀ» Çϵµ·Ï ÇÏ°Ú´Ù.

import java.util.*;
import javax.servlet.http.*;

public class SessionListener implements HttpSessionBindingListener {
/**
* ¼¼¼Ç¿¡ ´ëÇÑ °ªÀÌ bindingµÇ¾úÀ»¶§ È£ÃâµÇ´Âµ¥ ±×°ªÀº HttpSessionBindingEvent¿¡
* ÀúÀåµÇ¾î ÀÖ´Ù.
*/
	public void valueBound(HttpSessionBindingEvent event) {
		HttpSession hs = event.getSession();
		System.out.println("SessionListener.valueBound() : "+event.getName());

	}
/**
* ¼¼¼Ç¿¡ ¹ÙÀεù µÇ¾î ÀÖ´Â °ªÀÌ ¾ø¾îÁö°Ô µÉ¶§ ÀÚµ¿À¸·Î ¿£Áø¿¡ ÀÇÇÏ¿©
* È£Ã⠵Ǿî Áø´Ù.
*/
	public void valueUnbound(HttpSessionBindingEvent event) {
		HttpSession hs = event.getSession();
		System.out.println("SessionListener.valueUnbound() : "+event.getName());
	}
}

À§ÀÇ Äڵ忡¼­ ´ëü À̺¥Æ®´Â ¹«¾ùÀΰ¡? 
¾Õ¼­ delegate event ¸ðµ¨À» ¾´´Ù¶ó°í Çß´Ù.  Áï ½ÇÁ¦ µé¾î¿À´Â request¿¡ ´ëÇÑ ³»¿ëÀ»
ó¸®ÇÏ´ø µµÁß¿¡ ¿£ÁøÀº ÀڽŰú ¿¬°áµÈ °³¹ßÀÚ°¡ ÀÛ¼ºÇÑ event°¡ ÀÖ´Â Áö web.xml¿¡¼­
ã¾Æº¸°Ô µÈ´Ù. ÀÖ´Ù¶ó°í ÇÏ¸é ¿£ÁøÀº HttpSessionBindingEvent¶ó´Â °ÍÀ» ¸¸µé°Ô µÇ°í
name, session, value°ªÀ» ÀúÀåÇÏ¿© ´ç½ÅÀÌ ÀÛ¼ºÇÑ listenerŬ·¡½º·Î ´øÁö°Ô µÇ¸é
±×°ÍÀ» ó¸®ÇÏ´Â ·ÎÁ÷À» Áý¾î³Ö°Ô µÈ´Ù¸é ¿£ÁøÀº ±×¿¡ »óÀÀÇϴ ó¸®¸¦ ÇÏ°Ô µÉ°ÍÀÌ´Ù.

Áï ¸ðµç À̺¥Æ®¸¦ Á÷Á¢ ¿©·¯ºÐÀÌ Çڵ鸵ÇÏ¿© ÀÛ¼ºÇÏ´Â °ÍÀÌ ¾Æ´Ï¶ó ¿£Áø¿¡ À§ÀÓ½ÃÄ×À¸¹Ç·Î
delegationÀ̶õ ¿ë¾î¸¦ ¾´°ÍÀÌ´Ù.

ÀÚ. ±×·¯¸é ±× ¸®½º³Ê¸¦ »Ì¾Æ¼­ ¾î¶»°Ô ó¸®ÇØ¾ß Çϳª? 

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LinkPage extends HttpServlet {
	
public void doGet(HttpServletRequest req, HttpServletResponse res) 
throws ServletException,IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    HttpSession sess=req.getSession();
    String msg="";

    if(sess.isNew()) {
        msg="Error!!!. You must login.";
    } else {
        String[] data=(String[])sess.getAttribute("user.data");
        msg=data[0]+"!!, Welcome to this page.";
        // ¼¼¼Ç¸®½º³ÊÀÇ Á¤º¸¸¦ Àо´Ù.
        SessionListener listener = (SessionListener)sess.getAttribute("Listener");
    }
    
    out.println("<font size=-1 color=blue><b>"+msg+"</b></font>");
    sess.invalidate();
    out.close();
	}
}

À§¿Í °°ÀÌ Ã³¸®°¡ µÇ´Ù¸é Deployment descriptor¸¦ ÀÛ¼ºÇÏ¿©¾ß ÇÑ´Ù.
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<!-- START SESSION LISTENER --> 
   <listener>
         <listener-class>SessionListener</listener-class>
    </listener> 
    <!-- END SESSION LISTENER --> 

</web-app>

À§¿Í °°ÀÌ ¼³Á¤ÇÏ¸é °á°ú°ªÀ» Å×½ºÆ®Çغ¼ ¼ö ÀÖ°í, ¼¼¼Ç¿¡ °ªÀÌ ÀúÀåµÇ´Â
½ÃÁ¡¿¡ µ¥ÀÌÅÍ Ã³¸®ÇÏ´Â ¹æ¹ýÀ» Å×½ºÆ®ÇÒ ¼ö ÀÖÀ¸¸ç, À§¿Í °°Àº ¹æ¹ýÀ» ÄÚµùÇÒ ¼ö ÀÖ´Ù¸é
´Ù¸¥ ¸ðµç ¸®½º³ÊŬ·¡½º¿¡ ´ëÇÑ Çڵ鸵ÀÌ °¡´ÉÇØÁú °ÍÀÌ´Ù.

Àüü¼Ò½º´Â ÷ºÎµÈ ÆÄÀÏÀ» ÂüÁ¶Çϱ⠹ٶõ´Ù.

Written by Carouser : 2002-04-08
 
1
References
 
http://java.sun.com/servet
This article source zip : listener.zip
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD