如何在tomcat启动时自动调用一个servlet?

tripofdream 2002-09-19 11:06:04
宽带网络计费系统中要求在tomcat启动时加载一个类,并定时向运行系统发送查询,请问如何解决?刚接触JAVA不久,哪位知道的告知一下,不甚感激。

...全文
878 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
松耦合紧内聚 2002-09-19
  • 打赏
  • 举报
回复
一般在你的应用文件夹下会建立一个web-inf文件夹,里面放一些不想让用户的道的数据,比如又一个classes文件夹,来放servlet或者bean等等。
在web-inf下你可以建立一个web.xml文件,它的格式你可以参考%tomcat%/conf/web.xml来写,可以制定自己应用中用到的servlet。其中有一个<load-on-startup>1</load-on-startup>的标签,来定义servlet的启动时间,1为Tomcat启动时候启动。如果没有在这个文件里声明的得servlet会在第一个用户访问时候启动。但是推荐把所有servlet都在此文件中声明,可以实现servlet的别名访问,只是不写<load-on-startup>1</load-on-startup>,那么它就会在第一个用户访问时在启动了。
good luck...
tripofdream 2002-09-19
  • 打赏
  • 举报
回复
感谢eyeieye(魔之眼)和bdsc() ,不过两位提供的都是在application startup时加载的方法,那么tomcat中的application是在tomcat启动时启动呢,还是在第一个用户访问时启动?
beming 2002-09-19
  • 打赏
  • 举报
回复
关注eyeieye(魔之眼) 的方法
Symanteck 2002-09-19
  • 打赏
  • 举报
回复
在web.xml中
<servlet-mapping>
<servlet-name>CartServlet</servlet-name> //Servlet类名
<url-pattern>/cart.jsp</url-pattern> //将cart.jsp映射到Servet
</servlet-mapping>
在IE输入.../cart.jsp实际执行的是CartServlet

eyeieye 2002-09-19
  • 打赏
  • 举报
回复
in web.xml
<servlet>
<…………>
<load-on-startup>1</load-on-startup>
</servlet>
c_crazyren 2002-09-19
  • 打赏
  • 举报
回复
关注。
bdsc 2002-09-19
  • 打赏
  • 举报
回复
servlet specification 2.3, chapter10:

Application event listeners are classes that implement one or more of the servlet event listener interfaces

<Servlet Constext Events>
listener interace: javax.servlet.SerletContextListener

An Example of listener User:
When the application starts up, the listener class is notified.The application logs on to the database, and stores the connection in the servlet context.
<omited>
tripofdream 2002-09-19
  • 打赏
  • 举报
回复
多谢各位,结帐!
whiteclude 2002-09-19
  • 打赏
  • 举报
回复
web.xml 例子片断:
...
<servlet>
<servlet-name>controller</servlet-name>
<description>
This servlet plays the "controller" role in the MVC architecture
used in this application. It is generally mapped to the ".do"
filename extension with a <servlet-mapping> element, and all form
submits in the app will be submitted to a request URI like
"saveCustomer.do", which will therefore be mapped to this servlet.

The initialization parameter namess for this servlet are the
"servlet path" that will be received by this servlet (after the
filename extension is removed). The corresponding value is the
name of the action class that will be used to process this request.
</description>
<servlet-class>com.mycompany.mypackage.ControllerServlet</servlet-class>
<init-param>
<param-name>listOrders</paramName>
<param-value>com.mycompany.myactions.ListOrdersAction</param-value>
</init-param>
<init-param>
<param-name>saveCustomer</paramName>
<param-value>com.mycompany.myactions.SaveCustomerAction</param-value>
</init-param>
<!-- Load this servlet at server startup time -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
bdsc 2002-09-19
  • 打赏
  • 举报
回复
The servlet context has just been created and is avaible to service its first request.

other methods, except what i said, is container-specific.

Good luck!

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