1、书写一个servlet并编译,如:
package testservlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class Hello implements Servlet {
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("destroy");
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
System.out.println("servlet 初始化方法init");
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("service");
PrintWriter pw = res.getWriter();
pw.println("hello");
}
}
2、将这个类所属应用(web.xml已经配置好)部署于tommcat的webapps下,启动tomcat,在浏览器访问该servlet,多次刷新,控制台信息如下:
第一次访问servlet时,才调用init方法,service可以多次调用
3、destroy执行一次的验证
登录tomcat首页,点击Manager App,会提示输入用户名和密码(在tomcat的conf下的tomcat-users.xml中进行查询或配置,配置参见http://www.cnblogs.com/hblthink/p/8215387.html),成功登录后找到对应servlet所属应用,点击reload,控制台显示destroy