﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-An's Blog -文章分类-Servlet</title><link>http://www.blogjava.net/ag4444/category/5476.html</link><description>十年磨一键</description><language>zh-cn</language><lastBuildDate>Sat, 03 Mar 2007 04:02:49 GMT</lastBuildDate><pubDate>Sat, 03 Mar 2007 04:02:49 GMT</pubDate><ttl>60</ttl><item><title>Servlet 的生命周期</title><link>http://www.blogjava.net/ag4444/articles/22077.html</link><dc:creator>   An's Blog </dc:creator><author>   An's Blog </author><pubDate>Thu, 01 Dec 2005 02:14:00 GMT</pubDate><guid>http://www.blogjava.net/ag4444/articles/22077.html</guid><wfw:comment>http://www.blogjava.net/ag4444/comments/22077.html</wfw:comment><comments>http://www.blogjava.net/ag4444/articles/22077.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ag4444/comments/commentRss/22077.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ag4444/services/trackbacks/22077.html</trackback:ping><description><![CDATA[<FONT face=Arial>原文：http:<BR><BR>Servlet的生命周期主要有四个阶段：载入、初始化、执行、清除。<BR><BR>Servlet的是说容器将Servlet类别栽入JVM并实例化，这个時候Servlet物件还不算具备Servlet该有的功能，只能说是一个纯粹的Java物件，载入Servlet的时机有三种可能： <BR></FONT>
<UL>
<LI><FONT face=Arial>司服器启动时</FONT> 
<LI><FONT face=Arial>浏览器第一次接收请求时 </FONT>
<LI><FONT face=Arial>管理者要求时</FONT> </LI></UL>
<P><FONT face=Arial>1、若要求在司服器启动时栽入Servlet，可以在web.xml的&lt;servlet&gt;定义时，加入&lt;load-on- startup&gt;标识，例如：<BR><STRONG>web.xml</STRONG><BR>&lt;servlet&gt; <BR>&nbsp;&nbsp;&nbsp; &lt;servlet-name&gt;Servletname1&lt;/servlet-name&gt; <BR>&nbsp;&nbsp;&nbsp; &lt;servlet-class&gt;demo.servlet.SomeServlet1&lt;/servlet-class&gt; <BR>&nbsp;&nbsp;&nbsp; &lt;load-on-startup&gt;10&lt;/load-on-startup&gt; <BR>&lt;/servlet&gt; <BR>&lt;servlet&gt; <BR>&nbsp;&nbsp;&nbsp; &lt;servlet-name&gt;Servletname2&lt;/servlet-name&gt; <BR>&nbsp;&nbsp;&nbsp; &lt;servlet-class&gt;demo.servlet.SomeServlet2&lt;/servlet-class&gt; <BR>&nbsp;&nbsp;&nbsp; &lt;load-on-startup&gt;20&lt;/load-on-startup&gt; <BR>&lt;/servlet&gt; <BR><BR>&lt;load-on-startup&gt;标识设定是载入时的顺序值，数值越小越快载入<BR><BR>在servlet载入，并实例化之后，会执行初始化操作，此时init（）方法会被呼叫执行。init（）方法传入一个ServletConfig的物件----表示与Servlet相关的环境物件</FONT><FONT face=Arial>，web.xml中的一些设定也包括在这个物件当中，在初始化之后， Servlet物件才称的上具备Servlet功能：<BR><BR><BR></P>
<DIV style="MARGIN-LEFT: 40px"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">public void init(ServletConfig config) </SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throws ServletException </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">&nbsp;</SPAN><BR></DIV>
<P>ServletConfig的使用您并不陌生，在JSP的隐含物件 config 就是对应于ServletConfig，您可以用它来取得在web.xml中设定的初始参数（即使用config.getInitParameter ()）：<BR></P>
<UL>
<LI>web.xml</LI></UL><PRE>... <BR>&nbsp;&lt;servlet&gt; <BR>&nbsp;&nbsp;&nbsp; ... <BR>&nbsp;&nbsp;&nbsp; &lt;init-param&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;param-name&gt;parameter&lt;/param-name&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;param-value&gt;value&lt;/param-value&gt; <BR>&nbsp;&nbsp;&nbsp; &lt;/init-param&gt; <BR>&nbsp;&nbsp;&nbsp; ... <BR>&nbsp;&lt;/servlet&gt;<BR>...</PRE>
<P><BR>您也可以使用无参数的init()方法，在Servlet 2.1之后，容器会呼叫有参数的init()方法，在执行完毕后再呼叫无参数的init()方法：<BR></P>
<DIV style="MARGIN-LEFT: 40px"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">public void init() throws ServletException </SPAN><BR></DIV>
<P><BR><BR>通常在重新定义init()方法时，会这么写：<BR></P>
<DIV style="MARGIN-LEFT: 40px; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold">public void init(ServletConfig config) </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">&nbsp;&nbsp;&nbsp; throws ServletException { </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">&nbsp;&nbsp;&nbsp; super.init(config); </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">&nbsp;&nbsp;&nbsp; // ..... </SPAN><BR style="FONT-WEIGHT: bold"><SPAN style="FONT-WEIGHT: bold">} </SPAN><BR></DIV>
<P><SPAN style="FONT-FAMILY: Courier New,Courier,monospace"></SPAN><BR><BR>这是了兼具Servlet 2.0之前的相容，确保父类别GenericServlet会呼叫无参数的init()方法：<BR></P>
<DIV style="MARGIN-LEFT: 40px"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">public class GenericServlet </SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; implements Servlet, ServletConfig { </SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp; ServletConfig _config = null; </SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;</SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp; public void init(ServletConfig) throws ServletException { </SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _config = config; </SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; log("init classed"); </SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; init(); </SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp; } </SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp; .... </SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">} </SPAN><BR></DIV>
<P>Servlet的初始化可以用于一些资源的预先载入，例如开启资料库连线，避免在使用者第一次请求网页时才开启资料库连线，以免使用者必須花费时间等待连线的完成。<BR><BR>Servlet在载入之后会一直存在于伺服器的记忆体中，直到伺服器关闭或是要求清除Servlet时，这可以避免物件生成时所需的时间与资源负担，并可实现一些咨询的持续性（persistence），每一个使用者请求Servlet时，容器会产生一个执行线来存取Servlet，也因而在设计 Servlet时必须注意到执行时的安全问题。<BR><BR>当Servlet被清除之时，会呼叫destroy()方法，通常也建议呼叫super.destroy()，这会呼叫父类別 GenericServlet的destroy()，以在log档中记录Servlet被清除的询息，例如：</P>
<DIV style="MARGIN-LEFT: 40px"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">public void destroy() { </SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp; super.destroy(); </SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">&nbsp;&nbsp;&nbsp; //.... </SPAN><BR style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace"><SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">}</SPAN>&nbsp;<BR><BR>浏览器发出请求至Servlet的执行順序是：<SPAN style="FONT-WEIGHT: bold; FONT-FAMILY: Courier New,Courier,monospace">浏览器 -&gt; Web 伺服器 -&gt; Servlet 容器 -&gt; service()</SPAN><BR></DIV>
<P><BR>如果是HttpServlet，在service()之后呼叫对应的doXXX()方法。<BR><BR><BR></P></FONT><img src ="http://www.blogjava.net/ag4444/aggbug/22077.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ag4444/" target="_blank">   An's Blog </a> 2005-12-01 10:14 <a href="http://www.blogjava.net/ag4444/articles/22077.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>