初次使用MyEclipse || Servlet 的生命周期
生活随笔
收集整理的這篇文章主要介紹了
初次使用MyEclipse || Servlet 的生命周期
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Servlet 的生命周期:?
從第一次調用,到服務器關閉
如果在 web.xml 中配置了 load-on-startup 則是從服務器開啟到服務器關閉
package com.cl.servlet;import java.io.IOException;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** Servlet的生命周期:* 1.從第一次調用到服務器關閉* 2.如果Servlet在web.xml中配置了load-on-startup,生命周期為從服務器啟動到服務器關閉* 注意: init方法是對Servlet進行初始化的一個方法,會在servlet第一次加載進行存儲時執行* destroy方法是在servlet被銷毀時執行,也就是服務器關閉時* @author Administrator**/ public class servletLife extends HttpServlet {//初始化方法,在servlet第一次加載內容的時候被調用@Overridepublic void init() throws ServletException {System.out.println("servlet初始化完成");}//service方法,真正處理請求的方法@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.getWriter().write("servlet life");System.out.println("servlet life");}//@Overridepublic void destroy() {System.out.println("我被銷毀了……");} } <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>02-MyServlet</display-name><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>servletLife</servlet-name><servlet-class>com.cl.servlet.servletLife</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>servletLife</servlet-name><url-pattern>/life</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list> </web-app>
?
總結
以上是生活随笔為你收集整理的初次使用MyEclipse || Servlet 的生命周期的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: servlet的使用
- 下一篇: Service 和 doGet 和 do