1.建Web project“2Servlet_Basic”
2.建包com.amaker.servlet
3.建类“ServletBasic.java”
package com.amaker.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//继承HttpServet,doGet alt+/,sysout alt+/
public class ServletBasic extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 3616093822222878695L; public void init() throws ServletException {
System.out.println("init");
} protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doGet"); } protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doPost"); } protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("servlet"); }
public void destroy() {
System.out.println("destroy");
} }

4.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>basicservlet</servlet-name>
<servlet-class>com.amaker.servlet.ServletBasic</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>basicservlet</servlet-name>
<url-pattern>/servlet/basic</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>

5.MyHtml.html(WebRoot文件夹)

<!DOCTYPE html>
<html>
<head>
<title>MyHtml.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body>
<!--先调用service方法,没有service调用post方法,“method”上有注明-->
<form action="/2Servlet_Basic/servlet/basic" method="post">
<input type="submit" value ="click">
</form>
</body>
</html>
访问:
按钮进入后发现console
  init
  servlet
刷新页面 console
  init
  servlet
  servlet

总结:
servlet的生命周期:
(1)加载实例(创建一个servlet实例)
(2)调用init()方法(仅用一次)
(3)调用service方法(每次请求一次,表明多个用户共享一个servlet实例)
(4)调用destory()方法(tomcat销毁时使用)

2.servlet 生命周期的更多相关文章

  1. Servlet 生命周期与web容器的关系

    servlet生命周期由web容器(如tomcat)管理,初始化一次,直到web容器关闭才会被销毁.1.servlet是单例多线程,每个请求过来容器都会启用一个新线程 2.servlet在容器中保持单 ...

  2. 测试Servlet生命周期学习笔记

    测试环境:windows xp旗舰版 软件环境:myclipse8.5+tomcat7.0 ****************************************************** ...

  3. Servlet生命周期及工作原理

    1 Servlet生命周期Servlet 生命周期:Servlet 加载--->实例化--->服务--->销毁. init():在Servlet的生命周期中,仅执行一次init()方 ...

  4. Java开发之Servlet生命周期

    Servlet会在服务器启动或第一次请求该Servlet的时候开始生命周期,在服务器结束的时候结束生命周期.无论请求多少次Servlet,最多只有一个Servlet实例.多个客户端并发请求Servle ...

  5. Java Servlet系列之Servlet生命周期

    Servlet生命周期定义了一个Servlet如何被加载.初始化,以及它怎样接收请求.响应请求,提供服务.在讨论Servlet生命周期之前,先让我们来看一下这几个方法: 1. init()方法 在Se ...

  6. servlet生命周期与工作原理

    →   Jsp的本质是Servlet,Servlet是服务器端的小程序,运行在服务器,用于处理及响应客户端的请求. Servlet和JSP的区别: servlet是特殊的Java类,必须继承HttpS ...

  7. javaWeb中servlet开发(3)——Servlet生命周期

    生命周期:是一个程序的存在周期,servlet由于是受容器的管理,所以容器来决定其生命周期 1.servlet生命周期 2.servlet生命周期对应的方法 3.servlet生命周期代码 publi ...

  8. Servlet 生命周期、工作原理

    按照单例的编码规则,Servlet本身只是一个Java,结构并不是单例结构. 只是Web容器在维护这些Servlet的时候只给创建一个实例存在JVM中,用户请求服务时,服务器只调用它已经实例化好的Se ...

  9. Servlet生命周期+工作原理

    Servlet生命周期+工作原理 1.Servlet的生命周期:     Servlet加载,加载,服务,销毁. 2.典型函数解释:     Init():这个函数是用来初始化Servlet对象的.在 ...

  10. 【drp 9】Servlet生命周期

    一.基本概念 Servlet(Server Applet):全称Java Servlet,是用Java编写的服务器端程序.其主要功能在于交互式地浏览和修改数据,生成动态Web内容.狭义的Servlet ...

随机推荐

  1. Ubuntu下安装FTP服务及使用(VSFTPD详细设置)(二)

    vsftpd 作为一个主打安全的FTP服务器,有很多的选项设置.下面介绍了vsftpd的配置文件列表,而所有的配置都是基于vsftpd.conf这个配置文件 的.本文将提供完整的vsftpd.conf ...

  2. 代码片段 - Golang 实现集合操作

    ------------------------------------------------------------ 如果用于多例程,可以使用下面的版本: -------------------- ...

  3. reactor官方文档译文(1)Reactor简介

    原文地址:http://projectreactor.io/docs/reference/ Reactor简介 Reactor是一个基础库,用在构建实时数据流应用.要求有容错和低延迟至毫秒.纳秒.皮秒 ...

  4. CSRF 攻击的应对之道--转

    http://www.ibm.com/developerworks/cn/web/1102_niugang_csrf/ 简介: CSRF(Cross Site Request Forgery, 跨站域 ...

  5. C. Mobile phones

    Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows ...

  6. Sum of divisors

    Problem Description mmm is learning division, she's so proud of herself that she can figure out the ...

  7. MVC3+Linq to sql 显示数据库中数据表的数据

    1:首先创建asp.net mvc3应用程序 2:创建项目完成后 找到controllers文件鼠标右击选择添加控制器 3 为models文件夹添加一个linq to sql类文件,然后把数据库中的数 ...

  8. recent.css常用的页面初始化样式

    <style> @charset "utf-8"; body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form ...

  9. Android小项目之九 两种上下文的区别

    ------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 按惯例,写在前面的:可能在学习Android的过程中,大家会和我一样,学习过大量的基础知识,很多的知识点 ...

  10. 转载:Character data is represented incorrectly when the code page of the client computer differs from the code page of the database in SQL Server 2005

    https://support.microsoft.com/en-us/kb/904803 Character data is represented incorrectly when the cod ...