看看servlet的生命周期

servlet在被创建的时候,是会调用里面的init(),在被销毁的时候,调用destroy()方法!~所以我们可以把需要的一些初始化行为放在init()方法里面执行,比如数据库,网路等等。把释放资源等等放在destroy()里面执行。

———————————————————————-

package ynu.sanwen.gagaj2ee;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestLife extends HttpServlet {

private static final long serialVersionUID = 4219365912615355806L;

public void destroy() {
System.out.println(“TestLife.destroy()”);
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out.println(“<!DOCTYPE HTML PUBLIC \”-//W3C//DTD HTML 4.01 Transitional//EN\”>”);
out.println(“<HTML>”);
out.println(“  <HEAD><TITLE>A Servlet</TITLE></HEAD>”);
out.println(“  <BODY>”);
out.print(“hi test life”);
out.println(“  </BODY>”);
out.println(“</HTML>”);
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}

public void init() throws ServletException {
System.out.println(“TestLife.init()”);
}

}

—————————————————————————————–

共有 0 条评论

Top