Servlet & JSP – 13 – Listener

Merhaba Arkadaslar, Bu bolumde listener’lari inceleyecegiz. Bu listenerlar ;

javax.servlet.ServletContextListener

public void contextInitialized(ServletContextEvent sce);
public void contextDestroyed(ServletContextEvent sce);
javax.servlet.ServletContextAttributeListener

public void attributeAdded(ServletContextAttributeEvent scae);
public void attributeRemoved(ServletContextAttributeEvent scae);
public void attributeReplaced(ServletContextAttributeEvent scae);
javax.servlet.ServletRequestListener

public void requestDestroyed (ServletRequestEvent sre);
public void requestInitialized (ServletRequestEvent sre);
javax.servlet.ServletRequestAttributeListener

public void attributeAdded(ServletRequestAttributeEvent srae);
public void attributeRemoved(ServletRequestAttributeEvent srae);
public void attributeReplaced(ServletRequestAttributeEvent srae);
javax.servlet.http.HttpSessionListener

public void sessionCreated(HttpSessionEvent se);
public void sessionDestroyed(HttpSessionEvent se);
javax.servlet.http.HttpSessionAttributeListener

public void attributeAdded(HttpSessionBindingEvent se);
public void attributeRemoved(HttpSessionBindingEvent se);
public void attributeReplaced(HttpSessionBindingEvent se);
javax.servlet.http.HttpSessionActivationListener

public void sessionWillPassivate(HttpSessionEvent se); 
public void sessionDidActivate(HttpSessionEvent se);
javax.servlet.http.HttpSessionBindingListener

public void valueBound(HttpSessionBindingEvent event);
public void valueUnbound(HttpSessionBindingEvent event);

javax.servlet.ServletContextListener

ServletContextListener arabiriminde contextInitialized ve contextDestroyed metotlari bulunmaktadir. Bu metotlar  javax.servlet.ServletContextEvent tipinde parametre alirlar.

Projemiz ayaga kaltiginda(deploy) contextInitialized metodu calisacaktir. Proje tekrar deploy olmadan once contextDestroyed metodu calisacaktir. (Ornegin ; bir Servlet uzerinde degisiklik yaptigimizda projemiz tekrar deploy olacaktir , bu durumda contextDestroyed metodunun calistigini gorebiliriz.)

Birden fazla ServletContextListener tanimlandiginda web.xml de tanimladigimiz sirada calisacaktir. Bu ozellik Servlet 3.0 ile gelmistir , oncesinde ise random sekilde calismaktadir.

_08_Listener.context
ServletContextListenerImpl.java
ServletContextListenerImpl2.java
ServletContextListenerImpl3.java

ServletContextListenerImpl.java

package _08_Listener.context;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ServletContextListenerImpl implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("context initialized ...");
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("context contextDestroyed ...");
	}

}

ServletContextListenerImpl2.java

package _08_Listener.context;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ServletContextListenerImpl2 implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("context initialized 2 ...");
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("context contextDestroyed 2...");
	}

}

ServletContextListenerImpl3.java

package _08_Listener.context;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ServletContextListenerImpl3 implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("context initialized 3...");
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("context contextDestroyed 3...");
	}

}

listener tanimini web.xml ‘e eklerken etiketini kullaniriz;

 <listener>
      <listener-class>_08_Listener.context.ServletContextListenerImpl</listener-class>
 </listener>

 <listener>
      <listener-class>_08_Listener.context.ServletContextListenerImpl2</listener-class>
 </listener>

 <listener>
      <listener-class>_08_Listener.context.ServletContextListenerImpl3</listener-class>
 </listener>

javax.servlet.ServletContextAttributeListener

ServletContextAttributeListener arabiriminde attributeAdded , attributeRemoved , attributeReplaced metotlari yer almaktadir.
Bu metotlar javax.servlet.ServletContextAttributeEvent tipinde parametre almaktadirlar. ServletContextAttributeEvent sinifi ServletContextEvent sinifini kalitmaktadir.

Bu metotlar Context attribute eklendiginde, silindiginde , ya de degistirildiginde/override edildiginde calisacaktir.

_08_Listener.context.attribute
ServletContextAttributeListenerImpl.java
SCAttributeListenMe.java

ServletContextAttributeListenerImpl.java

package _08_Listener.context.attribute;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

public class ServletContextAttributeListenerImpl implements ServletContextAttributeListener {

	@Override
	public void attributeAdded(ServletContextAttributeEvent scae) {

		System.out.println("ServletContext attributeAdded : " + scae.getName() + " " + scae.getValue());

	}

