一、配置随项目启动而启动的Servlet
1)、创建一个类继承HttpServlet
package atlantis.test;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class Test extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
try {
System.out.println("加载静态资源!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
2)、在web.xml配置文件中配置servlet信息
<servlet>
<servlet-name>staticResourceLoad</servlet-name>
<servlet-class>atlantis.test.Test</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
3)、结果
二、监听(Listener)
1)、创建个监听类,实现ServletContextListener
package atlantis.test;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class LoadListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("卸载资源!");
}
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("加载资源!");
}
}
2)、在web.xml配置文件中配置监听信息
<listener>
<listener-class>atlantis.test.LoadListener</listener-class>
</listener>
3)、结果