servlet的生命周期
2017-03-20 02:00
232 查看
1、当servlet首次被调用的时候,会触发init函数,该函数会把servlet转载到内存中,init函数只会被调用一次。
2、然后去调用servlet的service函数(即大家常见的req和res)
3、当第二次访问,就直接调用service函数
4、当web应用reload 或者 关闭tomcat 或者 关机都会调用destroy函数。
详细的工作原理见:点击打开链接
servlet的基本函数:
1)实现servlet接口package com.lmy1; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class Myfirstweb implements Servlet { //该函数用于初始化Servlet,即把servlet加载到内存中 //该函数只会被调用一次 public void init(ServletConfig config) throws ServletExpection{ } //得到ServletConfig对象 public ServletConfig getServletConfig(){ return null; } //该函数是服务函数,我们的业务逻辑代码 //该函数每次都会被调用 public void service(ServletRequest req, ServletResponse res) throws ServletExpection, java.io.IOException{ //也可以连接数据库,便于更新数据 //在控制台输出 System.out.println("hello"+ new java.util.Date()); //在浏览器输出 res.getWriter().println("hello"+ new java.util.Date()); } //该函数得到servlet的配置信息 public java.lang.String getServletInfo(){ return null; } //销毁Servlet,从内存中清除,该函数被调用一次 public void destroy(){ } }
2)继承GenericServlet(略)
3)继承HttpServlet
package com.lmy; import javax.servlet.*; import javax.servlet.http*; import java.io.*; public class MyfirstHttpservlet extends HttpServlet { //在httpServlet中,get 和 post 提交 默认get //<form action="" method= ""> protect void doGet (HttpServletRequest req, HttpServletResponse resp) throw ServletExpection, java.io.IOException{ } protect void doPost (HttpServletRequest req, HttpServletResponse resp) throw ServletExpection, java.io.IOException{ } }
4、
synchronized() 解决servlet的单例 引起的线程安全问题。
1)、一个变量多个用户共享的话应该加同步机制 , 如下
synchronized(对象){
//同步代码
}
2)、如果不需要共享则直接在doGet 或者 doPoet里面定义
package com.lmy1; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class servlet1 extends HttpServlet { int ticket = 0; int i = 0;//成员变量 /** * Constructor of the object. */ public servlet1() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int j = 0;//局部变量 i++; j++; //一般开发人员讲doGet()和doPos()t和二为一 response.getWriter().println("hello"+new java.util.Date().toString()+"i="+i+"j="+j); /*ticket++; synchronized (this) { if(ticket>0){ System.out.println("你能买到票"); try { Thread.sleep(10*1000); } catch (Exception e) { // TODO: handle exception } ticket--; }else{ System.out.println("你不能买到票"); } }*/ } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } public void init() throws ServletException { // Put your code here } }
5、servlet的<load-on-startup>
(初始化任务,应用于定时日志,定时备份数据,定时发送电子邮件)
相关文章推荐
- [转]servlet的执行原理与生命周期
- Servlet生命周期
- Servlet 原理及生命周期(第一天)
- Servlet生命周期及运行过程
- JavaWeb三大组件之Servlet初次见面及生命周期
- servlet的生命周期
- Servlet的生命周期
- Servlet的生命周期及工作原理
- Servlet的生命周期,Servlet和CGI的区别
- ServLet工作原理图解和生命周期
- JavaWeb-3 Servlet之doGet doPost请求&生命周期简述
- Servlet生命周期和JSP
- Servlet学习笔记(一)之Servlet原理、初始化、生命周期、结构体系
- Servlet生命周期 HttpServlet Mapping配置
- Servlet的生命周期
- Java高级篇(四一)------Servlet生命周期
- [03] Servlet继承关系和生命周期
- Servlet的生命周期和API
- servlet生命周期:连接mysql数据库实例
- javaWeb之Servlet的工作原理与生命周期