<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/post//</link>
<title><![CDATA[ServletContextListener的用法]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[Web开发]]></category>
<pubDate>Sun, 30 Mar 2014 09:03:23 +0000</pubDate> 
<guid>https://www.heckjj.com/post//</guid> 
<description>
<![CDATA[ 
	ServletContextListener的用法：这个事件类作为Web应用服务的一部分，处理Web应用的 servlet上下文(context)的变化的通知。这可以解释为，好像有个人在服务器旁不断地通知我们服务器在发生什么事件。那当然需要监听者了。<br/>因此，在通知上下文(context)初始化和销毁的时候，ServletContextListner非常有用。<br/><br/><textarea name="code" class="java" rows="15" cols="100">
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.*;
public class MyListener implements ServletContextListener &#123;

private ServletContext context = null;

 public void contextDestroyed(ServletContextEvent event)&#123;
&nbsp;&nbsp;//Output a simple message to the server&#039;s console
&nbsp;&nbsp;System.out.println(&quot;The Simple Web App. Has Been Removed&quot;);
&nbsp;&nbsp;this.context = null;
&#125;

 // 这个方法在Web应用服务做好接受请求的时候被调用。
public void contextInitialized(ServletContextEvent event)&#123;
&nbsp;&nbsp;this.context = event.getServletContext();
&nbsp;&nbsp;//Output a simple message to the server&#039;s console
&nbsp;&nbsp;System.out.println(&quot;The Simple Web App. Is Ready&quot;);
&#125;

&#125;
</textarea><br/><br/><textarea name="code" class="xml" rows="15" cols="100">
&lt;web-app&gt;
&lt;listener&gt;
&nbsp;&nbsp;&lt;listener-class&gt;
&nbsp;&nbsp; com.listeners.MyContextListener
&nbsp;&nbsp;&lt;/listener-class&gt;
&lt;/listener&gt;
&lt;servlet/&gt;
&lt;servlet-mapping/&gt;
&lt;/web-app&gt; 
</textarea><br/>ServletContextListener接口有两方需要实现的方法:<br/>contextInitialized()和contextDestroyed();<br/>Listener,译为监听者.顾名思义,它会监听Servlet容器(实际上就是中间件启动和销毁时执行的方法),当应用开始的时候它会调用contextInitialized()方法;<br/>当应用关闭的时候,它同样会调用contextDestroyed()方法.<br/>我们可以利用这个特性初始化一些信息,当然我们也可以利用Servlet类init()方法,并在配置文件中让它启动应用的时候<br/>就执行,并且在关闭的时候执行destroy()方法.但是继承此接口应该更符合容器的应用.<br/>举个简单的例子:在一些论坛,社区及聊天室当中,删除在线的超时用户就可以利用这个接口来实现.<br/>可以利用JAVA的TimerTask及Timer类来实现每隔一定的时间进行自动检测.<br/><br/>实例代码如下:<br/>UserOnlineTimerTask.java<br/>-----------------<br/><textarea name="code" class="java" rows="15" cols="100">
package tk.hecks.servlet;
import java.util.TimerTask;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class UserOnlineTimerTask extends TimerTask &#123;
Log log = LogFactory.getLog(UserOnlineTimerTask.class);
public void run() &#123;
// 删除超时在线用户
log.info(&quot;删除在线的超时用户....&quot;);
&#125;

&#125;
</textarea><br/>------------------------------------<br/>SysListener.java<br/>-----------------------------------<br/><textarea name="code" class="java" rows="15" cols="100">
package tk.hecks.servlet;
import java.io.IOException;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SysListener extends HttpServlet implements ServletContextListener &#123;

Log log = LogFactory.getLog(SysListener.class);
Timer timer = new Timer();

public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException &#123;
//

&#125;

public void contextInitialized(ServletContextEvent sce) &#123;
log.info(&quot;initial context....&quot;);
timer.schedule(new UserOnlineTimerTask(), 0, 10000);

&#125;

public void contextDestroyed(ServletContextEvent sce) &#123;
log.info(&quot;destory context....&quot;);
timer.cancel();
&#125;

&#125;
</textarea><br/>--------------------------------<br/>如果你没有使用log4j的话,你可以把log.info()改为System.out.println()会得到同样的结果.<br/>Tags - <a href="https://www.heckjj.com/tags/servlet/" rel="tag">servlet</a> , <a href="https://www.heckjj.com/tags/timer/" rel="tag">timer</a> , <a href="https://www.heckjj.com/tags/class/" rel="tag">class</a> , <a href="https://www.heckjj.com/tags/log4j/" rel="tag">log4j</a> , <a href="https://www.heckjj.com/tags/webserver/" rel="tag">webserver</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post//#blogcomment</link>
<title><![CDATA[[评论] ServletContextListener的用法]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>