Using listeners to implement
1. Customizing a class Calllocationservelt implementation Servletcontextlistener
and cover its contextinitialized(servletcontexteventarg0) method
public void contextinitialized (Servletcontextevent arg0) {//TODO auto-generated method StubSystem.out.println (" Calllocationservelt Monitor Started ");//use timer to perform every 3 seconds to access a timer timer = new timer (); Timer.schedule (new TimerTask () {public void run () {//Initialize HTTP request client HttpClient httpClient =new defaulthttpclient ();//Use the Get method to request HttpGet HttpGet = new HttpGet ("http:// Localhost:8080/calllocationservelt/servlet/localservlet "); try {//executes the request and stores the result in the HTTP response object HttpResponse response = Httpclient.execute (HttpGet);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}, 0, 3*1000);
2. Configure the Listener in Web. XML to execute when Tomcat starts
<listener> <listener-class> calllocationservelt </listener-class> </ Listener>
3. Write a simple servlet for the listener to access
public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { System.out.println ("Number of visits Localservlet:" + (calltime++));}
The complete project can be downloaded: http://download.csdn.net/detail/ch717828/8394573
Reference: http://blog.csdn.net/cai5/article/details/7528888
http://download.csdn.net/detail/aaayongaaa/5300578
http://download.csdn.net/detail/linzhijia0612/4965858
Automatic access to the local servlet when Tomcat is started