servlet生命周期
1.当servlet第一次调用时,会出发init()函数,该函数会吧servlet实例加载到内存
2.然后调用service函数
3.当第二次访问该servlet时直接调用service函数
4.当web应用reload或者关闭tomcat或者关机都会去调用destory函数,该函数去销毁servlet
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.wangzhi.servlet; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class Myfirst implements Servlet { // 销毁函数,内存中清除,调用一次 @Override public void destroy() { // TODO Auto-generated method stub } @Override public ServletConfig getServletConfig() { // TODO Auto-generated method stub return null; } // 获取对象 @Override public String getServletInfo() { // TODO Auto-generated method stub return null; } // 每次调用一次,servlet装载内存 @Override public void init(ServletConfig arg0) throws ServletException { // TODO Auto-generated method stub } @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { // TODO Auto-generated method stub res.setCharacterEncoding("utf-8"); System.out.println("hello,world!"+new java.util.Date()); res.getWriter().println("hello,world"+new java.util.Date()+" I want to fuck you"+req.getRemoteHost()+"<h1>Helloworkd!大家好才是真的好</h1>" ); } }