	@Override
	public void attributeRemoved(ServletContextAttributeEvent scae) {
		System.out.println("ServletContext attributeRemoved : " + scae.getName() + " " + scae.getValue());
	}

	@Override
	public void attributeReplaced(ServletContextAttributeEvent scae) {
		System.out.println("ServletContext attributeReplaced : " + scae.getName() + " " + scae.getValue());
	}

}

SCAttributeListenMe.java

package _08_Listener.context.attribute;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SCAttributeListenMe extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ServletContext context = getServletContext();
		
		context.setAttribute("context attribute", "my context attribute");
		
		context.setAttribute("context attribute", "my new context attribute");
		
		context.removeAttribute("context attribute");
		
	}
}

web.xml dosyamiza listener’i ve servlet’imizi ekleyelim ;

 <listener>
      <listener-class>_08_Listener.context.attribute.ServletContextAttributeListenerImpl</listener-class>
 </listener>

 <servlet>
      <servlet-name>SCAttributeListenMe</servlet-name>
      <servlet-class>_08_Listener.context.attribute.SCAttributeListenMe</servlet-class>
 </servlet>

 <servlet-mapping>
      <servlet-name>SCAttributeListenMe</servlet-name>
      <url-pattern>/scattributelistenMe</url-pattern>
 </servlet-mapping>

SCAttributeListenMe servlet’imizi calistirdigimizda context’e attribute ekledigimizde attributeAdded metodu calisacaktir. Daha sonrasinda bu attribute’u override ettigimizde attributeReplaced metodu calisir. Son olarak da bu attribute’u sildigimizde attributeRemoved metodu calisacaktir.

javax.servlet.ServletRequestListener

ServletRequestListener , requestInitialized ve requestDestroyed metotlarina sahiptir. |
Bu metotlar javax.servlet.ServletRequestEvent tipinde parametre almaktadir.
Web application’a bir istek/request geldiginde requestInitialized metodu calisacaktir. Bu istek sonlandiginda requestDestroyed metodu calisacaktir.

_08_Listener.request
ServletRequestListenerImpl.java
ServletRequestListenMe.java

ServletRequestListenerImpl.java

package _08_Listener.request;

import java.util.Date;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;

public class ServletRequestListenerImpl implements ServletRequestListener {

	@Override
	public void requestInitialized(ServletRequestEvent sre) {

		System.out.println("requestInitialized...");

		HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();

		String servletPath = request.getServletPath();
		Date date = new Date();

		System.out.println(servletPath + " " +date);

	}

	@Override
	public void requestDestroyed(ServletRequestEvent sre) {
		System.out.println("requestDestroyed...");
	}

}

ServletRequestListenMe.java

package _08_Listener.request;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletRequestListenMe extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("Check Eclipse Console!");
	}
}

web.xml dosyamiza listener ve servlet siniflarimizi ekleyelim;

 <listener>
      <listener-class>_08_Listener.request.ServletRequestListenerImpl</listener-class>
 </listener>


 <servlet>
      <servlet-name>RequestListenMe</servlet-name>
      <servlet-class>_08_Listener.request.ServletRequestListenMe</servlet-class>
 </servlet>

 <servlet-mapping>
      <servlet-name>RequestListenMe</servlet-name>
      <url-pattern>/requestlistenme</url-pattern>
 </servlet-mapping>

javax.servlet.ServletRequestAttributeListener

ServletRequestAttributeListener da ServletContextAttributeListener gibi calisir , benzer sekilde attributeAdded , attributeRemoved , attributeReplaced metotlarina sahiptir.
Bu metotlar  ServletRequestAttributeEvent tipinde parameter almaktadir. ServletRequestAttributeEvent sinifi ServletRequestEvent sinifini kalitmaktadir.

_08_Listener.request.attribute
ServletRequestAttributeListenerImpl.java
ServletRequestAttributeListenMe.java

ServletRequestAttributeListenerImpl.java

package _08_Listener.request.attribute;

import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;

public class ServletRequestAttributeListenerImpl implements ServletRequestAttributeListener {

	@Override
	public void attributeAdded(ServletRequestAttributeEvent srae) {
		System.out.println("request attributeAdded :" + srae.getName() + " " + srae.getValue());
	}

	@Override
	public void attributeRemoved(ServletRequestAttributeEvent srae) {
		System.out.println("request attributeRemoved :" + srae.getName() + " " + srae.getValue());
	}

	@Override
	public void attributeReplaced(ServletRequestAttributeEvent srae) {
		System.out.println("request attributeReplaced :" + srae.getName() + " " + srae.getValue());
	}

}

