请问怎么在服务器启动时执行某段代码

zjy30015563 2012-03-16 10:05:54
用的tomcat,想在服务器启动时执行某段代码,改怎么办
...全文
418 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjy30015563 2012-03-16
  • 打赏
  • 举报
回复
非常感谢
Mainstream_Code 2012-03-16
  • 打赏
  • 举报
回复

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<listener>
<listener-class>com.MyServletContextListener</listener-class>
</listener>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>




package com;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
* 初始化加载
* @author xuhe
*
*/
public class MyServletContextListener implements ServletContextListener {

public void contextInitialized(ServletContextEvent arg0) {
System.out.println("初始化加载");
}

public void contextDestroyed(ServletContextEvent sce) {

}
}

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