{"id":2300,"date":"2017-02-27T11:15:43","date_gmt":"2017-02-27T09:15:43","guid":{"rendered":"http:\/\/hasselba.ch\/blog\/?p=2300"},"modified":"2017-02-27T18:55:26","modified_gmt":"2017-02-27T16:55:26","slug":"domino-rest-listeners-for-initialization-destroying-of-a-servlet","status":"publish","type":"post","link":"https:\/\/hasselba.ch\/blog\/?p=2300","title":{"rendered":"Domino &#038; REST: Listeners for Initialization &#038; Destroying of a Servlet"},"content":{"rendered":"<p>If you need to know when your Servlet is initialized or destroyed, you can use a <em>ServletContextListener<\/em> in your application.<\/p>\n<p>First, create the class <em>AppServletContextListener<\/em> and implement the <em>javax.servlet.ServletContextListener <\/em>interface<em>.<\/em> This provides two methods for capturing the events: <em>contextInitialized<\/em> and <em>contextDestroyed:<\/em><\/p>\n<pre><code>package ch.hasselba.servlet;\r\n\r\nimport javax.servlet.ServletContextEvent;\r\nimport javax.servlet.ServletContextListener;\r\n\r\npublic class AppServletContextListener\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 implements ServletContextListener{\r\n\r\n\u00a0\u00a0 \u00a0@Override\r\n\u00a0\u00a0 \u00a0public void contextDestroyed(ServletContextEvent event) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"ServletContextListener destroyed\");\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0@Override\r\n\u00a0\u00a0 \u00a0public void contextInitialized(ServletContextEvent event) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"ServletContextListener started\");\r\n\u00a0\u00a0 \u00a0}\r\n}<\/code><\/pre>\n<p>The <a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/servlet\/ServletContextEvent.html\" target=\"_blank\"><em>ServletContextEvent<\/em><\/a> allows you to access the <a href=\"http:\/\/docs.oracle.com\/javaee\/6\/api\/javax\/servlet\/ServletContext.html\" target=\"_blank\"><em>ServletContext<\/em><\/a> if required.<\/p>\n<p>Now you have to register the listener in your <em>web.xml<\/em>:<\/p>\n<pre><code>&lt;web-app&gt;\r\n\u00a0\u00a0\u00a0\u00a0 &lt;listener&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;listener-class&gt;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 ch.hasselba.servlet.AppServletContextListener\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/listener-class&gt;\r\n\u00a0\u00a0\u00a0 &lt;\/listener&gt;\r\n&lt;\/web-app&gt;<\/code><\/pre>\n<p>As soon as you start or stop the HTTP task, the Listener is called, and you can execute some application specific code (i.e. shutdown threads, bring out the garbage, etc.)<\/p>\n<p><a href=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2017\/02\/screenshot-startup.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2302\" src=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2017\/02\/screenshot-startup.png\" alt=\"\" width=\"680\" height=\"224\" srcset=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2017\/02\/screenshot-startup.png 680w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2017\/02\/screenshot-startup-300x99.png 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/a><a href=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2017\/02\/screenshot-shutdown.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2301\" src=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2017\/02\/screenshot-shutdown.png\" alt=\"\" width=\"681\" height=\"149\" srcset=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2017\/02\/screenshot-shutdown.png 681w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2017\/02\/screenshot-shutdown-300x66.png 300w\" sizes=\"auto, (max-width: 681px) 100vw, 681px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you need to know when your Servlet is initialized or destroyed, you can use a ServletContextListener in your application. First, create the class AppServletContextListener and implement the javax.servlet.ServletContextListener interface. This provides two methods for capturing the events: contextInitialized and &hellip; <a href=\"https:\/\/hasselba.ch\/blog\/?p=2300\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89,116,61,82],"tags":[118,7,117,60,119,12],"class_list":["post-2300","post","type-post","status-publish","format-standard","hentry","category-java","category-jee","category-rest","category-server","tag-apache-wink","tag-domino","tag-jee","tag-rest","tag-servlet","tag-web"],"_links":{"self":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2300"}],"version-history":[{"count":4,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2300\/revisions"}],"predecessor-version":[{"id":2306,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2300\/revisions\/2306"}],"wp:attachment":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}