ServletRequestAttributeListenMe.java

package _08_Listener.request.attribute;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletRequestAttributeListenMe extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		req.setAttribute("name", "myname attribute");
		req.setAttribute("name", "new myname attribute");
		req.removeAttribute("name");
	}
}

web.xml dosyamiza listener ve servlet tanimimizi ekleyelim ;

 
 <listener>
      <listener-class>_08_Listener.request.attribute.ServletRequestAttributeListenerImpl</listener-class>
 </listener>
 
 <servlet>
      <servlet-name>RequestAttributeListenMe</servlet-name>
      <servlet-class>_08_Listener.request.attribute.ServletRequestAttributeListenMe</servlet-class>
 </servlet>

 <servlet-mapping>
      <servlet-name>RequestAttributeListenMe</servlet-name>
      <url-pattern>/requestattributelistenme</url-pattern>
 </servlet-mapping>

ServletRequestAttributeListenMe servlet’imizi calistirdigimizda once requestInitialized metodu calisir , daha sonrasinda attributeAdded metodu calisir. Request attribute override edildigi icin attributeReplaced metodu calisir , daha sonrasinda bu request attribute silindigi icin attributeRemoved metodu calisir. Son olarak da requestDestroyed metodu calisacaktir.

javax.servlet.http.HttpSessionListener

HttpSessionListener , sessionCreated ve sessionDestroyed metotlarina sahiptir. Bu metotlar yeni session olusturuldugunda ve bu session yok oldugunda calisacaktir.
Bu metotlar javax.servlet.http.HttpSessionEvent tipinde parametre almaktadir.
Yeni bir session olusturmak icin getSession metodu kullanilmaktadir.

_08_Listener.session
HttpSessionListenerImpl.java
HttpSessionListenMe.java

HttpSessionListenerImpl.java

package _08_Listener.session;

import java.util.Date;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class HttpSessionListenerImpl implements HttpSessionListener {

	@Override
	public void sessionCreated(HttpSessionEvent se) {

		System.out.println("sessionCreated .... " + new Date());
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {

		System.out.println("sessionDestroyed ... " + new Date());
	}

}

HttpSessionListenMe.java

package _08_Listener.session;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class HttpSessionListenMe extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
		HttpSession session = req.getSession();
	
	}
}

web.xml dosyamiza listener ve servlet tanimini ekleyelim;

 <listener>
      <listener-class>_08_Listener.session.HttpSessionListenerImpl</listener-class>
 </listener>

 <servlet>
      <servlet-name>HttpSessionListenMe</servlet-name>
      <servlet-class>_08_Listener.session.HttpSessionListenMe</servlet-class>
 </servlet>

 <servlet-mapping>
      <servlet-name>HttpSessionListenMe</servlet-name>
      <url-pattern>/httpsessionlistenme</url-pattern>
 </servlet-mapping>

HttpSessionListenMe servletimizi calistiralim eclipse console’u kontrol edelim.

javax.servlet.http.HttpSessionAttributeListener

HttpSessionAttributeListener da attributeAdded , attributeReplaced , attributeRemoved metotlarina sahiptir. Bu metotlarin javax.servlet.http.HttpSessionBindingEvent tipinde parametre alirlar.

_08_Listener.session.attribute
HttpSessionAttributeListenerImpl.java
HttpSessionAttributeListenMe.java

HttpSessionAttributeListenerImpl.java

package _08_Listener.session.attribute;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class HttpSessionAttributeListenerImpl implements HttpSessionAttributeListener {

	@Override
	public void attributeAdded(HttpSessionBindingEvent se) {
		System.out.println("attributeAdded : " + se.getName() + " " + se.getValue());
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent se) {
		System.out.println("attributeRemoved : " + se.getName() + " " + se.getValue());
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent se) {
		System.out.println("attributeReplaced : " + se.getName() + " " + se.getValue());

	}

}

HttpSessionAttributeListenMe.java

package _08_Listener.session.attribute;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class HttpSessionAttributeListenMe extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();

		session.setAttribute("session attribute", "my attribute");
		session.setAttribute("session attribute", "my new attribute");
		session.removeAttribute("session attribute");
	}

}

web.xml dosyamiza listener ve servlet’imizi ekleyelim;

 <servlet>
     <servlet-name>HttpSessionAttributeListenMe</servlet-name>
     <servlet-class>_08_Listener.session.attribute.HttpSessionAttributeListenMe</servlet-class>
 </servlet>

 <servlet-mapping>
     <servlet-name>HttpSessionAttributeListenMe</servlet-name>
     <url-pattern>/httpsessionattributelistenme</url-pattern>
 </servlet-mapping>

