标签:servlet
a) 载入
i. 实例化
ii. <load-on-startup>元素,表名servlet被加载的,顺序从小到大加载
b) 初始化
i. 第一次请求的时候被初始化
c) 执行
i. service()方法
ii. 每个请求,new一个新的线程,调用一次service—>super.service—>doGet,doPost
iii. 不要在servlet中设置成员变量(多个请求线程共享,会发生资源冲突的问题)
iv. doGet()、doPost()
v.
d) 销毁
i. destroy()方法
范例:
package com.zgy.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LifCircleServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
System.out.println("servlet执行了");
res.getWriter().println("servlet被执行");
}
@Override
public void destroy() {
System.out.println("servlet被销毁");
}
@Override
public void init() throws ServletException {
System.out.println("servlet被初始化");
}
}
一般servlet的销毁是由容器自动销毁的
Servlet的Get和Post方法
package com.zgy.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestPostGetServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("doGet()!!!!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
}
}
标签:servlet
原文地址:http://blog.csdn.net/yaguanzhou2014/article/details/43629601