Servlet 生命周期
2011-01-25 09:20
120 查看
Servlet 在部署后给用户访问一直到结束这个服务器关闭,或者 Servlet 类被替换,都只有一个 Servlet 对象,这个对象支配着全部用户的访问。
Servlet 的对象创建自第一个用户对此 Servlet 的访问,创建以后,就一直存在于服务器。
Servlet 的创建会首先调用其子类的构造函数,然后调用 init() 方法,实现初始化,init() 方法在 Servlet 生命周期中也只调用一次,接着调用 public service() 方法,public service() 方法分发客户浏览器的请求给 protected service() 方法,protected service() 方法接受来自 public service() 方法的标准 HTTP 请求,然后分发这些请求给相应的 doXxx() 方法处理。
可以参见 Servlet API:
Receives standard HTTP requests from the public
Dispatches client requests to the protected
可以通过覆盖那些方法进行验证:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
在配置好 web.xml 后,用浏览器首次访问,根据上面的分析,得到的结果应该是:
然后的每次访问都是:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
但是,但是结果并非如此:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }以后的每次访问结果都是:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
为什么 public service 和 protected service 跑到后面去了呢?分析源代码应该能够找到答案,(⊙o⊙)…,Tomcat 中没有。
应该是 public service() 方法里面有个过程是要得到 protected service() 方法的反馈才继续的,同理 protected service() 方法中也要得到 doXxx() 方法的反馈才能继续。鄙人瞎猜,没有分析源码的胡乱解释。
---EOF---
那本 《Core Java 》我真的看不懂。
Servlet 的对象创建自第一个用户对此 Servlet 的访问,创建以后,就一直存在于服务器。
Servlet 的创建会首先调用其子类的构造函数,然后调用 init() 方法,实现初始化,init() 方法在 Servlet 生命周期中也只调用一次,接着调用 public service() 方法,public service() 方法分发客户浏览器的请求给 protected service() 方法,protected service() 方法接受来自 public service() 方法的标准 HTTP 请求,然后分发这些请求给相应的 doXxx() 方法处理。
可以参见 Servlet API:
service
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
Receives standard HTTP requests from the public
servicemethod and dispatches them to the
doXXX methods defined in this class. This method is an HTTP-specific version of the
Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)method. There's no need to override this method. Parameters:
req- the
HttpServletRequestobject that contains the request the client made of the servlet
resp- the
HttpServletResponseobject that contains the response the servlet returns to the client Throws:
IOException- if an input or output error occurs while the servlet is handling the HTTP request
ServletException- if the HTTP request cannot be handledSee Also:
Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
service
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
Dispatches client requests to the protected
servicemethod. There's no need to override this method. Specified by:
servicein interface
ServletSpecified by:
servicein class
GenericServletParameters:
req- the
HttpServletRequestobject that contains the request the client made of the servlet
res- the
HttpServletResponseobject that contains the response the servlet returns to the client Throws:
IOException- if an input or output error occurs while the servlet is handling the HTTP request
ServletException- if the HTTP request cannot be handledSee Also:
Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
可以通过覆盖那些方法进行验证:
1: import java.io.IOException;
2:
3: import javax.servlet.ServletException;
4: import javax.servlet.ServletRequest;
5: import javax.servlet.ServletResponse;
6: import javax.servlet.http.HttpServlet;
7: import javax.servlet.http.HttpServletRequest;
8: import javax.servlet.http.HttpServletResponse;
9:
10: public class LoginServlet extends HttpServlet {
11:
12: public LoginServlet() {
13: System.out.println("Constructor...");
14: }
15:
16: @Override
17: protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
18: throws ServletException, IOException {
19: super.service(arg0, arg1);
20: System.out.println("protected service");
21: }
22:
23: @Override
24: public void service(ServletRequest arg0, ServletResponse arg1)
25: throws ServletException, IOException {
26: super.service(arg0, arg1);
27: System.out.println("public service");
28: }
29:
30: @Override
31: public void init() throws ServletException {
32: super.init();
33: System.out.println("init");
34: }
35:
36: @Override
37: public void destroy() {
38: System.out.println("Destroyed...");
39: }
40:
41: @Override
42: public void doGet(HttpServletRequest req, HttpServletResponse resp)
43: throws ServletException, IOException {
44: System.out.println("doGet");
45: }
46:
47: @Override
48: public void doPost(HttpServletRequest req, HttpServletResponse resp)
49: throws ServletException, IOException {
50: doGet(req,resp);
51: }
52:
53: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
在配置好 web.xml 后,用浏览器首次访问,根据上面的分析,得到的结果应该是:
1: Constructor...
2: init
3: public service
4: protected service
5: doGet
然后的每次访问都是:
1: public service
2: protected service
3: doGet
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
但是,但是结果并非如此:
1: Constructor...
2: init
3: doGet
4: protected service
5: public service
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }以后的每次访问结果都是:
1: doGet
2: protected service
3: public service
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
为什么 public service 和 protected service 跑到后面去了呢?分析源代码应该能够找到答案,(⊙o⊙)…,Tomcat 中没有。
应该是 public service() 方法里面有个过程是要得到 protected service() 方法的反馈才继续的,同理 protected service() 方法中也要得到 doXxx() 方法的反馈才能继续。鄙人瞎猜,没有分析源码的胡乱解释。
---EOF---
那本 《Core Java 》我真的看不懂。
相关文章推荐
- [Servlet]Servlet的生命周期
- Servlet的生命周期
- 【drp 9】Servlet生命周期
- servlet生命周期和线程安全
- servlet3.1规范翻译:第11章 应用生命周期事件
- Servlet 生命周期、工作原理
- Listener,Filter,Servlet执行顺序和生命周期
- Servlet开发【04】Servlet的生命周期
- Servlet生命周期
- servlet的生命周期
- Servlet初体验之------Servlet生命周期
- Servlet对象生命周期以及运行过程
- servlet的执行原理与生命周期
- servlet生命周期的好的总结文章
- Servlet的生命周期小实例
- Web---演示servlet技术(servlet生命周期),解决中文乱码问题
- servlet生命周期
- servlet的生命周期
- Servlet调用过程和生命周期
- Servlet 的配置和生命周期方法——day_04