HttpSessionAttributeListenMe sinifimizi calistirdigimizda Eclipse Console’da ciktilari inceleyebiliriz.

javax.servlet.http.HttpSessionBindingListener

HttpSessionBindingListener , valueBound ve valueUnbound metotlarina sahiptir. Bu metotlar javax.servlet.http.HttpSessionBindingEvent tipinde parametre almaktadir.

_08_Listener.session.binding
HttpSessionBindingListenerImpl.java
HttpSessionBindingListenMe.java

Session’a HttpSessionBindingListener tipinde bir attribute eklendiginde valueBound metodu , HttpSessionBindingListener tipinde attribute sessiondan silindiginde valueUnbound metodu calisacaktir.

HttpSessionBindingListenerImpl.java

package _08_Listener.session.binding;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class HttpSessionBindingListenerImpl implements HttpSessionBindingListener {

	@Override
	public void valueBound(HttpSessionBindingEvent event) {

		System.out.println("valueBound HttpSessionBindingListenerImpl... ");
	}

	@Override
	public void valueUnbound(HttpSessionBindingEvent event) {
		System.out.println("valueUnbound HttpSessionBindingListenerImpl... ");
	}

}

HttpSessionBindingListenMe.java

package _08_Listener.session.binding;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class HttpSessionBindingListenMe extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		HttpSession session = req.getSession();

		HttpSessionBindingListenerImpl attribute = new HttpSessionBindingListenerImpl();
		session.setAttribute("binding attribute", attribute);

		session.removeAttribute("binding attribute");
	}
}

session attribute olarak HttpSessionBindingListenerImpl objesini(HttpSessionBindingListenerImpl IS-A HttpSessionBindingListener ) ekledik. Bu nedenle valueBound metodu calisacaktir.
removeAttribute ile ilgili attribute silindigin icin valueUnbound metodu calisacaktir.

HttpSessionBindingListener’i web.xml e eklememize gerek yoktur! Sadece servlet’imizi eklememiz yeterli olacaktir.

 <!--HttpSessionBindingListener i listener olarak eklemeye gerek yoktur. 
 Dilersek yazabiliriz. -->
 <listener>
     <listener-class>_08_Listener.session.binding.HttpSessionBindingListenerImpl</listener-class>
 </listener>

 <servlet>
     <servlet-name>HttpSessionBindingListenMe</servlet-name>
     <servlet-class>_08_Listener.session.binding.HttpSessionBindingListenMe</servlet-class>
 </servlet>

 <servlet-mapping>
     <servlet-name>HttpSessionBindingListenMe</servlet-name>
     <url-pattern>/httpsessionbindinglistenme</url-pattern>
 </servlet-mapping>

javax.servlet.http.HttpSessionActivationListener

HttpSessionActivationListener , sessionWillPassivate ve sessionDidActivate metotlarina sahiptir. Bu metotlar javax.servlet.http.HttpSessionEvent tipinde parametre alirlar.

Uygulamamizin dagitik/distributed bir sekilde calistigini dusunelim. Her serverda farkli JVM olacaktir. Her JVM icin bir ServletContext objesi ve yine her JVM deki Servletlere karsilik ServletConfig objesi olacaktir. Sadece HttpSession objesi bir tane olacaktir ! Yani ayni session objesinin kopyasi farkli JVM’lerde olamaz! Bununla birlikte bu HttpSession objesi bir JVMden diger JVM yani bir dagitik sistemden diger dagitik sisteme gecebilir.
HttpSessionActivationListener arabirimindeki metotlar bu HttpSession objesini farkli JVMler arasinda gecisi/tasinmasi sonrasinda calisacaktir.

package _08_Listener.session.activation;

import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;

public class HttpSessionActivationListenerImpl implements HttpSessionActivationListener {

	@Override
	public void sessionWillPassivate(HttpSessionEvent se) {
		System.out.println("sessionWillPassivate ...");
	}

	@Override
	public void sessionDidActivate(HttpSessionEvent se) {
		System.out.println("sessionDidActivate ...");
	}

}

web.xml dosyamiza listener’i ekleyelim ;

 <listener>
     <listener-class>_08_Listener.session.activation.HttpSessionActivationListenerImpl</listener-class>
 </listener>

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6, Web Component Developer

Print Friendly

Leave a Reply

Your email address will not be published. Required fields are marked *