方法1:<load-on-startup> servlet
ServletContext application = this.getServletContext();
public void init() throws ServletException {
ServletContext application = this.getServletContext();
... ... ...
}在servlet的init()方法加入自己的代码 <servlet>
<servlet-name>initCache</servlet-name>
<servlet-class>maWei.cache.InitCache</servlet-class>
<load-on-startup>9</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>initCache</servlet-name>
<url-pattern>/initCache</url-pattern>
</servlet-mapping>
在web.xml里加入servlet的引用,其中<load-on-startup>9</load-on-startup>里的数字为0,自动启动的优先级最高;没有此句or负数,则servlet不自动启动
问题:
需要更新cache时,这个servlet没法在bean里调用?
找到的资料可以用servlet调用bean
参考:
http://www.qtedu.net/sspd/xxjs/200504/9770.html
http://blog.sina.com.cn/s/blog_57769b7b0100tpjc.html
方法2:ServletContextListener
http://www.qqgb.com/Program/Java/JavaBlog/Program_145756.html
http://www.iteye.com/problems/30223
本文详细介绍了如何在web.xml中配置servlet实现自动启动,并通过servlet调用bean来更新cache。当需要更新cache时,可以通过在servlet的init方法中加入自己的代码来实现。此外,还提供了使用ServletContextListener作为替代方案的方法。
182

被折叠的 条评论
为什么被折叠?



