当前位置: 技术问答>java相关
如何在tomcat启动时自动调用一个servlet?
来源: 互联网 发布时间:2015-09-08
本文导语: 宽带网络计费系统中要求在tomcat启动时加载一个类,并定时向运行系统发送查询,请问如何解决?刚接触JAVA不久,哪位知道的告知一下,不甚感激。 | servlet specification 2.3, chapter10: Application event lis...
宽带网络计费系统中要求在tomcat启动时加载一个类,并定时向运行系统发送查询,请问如何解决?刚接触JAVA不久,哪位知道的告知一下,不甚感激。
|
servlet specification 2.3, chapter10:
Application event listeners are classes that implement one or more of the servlet event listener interfaces
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.
Application event listeners are classes that implement one or more of the servlet event listener interfaces
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.
|
in web.xml
1
1
|
一般在你的应用文件夹下会建立一个web-inf文件夹,里面放一些不想让用户的道的数据,比如又一个classes文件夹,来放servlet或者bean等等。
在web-inf下你可以建立一个web.xml文件,它的格式你可以参考%tomcat%/conf/web.xml来写,可以制定自己应用中用到的servlet。其中有一个1的标签,来定义servlet的启动时间,1为Tomcat启动时候启动。如果没有在这个文件里声明的得servlet会在第一个用户访问时候启动。但是推荐把所有servlet都在此文件中声明,可以实现servlet的别名访问,只是不写1,那么它就会在第一个用户访问时在启动了。
good luck...
在web-inf下你可以建立一个web.xml文件,它的格式你可以参考%tomcat%/conf/web.xml来写,可以制定自己应用中用到的servlet。其中有一个1的标签,来定义servlet的启动时间,1为Tomcat启动时候启动。如果没有在这个文件里声明的得servlet会在第一个用户访问时候启动。但是推荐把所有servlet都在此文件中声明,可以实现servlet的别名访问,只是不写1,那么它就会在第一个用户访问时在启动了。
good luck...
|
web.xml 例子片断:
...
controller
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 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.
com.mycompany.mypackage.ControllerServlet
listOrders
com.mycompany.myactions.ListOrdersAction
saveCustomer
com.mycompany.myactions.SaveCustomerAction
1
controller
*.do
...
controller
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 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.
com.mycompany.mypackage.ControllerServlet
listOrders
com.mycompany.myactions.ListOrdersAction
saveCustomer
com.mycompany.myactions.SaveCustomerAction
1
controller
*.do