------- android培训、java培训、期待与您交流! --------
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4 Creation-Date: 2013-01-13T00:13:38+08:00 ====== Servlet 02 生命周期 ====== Created Sunday 13 January 2013 Servlet 的生命周期<工作流程> <线程不安全> WEB收到客户端的servlet访问请求后 1. 当servlet第一次被调用的时候,会触发init()函数,该函数会把servlet的实例装载到内存 init()只会被调用一次 2. 然后去调用servlet 的 service () 函数<创建HttpServletRequest 和 HttpServletResponse > 3. 当第二次后访问该servlet 则直接调用 service() 函数 4. 当WEB应用 reload 或者 关闭 都会去调用 destory ()函数,该函数就会去销毁servlet;
public class TestServlet implements Servlet {
public void destroy() { // 销毁servlet 该函数之执行一次 } public ServletConfig getServletConfig() { // 获得serletConfig对象 return null; } public String getServletInfo() { // 获得servletInfo信息 return null; } public void init(ServletConfig arg0) throws ServletException { // 启动servlet 该函数只执行一次 } public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { // 该函数执行多次 我们的逻辑代码就写在这里 }
}
====== Servlet 04 ServletConfig ====== Created Sunday 13 January 2013 ServletConfig对象 改对象主要是用于读取servlet的一些信息 配置当前servlet信息 <servlet> <init-param><!-- 只能被该servlet读取--> <param-name>encoding</param-name> <param-value>utf-8</param-vlaue> </init-param> </servlet> 全局设置配置信息 <context-param> </context-param> 读取servletConfig配置信息 用配置名读取 this.getServletConfig().getInitParameter("encoding"); 读取所有配置 Enumeration<String> names =this.getServletConfig.getInitParameterNames(); 略!while(names.next.....)遍历name集合 取值