您的位置:首页 > 运维架构 > Tomcat

tomcat启动时,自动加载

2012-02-28 20:45 295 查看
方法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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: