ServletConfig 类从类名上来看,就知道是 Servlet 程序的配置信息类。 Servlet 程序和 ServletConfig 对象都是由 Tomcat 负责创建,我们负责使用。...Servlet 程序默认是第一次访问的时候创建,ServletConfig 是每个 Servlet 程序创建时,就创建一个对应的 ServletConfig 对 象。...a)ServletConfig 类的三大作用 1、可以获取 Servlet 程序的别名 servlet-name 的值 2、获取初始化参数 init-param 3、获取 ServletContext...servlet-name 的值 System.out.println("HelloServlet 程序的别名是:" + servletConfig.getServletName()); // 2、...对象 System.out.println(servletConfig.getServletContext()); }
一、概述 ServletConfig对象对应web.xml文件中的元素。...例如你想获取当前Servlet在web.xml文件中的配置名,那么可以使用servletConfig.getServletName()方法获取!...ServletConfig对象是由服务器创建的,然后传递给Servlet的init()方法,你可以在init()方法中使用它!...二、ServletConfig对象的原理图 三、ServletConfig对象API演示 String getServletName():获取Servlet在web.xml文件中的配置名称, 即指定的名称
2 ServletConfig 2.1 ServletConfig概述 2.1.1 基本概念 它是Servlet的配置参数对象,在Servlet规范中,允许为每个Servlet都提供一些初始化配置。...所以,每个Servlet都一个自己的ServletConfig。它的作用是在Servlet初始化期间,把一些配置信息传递给Servlet。...private ServletConfig servletConfig; /** * 在初始化时为ServletConfig赋值 * @param config...private ServletConfig servletConfig; /** * 在初始化时为ServletConfig赋值 * @param config...System.out.println(servletConfig); //2.获取Servlet的名称 String servletName= servletConfig.getServletName
一、ServletConfig 和 ServletContext 的概念含义创建时期作用范围二、ServletConfig 和 SerlvetContext 代码表示ServletConfigServletContext...ServletConfig : 每个JVM中每个Web应用的每个Servlet一个ServletConfig。...二、ServletConfig 和 SerlvetContext 代码表示 ServletConfig ServletConfig 位于javax.servlet包内, ServletConfig 是一个接口...ServletConfig : 用于XML文件中,例如 <?xml version="1.0" encoding="UTF-8"?...接口 ServletConfig servletConfig = getServletConfig(); // -- 获取当前Servlet 中的web.xml
ServletConfig 类 ServletConfig 类从类名上来看,就知道是 Servlet 程序的配置信息类。...Servlet 程序和 ServletConfig 对象都是由 Tomcat 负责创建,我们负责使用。...Servlet 程序默认是第一次访问的时候创建,ServletConfig 是每个 Servlet 程序创建时,就创建一个对应的 ServletConfig 对象。...ServletConfig 类的三大作用 1、可以获取 Servlet 程序的别名 servlet-name 的值 2、获取初始化参数 init-param 3、获取 ServletContext...("username")); System.out.println(" 初始化参数 url 的值是;" + servletConfig.getInitParameter("url"));
那么ServletConfig 对象肯定以后要在Service 方法中使用,怎么才能保证ServletConfig 对象在Service方法中能够使用呢?...ServletConfig 从上面的知识我们知道了 : Tomcat 服务器先创建了 ServletConfig 对象,然后调用init()方法,将ServletConfig对象传给了init()方法。...先来一波,自问自答: ServletConfig 是什么? package javax.servlet; 显然 ServletConfig 是一个接口。 图片 谁去实现了这个接口呢 ?...Tomcat 服务器(WEB服务器) 创建了ServletConfig 对象,在创建Servlet 对象的时候,同时创建ServletConfig 对象....这个是 :一个 Servlet 对象就一 个 ServletConfig 对象,有 100 个 Servlet 对象就有 100 个 ServletConfig 对象。
一、ServletConfig类使用 ServletConfig类时Servlet程序的配置信息类,使用Servlet可以实现 获取Servlet程序的别名,既web.xml中配置servlet-name..., 其中init方法中包含了ServletConfig类作为方法参数,可以直接使用servletConfig获取web.xml中的一些配置 @Override public void init(ServletConfig...@Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println...对象都是由Tomcat负责创建的,Servlet程序默认是第一次访问的时候创建,ServletConfig是每个Servlet程序创建是都会创建一个对应的ServletConfig对象 在其他地方使用ServletConfig...对象 在其他方法中使用ServletConfig可以直接调用getServletConfig方法来获取ServletConfig对象;在controller包中新增HalloServlet,在doPost
Servlet入门 - ServletConfig的使用介绍 ServletConfig 类 ServletConfig 类从类名上来看,就知道是 Servlet 程序的配置信息类。...Servlet 程序和 ServletConfig 对象都是由 Tomcat 负责创建,我们负责使用。...Servlet 程序默认是第一次访问的时候创建,ServletConfig 是每个 Servlet 程序创建时,就创建一个对应的 ServletConfig 对象。...ServletContext 对象 image-20201112000338669 @Override public void init(ServletConfig servletConfig) throws...()); } 使用总结 一般 ServletConfig 是在提供框架初始化参数的时候使用的,那么也就需要使用 web.xml 配置框架的 Servlet 的时候提供传递参数。
接下来我们要说的是Servlet的四个类: ServletConfig对象,ServletContext对象、request对象,response对象 从图中我们可以知道这几个对象之间的联系!...一、ServletConfig对象 1.1、获取ServletConfig对象 1)使用初始化方法获得一个ServletConfig对象 ? ...2)通过继承父类(GenericServlet)的方法得到一个ServletConfig对象 ServletConfig config = this.getServletConfig(); 1.2...、ServletConfig对象作用 ? ...注意:在上面我们所分析的源码过程中,我们就知道,其实可以不用先获得ServletConfig,然后在获取其各种参数,可以直接使用其方法,比如上面我们用的ServletConfig().getServletName
2.用ServletConfig从init-param获取: 就单纯获取init-param而言,上一种和这一种方法一样。不过ServletConfig还有很多其他的功能。见例子。...ServletHello1.java: package com; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig...HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletConfig
文章目录 一、ServletConfig 1.ServletConfig概述 1.1 基本概念 1.2 生命周期 2.ServletConfig的使用 2.1 如何获取 2.2 如何配置 2.3...常用方法 ---- 一、ServletConfig 1.ServletConfig概述 1.1 基本概念 它是Servlet的配置参数对象,在Servlet规范中,允许为每个Servlet都提供一些初始化配置...private ServletConfig servletConfig; /** * 在初始化时为ServletConfig赋值 * @param config...private ServletConfig servletConfig; /** * 在初始化时为ServletConfig赋值 * @param config...System.out.println(servletConfig); //2.获取Servlet的名称 String servletName= servletConfig.getServletName
*/ @Override public void init(ServletConfig config) throws ServletException { // 把tomcat传递的Servletconfig...因为在GenericServlet中的init(ServletConfig)方法中保存 了ServletConfig对象,如果覆盖了保存ServletConfig的代码,那么就不能再使用...ServletConfig了。...config实例变量,并在init(ServletConfig)方法中把参数ServletConfig赋给了实例变量。...等ServletConfig的方法。
(出生之后) * */ @Override public void init(ServletConfig servletConfig) throws ServletException {...2 GenericServlet的init()方法 在GenericServlet中,定义了一个ServletConfig config实例变量,并在init(ServletConfig)方法中把参数ServletConfig...3 实现了ServletConfig接口 GenericServlet还实现了ServletConfig接口,所以可以直接调用getInitParameter()、getServletContext...()等ServletConfig的方法。...因为在GenericServlet中的init(ServletConfig)方法中保存了ServletConfig对象,如果覆盖了保存ServletConfig的代码,那么就不能再使用ServletConfig
此接口中的方法有以下几个: public void init(ServletConfig config) throws ServletException; public ServletConfig getServletConfig...2.ServletConfig接口分析 此接口为Servlet配置抽象接口,定义了获取Servlet信息的相关接口,接口列表如下: //获取Servlet名称,即是web.xml中配置的servlet-name...此类中维护了一个ServletConfig变量,定义方式如下: private transient ServletConfig config; 添加transient修饰的作用:序列化的时候不包含此字段...= config; this.init(); } init方法是由Servletr容器调用,因此,web.xml中Servlet配置转化为ServletConfig的工作因该是由容器完成的。...其中ServletConfig接口的方法实现方式基本如下: public String getServletName() { ServletConfig sc = getServletConfig
var1) throws ServletException; ServletConfig getServletConfig(); void service(ServletRequest var1,...getServletConfig(); 方法与其他方法不同,返回值类型ServletConfig,向下点ServletConfig,发现也是一个接口 public interface ServletConfig...的操作 private transient ServletConfig config; (transient 的关键字为不对该对象进行序列化操作) ServletConfig讲解 1.1、配置Servlet...方法时,将ServletConfig对象传递给servlet。...进而,我们通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
ServletConfig接口 接口结构 方法解析 ServletConfig 与ServletContext 四. GenericServlet抽象类 类结构 方法解析 五....ServletConfig接口 接口结构 ?...,为自己附加了ServletConfig装饰身份( private transient ServletConfig config;装饰者设计模式参见《设计模式之装饰者模式》)。...在具体实现中,GenericServlet类包装了一个ServletConfig接口的实例,通过该实例来实现ServletConfig接口中的方法。 类结构 ?...Config) 方法, 将传递的 ServletConfig 对象的引用保存到私有成员变量中, 使得GenericServlet 对象和一个ServletConfig 对象关联.
java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet; import javax.servlet.ServletConfig...void destroy() { // TODO Auto-generated method stub } @Override public ServletConfig...getServletInfo() { // TODO Auto-generated method stub return null; } private ServletConfig...servletConfig; @Override public void init(ServletConfig servletConfig) throws ServletException...{ // TODO Auto-generated method stub this.servletConfig=servletConfig; } @Override
(稍后会介绍servletConfig接口)的实例,然后调用servlet接口的init方法并且传入一个servletconfig实例,完成servlet的初始化。...的配置产生一个servletconfig的对象,随后调用servlet的接口的init方法,并且将产生的servletconfig的对象当作参数传入。...servletconfig的实例 GenericServlet也包括了Servlet與ServletConfig所定義方法的簡單實作,實作內容主要是透過ServletConfig來取得一些相關資訊,例如...的信息,而不用意识到servletconfig的存在。...但实际上,当整个Web应用程序加载进入Web容器之后,容器会生成一个ServletContext对象作为整个应用程序的代表,并设定给ServletConfig,你只要通过调用ServletConfig的
为何要设计成这样呢 Servlet 顶层类关联图 从上图可以看出 Servlet 规范就是基于这几个类运转的,与 Servlet 主动关联三个类 ServletConfig、ServletRequest...仔细查看 ServletConfig 接口中声明的方法,发现都是为了获取这个 Servlet 的一些配置属性,而这些配置属性可能在 Servlet 运行时被用到。...,它们通常都是作为运输工具来传递交互结果 ServletConfig 是在 Servlet init 时由容器传过来的,那么 ServletConfig 到底是个什么对象呢?...ServletConfig,ServletContext在 Tomcat 容器中的类关系 可以看出 StandardWrapper 和 StandardWrapperFacade 都实现了 ServletConfig...所以传给 Servlet 的是 StandardWrapperFacade 对象,它能够保证从 StandardWrapper 中拿到 ServletConfig 所规定的数据,而又不把 ServletConfig
2.1 Servlet3.1中Servlet的接口定义如下 public interface Servlet { public void init(ServletConfig config)...2.2 ServletConfig接口定义 public interface ServletConfig { public String getServletName(); public...和ServletConfig里的方法,比如获取ServletConfig中的方法时候可以直接调用,而无须调用getServletConfig().getServletContext()了,不过底层实现其实是在内部调用了...,代码如下: public ServletContext getServletContext() { ServletConfig sc = getServletConfig();...public void init(ServletConfig config) throws ServletException { this.config = config; this.init();
领取专属 10元无门槛券
手把手带您无忧上云