初次使用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