Timer类

需求:系统启动之后立即启动定时器 每隔十秒中在控制台打印当前时刻

利用servlet loadonstartup配置实现
public class startupServlet extends HttpServlet
{

	public void init() throws ServletException
	{
		super.init();
		Timer timer = new Timer(10000, new ActionListener()
		{

			public void actionPerformed(ActionEvent arg0)
			{
				System.out.println(new Date());
			}

		});

		timer.start();
	}

}


web.xml中
 <servlet>
     <servlet-name>startupServlet</servlet-name>
    <servlet-class>com.tommyzhang.servlets.startupServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

你可能感兴趣的:(java,Web,xml,servlet)