标签:
在下列时刻Servlet容器装载Servlet:
1.Servlet容器启动时自动装载某些Servlet,实现它只需要在web.xml文件中的<Servlet></Servlet>之间添加如下代码:
<load-on-startup>1</load-on-startup> 数字越小表示优先级越高。
2.在Servlet容器启动后,客户端首次向Servlet发送请求。
3.Servlet类文件被更新后,重新装载Servlet。
第一种情况:
Servlet被装载后,Servlet容器创建一个Servlet实例并且调用Servlet的init()方法进行初始化。在Servlet的整个生命周期内,init()方法只被调用一次。
package Servlet; 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 TestServlet1 extends HttpServlet { /** * Constructor of the object. */ public TestServlet1() { System.out.println("TestServlet1构造方法被执行..."); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log System.out.println("TestServlet1销毁方法被执行..."); } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("TestServlet1 doGet()方法被执行..."); response.setContentType("text/html;charset=utf-8"); 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.println("<h1>大家好,我是TestServlet1</h1>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("TestServlet1 doPost()方法被执行..."); doGet(request,response);//让doPost()执行与doGet()相同的操作。 } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { System.out.println("TestServlet1初始化方法被执行..."); } }
web.xml代码:
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>TestServlet1</servlet-name> <servlet-class>Servlet.TestServlet1</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet1</servlet-name> <url-pattern>/servlet/TestServlet1</url-pattern> </servlet-mapping>
index.jsp代码:
<body> <h1>Servlet生命周期</h1> <hr> <a href="servlet/TestServlet1">以get方式请求TestServlet1</a> </body>
运行结果:
第二种:
怎么让Servlet容器启动时自动装载某些Servlet?
答:需要在web.xml文件中进行如下配置:
<servlet>
....
<load-on-startup>1</load-on-startup>
</servlet>
代码:
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>TestServlet2</servlet-name> <servlet-class>Servlet.TestServlet2</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>TestServlet1</servlet-name> <servlet-class>Servlet.TestServlet1</servlet-class> <load-on-startup>2</load-on-startup> </servlet>
标签:
原文地址:http://www.cnblogs.com/sdut-myj/p/4340047.html