方法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