Automatic access to the local servlet when Tomcat is started

Source: Internet
Author: User

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:


Automatic access to the local servlet when Tomcat is started

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.