当前位置: 移动技术网 > IT编程>开发语言>Java > Servlet生命周期、通过继承 HttpServlet 实现 Servlet 程序、ServletConfig 类、ServletContext 类 最全详解

Servlet生命周期、通过继承 HttpServlet 实现 Servlet 程序、ServletConfig 类、ServletContext 类 最全详解

2020年07月22日  | 移动技术网IT编程  | 我要评论
Servlet一.什么是 Servlet​1.Servlet 是 java EE 的规范之一。也就是接口。Servlet 接口定义了一套网络请求的规范​2.Servlet 是 javaweb 的三大组件之一。 javaweb 的三大组件分为 Servlet程序、Filter 过滤器、Listener 监听器​3.Servlet 是运行在服务器上的一个 java 程序。作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层(可以接收客户端

Servlet

一.什么是 Servlet

​ 1.Servlet 是 java EE 的规范之一。也就是接口。Servlet 接口定义了一套网络请求的规范

​ 2.Servlet 是 javaweb 的三大组件之一。 javaweb 的三大组件分为 Servlet程序、Filter 过滤器、Listener 监听器

​ 3.Servlet 是运行在服务器上的一个 java 程序。作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层(可以接收客户端发送的请求,并且响应 数据给客户端)。

二.Servlet 的生命周期

1.初始化阶段

Servlet 容器加载 Servlet,加载完成后,Servlet 容器会创建Servlet 实例并调用 init() 方法,init() 方法只会调用一次。

Servlet 容器会在以下几种情况下装载 Servlet:

(1). Servlet 容器启动时自动装载Servlet ,自动装载需要在web.xml中设置

(2). 在 Servlet 容器启动后,客户首次向Servlet 发送请求

(3). Servlet 类文件被修改后,重新装载

2.处理客户端请求阶段

(1). 对用户的请求,Servlet 容器会创建一个请求对象(ServletRequest)和响应对象(ServletResponse),并且激活Servlet 的 service() 方法,以请求对象和响应对象作为参数。

(2). service()方法获得关于请求对象的信息,处理请求(例如 doGet()、doPost() 等方法),访问其他资源,获得需要的信息;

3. 终止阶段

当web应用被终止,或Servlet容器终止运行、或者Servlet 重新装载时,Servlet 容器会调用 Servlet 的 destory() 方法

三.通过继承 HttpServlet 实现 Servlet 程序

1.步骤

(1). 编写一个类去继承 HttpServlet 类

(2). 根据业务需要重写 doGet()、doPost() 请求

(3). 在 web.xml 中配置 Servlet 程序的访问地址

2.Servlet 类的继承体系

在这里插入图片描述

四. ServletConfig 类

1.介绍

Servlet 和 ServletConfig 对象都是由 Tomcat 创建。Servlet 程序默认在第一次访问的时候创建,ServletConfig 时每个 Servlet 程序创建时,就创建一个对应的 ServletConfig 对象 (注意:一个 Servlet 只能对应一个 ServletConfig 对象。)

2. 三大作用

	1. 获取 Servlet 程序的别名 Servlet-name 的值
	2.  获取初始化参数 init-param     (initi-param 在 web.xml 中配置)
    3. 获取 ServletConfig 对象

示例代码:

@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

     

        System.out.println("HelloServlet2 的doGet方法");
        // 也可以使用.
        ServletConfig servletConfig = getServletConfig();
        System.out.println(servletConfig);

        //        2、获取初始化参数init-param
        System.out.println("初始化参数username的值是;" + servletConfig.getInitParameter("username"));
        System.out.println("初始化参数url的值是;" + servletConfig.getInitParameter("url"));


    }

3.重写 init() 方法注意点 (自定义类继承 HttpServlet 时)

当在自定义类并且是继承了 HttpServlet 的自定义类当中重写 init() 类方法时,一定要加上 super.init(config); 也就是调用父类的 init(ServletConfig) 方法。

getServletConfig() 是 javax.servlet.GenericServlet 类的方法

在这里插入图片描述

在这里插入图片描述

查看 GenericServlet 类源码发现,getServletConfig() 方法为

返回一个config对象

在这里插入图片描述
而 GenericServlet 类当中的 init() 方法,是调用init() 方法时将config保存起来
在这里插入图片描述
当重写子类的 init() 方法时,再次调用是调用的是子类的 init() 方法,父类当中的保存操作就没有执行,所以要使用 super.init(config) 调用父类方法

五. ServletContext 类

1.介绍

	1. ServletContext 是一个接口,表示 Servlet 上下文对象
	2. 一个 web 工程只有一个 ServletContext 对象实例
	3. ServletContext 对象是一个域对象
	4. ServletContext 在 web 工程部署启动的时候创建,在 web 工程停止的时候销毁

域对象 :在一定范围内,使多个 Servlet 共享数据。在一个 web 应用中多个Servlet 程序公享一个 ServletContext 对象,多个 Servlet 程序之间可以通过 ServletContext 对象实现通讯

在这里插入图片描述

2. 四大作用

	1. 获取 web.xml 中配置的上下文参数 context-param
	2. 获取当前的工程路径
	3. 获取工程部署后在服务器硬盘上的绝对路径
	4. 像 Map 一样存取数据

3.演示代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        1、获取web.xml中配置的上下文参数context-param
        ServletContext context = getServletConfig().getServletContext();

        String username = context.getInitParameter("username");
        System.out.println("context-param参数username的值是:" + username);
        System.out.println("context-param参数password的值是:" + context.getInitParameter("password"));
//        2、获取当前的工程路径,格式: /工程路径
        System.out.println( "当前工程路径:" + context.getContextPath() );
//        3、获取工程部署后在服务器硬盘上的绝对路径
        /**
         *  / 斜杠被服务器解析地址为:http://ip:port/工程名/  映射到IDEA代码的web目录<br/>
         */
        System.out.println("工程部署的路径是:" + context.getRealPath("/"));
        
    }

像 Map 一样存取数据

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取ServletContext对象
        ServletContext context = getServletContext();
        System.out.println(context);
        System.out.println("保存之前: Context1 获取 key1的值是:"+ context.getAttribute("key1"));

        context.setAttribute("key1", "value1");

        System.out.println("Context1 中获取域数据key1的值是:"+ context.getAttribute("key1"));
    }

注意:

ServletContext 对象只有保存之后再能获取到

在这里插入图片描述

同样在 Servlet2 中也能 get 到 ServletContext 对象

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = getServletContext();
        System.out.println(context);
        System.out.println("Context2 中获取域数据key1的值是:"+ context.getAttribute("key1"));
    }

在这里插入图片描述

本文地址:https://blog.csdn.net/weixin_43724919/article/details/107476849

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网