利用servlet在tomcat启动时初始化相关信息(转)
网站建设时需要用到这样的功能:显示网站(或系统)的启动时间,至今已运行了时间。曹海峰的思路是web.xml中配置servlet,在网站启动时调用servlet的init()方法,初始化相关数据。具体代码如下。
1、web.xml添加
public class GetCmsStartTimeServlet extends HttpServlet {public void init() throws ServletException{SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String starttime = sdf.format(new Date());ServletContext application=this.getServletContext();application.setAttribute("starttime", starttime);}}
?