生命周期:是一个程序的存在周期,servlet由于是受容器的管理,所以容器来决定其生命周期
1、servlet生命周期
2、servlet生命周期对应的方法
3、servlet生命周期代码
public class LifeCycleServlet extendsHttpServlet{public void init() throwsServletException{
System.out.println("** 1、Servlet初始化 --> init()") ;
}public voiddoGet(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{
System.out.println("** 2、Servlet服务 --> doGet()、doPost()") ;
}public voiddoPost(HttpServletRequest req,HttpServletResponse resp)throwsServletException,IOException{this.doGet(req,resp) ;
}public voiddestroy(){
System.out.println("** 3、Servlet销毁 --> destory()") ;try{
Thread.sleep(3000) ;
}catch(Exception e){}
}/*public void service(ServletRequest req,
ServletResponse res)
throws ServletException,
IOException{
System.out.println("************ 服务 **************") ;
}*/}
一个基本生命周期编译后,就可以在web-INF/web.xml中进行映射配置。下面直接在根目录下配置
life
com.alice.servlet.LifeCycleServlet
1
life
/LifeServlet
访问路径为:
4、取得初始化配置信息
public class InitParamServlet extendsHttpServlet {private String initParam = null ; //用于保存初始化参数
public void init() throwsServletException{
System.out.println("*****************") ;
}public void init(ServletConfig config) throwsServletException{
System.out.println("#######################") ;this.initParam = config.getInitParameter("ref") ; //接收的初始化参数名称暂时为ref
}public voiddoGet(HttpServletRequest req,
HttpServletResponse resp)throwsServletException,
IOException{
System.out.println("** 初始化参数:" + this.initParam) ;
}public voiddoPost(HttpServletRequest req,
HttpServletResponse resp)throwsServletException,
IOException{this.doGet(req,resp) ;
}
}
之后,要做web.xml中配置
initparam
com.alice.servlet.InitParamServlet
ref
www.baidu.cn
initparam
/InitParamServlet
如果一个servlet中覆写了两个init()方法,只有在web.xml中配置了参数的初始化方法才可以起作用。
一般来说,取得初始化参数在一些系统架构中经常使用的。
5、取得其他内置对象
5.1 取session对象
servlet本身提供的只有request和response对象,要想取得session对象,则只能依靠request对象,因为session本身属于http协议范畴,而且在每次发生请求时,服务器都会自动为客户端设置一个cookie,session中要使用cookie机制,cookie又只能通过request取得,那么自然地session也只能通过request取得。
public class HttpSessionDemoServlet extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
HttpSession ses = req.getSession() ;
System.out.println("SESSION ID --> " + ses.getId()) ;
ses.setAttribute("username","李李") ; // 设置session属性
System.out.println("username属性内容:" + ses.getAttribute("username")) ;
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
this.doGet(req,resp) ;
}
}
之后进行web.xml进行配置
sessiondemo
com.alice.servlet.HttpSessionDemoServlet
sessiondemo
/HttpSessionDemoServlet
5.2 取application对象
public class ServletContextDemoServlet extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
ServletContext app = super.getServletContext() ;
System.out.println("真实路径:" + app.getRealPath("/")) ;
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
this.doGet(req,resp) ;
}
}
applicationdemo
com.alice.servlet.ServletContextDemoServlet
applicationdemo
/ServletContextDemoServlet