Servlet--ServletConfig接口,GenericServlet类

2024-06-17 11:48

本文主要是介绍Servlet--ServletConfig接口,GenericServlet类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • ServletConfig接口
定义:public interface ServletConfig
这个接口定义了一个对象, 通过这个对象, Servlet 引擎配置一个 Servlet 并且允许 Servlet获得一个有关它的 ServletContext 接口的说明。每一个 ServletConfig 对象对应着一个唯一的Servlet。
方法:
1、getInitParameter
public String getInitParameter(String name);
这个方法返回一个包含 Servlet 指定的初始化参数的 String。 如果这个参数不存在, 返加空值。
2、getInitParameterNames
public Enumeration getInitParameterNames();
这个方法返回一个列表 String 对象,该对象包括 Servlet 的所有初始化参数名。如果Servlet 没有初始化参数,getInitParameterNames 返回一个空的列表。
3、getServletContext
public ServletContext getServletContext();
返回这个 Servlet 的 ServletContext 对象。



  • GenericServlet接口
public abstract class GenericServlet implements Servlet,ServletConfig, Serializable;
这个类的存在使得编写 Servlet 更加方便。它提供了一个简单的方案,这个方案用来执
行有关Servlet生命周期的方法以及在初始化时对ServletConfig对象和ServletContext对象进
行说明。
方法
1、destroy
public void destroy();
在这里 destroy 方法不做任何其他的工作。
2、getInitParameter
public String getInitParameter(String name);
这是一个简便的途径,它将会调用 ServletConfig 对象的同名的方法。
3、getInitParameterNames
public Enumeration getInitParameterNames();
这是一个简便的途径,它将会调用 ServletConfig 对象的同名的方法。
4、getServletConfig
public ServletConfig getServletConfig();
返回一个通过这个类的 init 方法产生的 ServletConfig 对象的说明。
5、getServletContext
public ServletContext getServletContext();
这是一个简便的途径,它将会调用 ServletConfig 对象的同名的方法。
6、getServletInfo
public String getServletInfo();
返回一个反映 Servlet 版本的 String。
7、init
public void init() throws ServletException;
public void init(ServletConfig config) throws ServletException;

init(ServletConfig config)方法是一个对这个 Servlet 的生命周期进行初始化的简便的途径。

init(ServletConfig config)方法会存储config对象然后调用init() 。 如果你重载了这个方法 ,你必须调用 super.init(config),这样 GenericServlet 类的其他方法才能正常工作。

init()方法是用来让你对 GenericServlet 类进行扩充的,使用这个方法时,你不需要存储config 对象,也不需要调用 super.init(config)。
8、 log
public void log(String msg);
public void log(String msg, Throwable cause);
通过 Servlet content 对象将 Servlet 的类名和给定的信息写入 log 文件中。
9、 service
public abstract void service(ServletRequest request, ServletResponse response) throws ServletException, IOException;
这是一个抽象的方法,当你扩展这个类时,为了执行网络请求,你必须执行它。

package javax.servlet;import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration;public abstract class GenericServlet implements Servlet, ServletConfig, Serializable
{//这里封装一个ServletConfig对象,每一个 ServletConfig 对象对应着一个唯一的Servlet。private transient ServletConfig config;public void destroy(){}//这是一个简便的途径,它将会调用 ServletConfig 对象的同名的方法。以下3个方法都是:public String getInitParameter(String name){return getServletConfig().getInitParameter(name);}public Enumeration getInitParameterNames(){return getServletConfig().getInitParameterNames();}//Servlet引擎配置一个 Servlet,并且允许 Servlet,获得一个有关它的 ServletContext 接口的说明public ServletContext getServletContext(){return getServletConfig().getServletContext();}//我们常常在自己写的Servlet用这个方法,其实就是在这里被继承过去的public ServletConfig getServletConfig(){return this.config;}public String getServletInfo(){return "";}//重写Servlet的init方法public void init(ServletConfig config) throws ServletException{this.config = config;init();}public void init() throws ServletException{}public void log(String msg){getServletContext().log(getServletName() + ": " + msg);}public void log(String message, Throwable t){getServletContext().log(getServletName() + ": " + message, t);}//推迟到子类实现,这里仍然是抽象方法public abstract void service(ServletRequest paramServletRequest, ServletResponse paramServletResponse) throws ServletException, IOException;public String getServletName(){return this.config.getServletName();}
}

这篇关于Servlet--ServletConfig接口,GenericServlet类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1069357

相关文章

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

Java中的Closeable接口及常见问题

《Java中的Closeable接口及常见问题》Closeable是Java中的一个标记接口,用于表示可以被关闭的对象,它定义了一个标准的方法来释放对象占用的系统资源,下面给大家介绍Java中的Clo... 目录1. Closeable接口概述2. 主要用途3. 实现类4. 使用方法5. 实现自定义Clos

java对接第三方接口的三种实现方式

《java对接第三方接口的三种实现方式》:本文主要介绍java对接第三方接口的三种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录HttpURLConnection调用方法CloseableHttpClient调用RestTemplate调用总结在日常工作

Java 的 Condition 接口与等待通知机制详解

《Java的Condition接口与等待通知机制详解》在Java并发编程里,实现线程间的协作与同步是极为关键的任务,本文将深入探究Condition接口及其背后的等待通知机制,感兴趣的朋友一起看... 目录一、引言二、Condition 接口概述2.1 基本概念2.2 与 Object 类等待通知方法的区别

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

Java对接Dify API接口的完整流程

《Java对接DifyAPI接口的完整流程》Dify是一款AI应用开发平台,提供多种自然语言处理能力,通过调用Dify开放API,开发者可以快速集成智能对话、文本生成等功能到自己的Java应用中,本... 目录Java对接Dify API接口完整指南一、Dify API简介二、准备工作三、基础对接实现1.

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决

go中空接口的具体使用

《go中空接口的具体使用》空接口是一种特殊的接口类型,它不包含任何方法,本文主要介绍了go中空接口的具体使用,具有一定的参考价值,感兴趣的可以了解一下... 目录接口-空接口1. 什么是空接口?2. 如何使用空接口?第一,第二,第三,3. 空接口几个要注意的坑坑1:坑2:坑3:接口-空接口1. 什么是空接