Servlet的生命周期
Servlet的生命周期
Servlet的生命周期是由tomcat服务器来控制的。
1 构造方法:
创建servlet对象的时候调用。默认情况下,第一访问servlet就会创建servlet对象只创建一次。说明servlet对象在tomcat中是单实例的。
2初始化 init方法
当创建完servlet对象的时候会调用init()方法,只调用一次。
1>ServletConfig对象
获取serlvet初始化参数
调用的时候用
this.getInitParameter(参数名)
2>ServletContext对象
获取上下文信息
A 获取全局参数
ServletContext sc= this.getServletContext()
sc.getInitParameter("ecode");
sc.getContextPath() 项目路径 --项目名字
sc.getRealPath() 发布路径 --发布后在tomcat中的路径
3 调用服务 service 方法 其中就包含doGet doPost等方法
每次发送请求的时候调用。可以调用n次。
4 销毁 destory 方法
销毁servlet对象的时候调用,停止服务器或者重新部署的web项目的时候销毁servlet就会调用destory方法
package com.bw.reg.servlet; 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; import com.bw.reg.bean.Users;
import com.bw.reg.service.ServiceImpl; public class LoginServlet extends HttpServlet { /**
* Constructor of the object.
*/
public LoginServlet() {
super();
System.out.println("这里是构造方法");
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
System.out.println("销毁");
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*
*/ //这里是service方法
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { boolean isLogin = false; //设置编码类型
request.setCharacterEncoding("UTF-8"); //获取登入时输入的用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("pwd"); //创建users的对象
Users users = new Users();
//给users的对象设置登入时输入的用户名和密码
users.setUserName(username);
users.setPwd(password);
//登入时的查找
ServiceImpl serImpl = new ServiceImpl();
isLogin = serImpl.find(users); if(isLogin){
System.out.println("登入成功!!");
}else {
System.out.println("登录失败");
}
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
System.out.println("这里是初始化方法");
} }
Servlet的生命周期的更多相关文章
- Servlet的生命周期及工作原理
Servlet生命周期分为三个阶段: 1,初始化阶段 调用init()方法 2,响应客户请求阶段 调用service()方法 3,终止阶段 调用destroy()方法 Servlet初始化阶段: 在 ...
- Servlet的生命周期+实现方式
1.Servlet的生命周期: (1)被创建: 默认情况下,Servlet第一次被访问时,被服务器创建.会调用init()方法. 一个 ...
- Servlet基础(二) Servlet的生命周期
Servlet基础(二) Servlet的生命周期 Servlet的生命周期可以分为三个阶段: 1.初始化阶段 2.响应客户请求阶段 3.终止阶段 Servlet的初始化阶段 在下列时刻Servlet ...
- JSP Servlet WEB生命周期
[转载] JavaWeb的生命周期是由Servlet容器来控制的总的来说分为三个阶段1.启动阶段:加载web应用相关数据,创建ServletContext对象,对Filter和servlet进行初始化 ...
- servlet的生命周期与运行时的线程模型
第 14 章 生命周期 注意 讲一下servlet的生命周期与运行时的线程模型,对了解servlet的运行原理有所帮助,这样才能避免一些有冲突的设计. 如果你不满足以下任一条件,请继续阅读,否则请跳过 ...
- Servlet/JSP-01 Servlet及其生命周期
一.起步 1.新建一个类继承Servlet接口 public class HelloServlet implements Servlet { @Override public void destroy ...
- JavaWeb学习之Servlet(二)----Servlet的生命周期、继承结构、修改Servlet模板
[声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4140466.html 一.http协议回顾: 在上一篇文章中:JavaW ...
- Servlet学习笔记(1)--第一个servlet&&三种状态对象(cookie,session,application)&&Servlet的生命周期
servlet的404错误困扰了两天,各种方法都试过了,翻书逛论坛终于把问题解决了,写此博客来纪念自己的第一个servlet经历. 下面我会将自己的编写第一个servlet的详细过程提供给初学者,大神 ...
- servlet的生命周期与工作原理、使用!
概念: Servlet是一个java程序运行在服务器上,处理客户端请求并做粗响应的程序!Servlet是和平台无关的服务器组件,它运行在Servlet容器中,Servlet容器 负责servlet和客 ...
随机推荐
- 如果你也会C#,那不妨了解下F#(2):数值运算和流程控制语法
本文链接:http://www.cnblogs.com/hjklin/p/fs-for-cs-dev-2.html 一些废话 一门语言火不火,与语言本身并没太大关系,主要看语言的推广. 推广得好,用的 ...
- TypeScript 强类型 JavaScript – Rafy Web 框架选型
今天看到了 AngularJs 2.0 版本将基于 TypeScript 构建 的消息.与同事们对 TypeScript 展开了讨论.本文记录一些个人的想法. 理想的 JavaScript 开发模式 ...
- python 数据类型---布尔型& 字符串
python数据类型-----布尔型 真或假=>1或0 >>> 1==True True >>> 0==False True python 数据类型----- ...
- Backbone中的model和collection在做save或者create操作时, 如何选择用POST还是PUT方法 ?
Model和Collection和后台的WEB server进行数据同步非常方便, 都只需要在实行里面添加一url就可以了,backbone会在model进行save或者collection进行cre ...
- 解决 Tomcat Server in Eclipse unable to start within 45 seconds 不能启动的问题
1.在 Eclipse 下方 Servers TAB页,双击 "Tomcat 7.0 at localhost": 2.在右上角处点开 Timeouts 的设定,修改Start( ...
- Java面试基础概念总结
面向对象软件开发的优点有哪些? 答:开发模块化,更易维护和修改:代码之间可以复用:增强代码的可靠性.灵活性和可理解性. 多态的定义? 答:多态是编程语言给不同的底层数据类型做相同的接口展示的一种能力. ...
- jdk源码分析ArrayDeque
ArrayDeque 数组循环队列,这个数据结构设计的挺有意思的. 据说此类很可能在用作堆栈时快于 Stack,在用作队列时快于 LinkedList. 一.容量 1.1默认容量是8=2^3 1.2指 ...
- Android Git 客户端
1.tortoisegit Git下载地址: https://tortoisegit.org/download/ SVN下载地址: https://tortoisesvn.net/downloads. ...
- http转https 和 微信小程序设置了合法请求域名,小程序一直提示不在合法域名列别中
hotapp 有免费的https proxy ,可以免费代理请求任何http或者https服务,只要设置好合法域名为https://wxapi.hotapp.cn , 就可以请求网址如请求小程序联盟的 ...
- Android入门(一)
原文链接:http://www.orlion.ga/387/ 一.安卓的系统架构 1. linux内核层,这一层为安卓设备提供底层的驱动 系统运行库层,这一层通过一些C/C++库来为Android系统 ...