java servlet的生命周期_javaWeb中servlet开发(3)——Servlet生命周期

生命周期:是一个程序的存在周期,servlet由于是受容器的管理,所以容器来决定其生命周期

1、servlet生命周期

32bf64989151aa07671a08781f516734.png

2780c0c817f8d61a5f0b024ea208cb58.png

2、servlet生命周期对应的方法

5328c8614fb36fe85322a9e6ac675426.png

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

访问路径为:

93fefa1e1710aef93cdab4a525e31500.png

4、取得初始化配置信息

3340dfcfc49dce51febfc59cacb3f412.png

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对象

65938103bbce7d977cf489e32ffb92ff.png

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对象

f302ee9c2564c3076e28a35420e2e2f1.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值