ServletConfig接口

      

A servlet configuration object used by a servlet container to pass information to a servlet during initialization.

servlet 容器使用的 servlet 配置对象,该对象在初始化期间将信息传递给 servlet。

web.xml配置

  <servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.qf.servlet.HelloServlet3</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>wxf</param-value>
</init-param>
<init-param>
<param-name>age</param-name>
<param-value>24</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

servlet类编写

 public class HelloServlet extends HttpServlet {

     @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet");
testServletConfig();
} public void testServletConfig() { //得到servlet配置对象 专门用于在配置servlet的信息
ServletConfig config = getServletConfig(); //获取到的是配置servlet里面servlet-name 的文本内容
String servletName = config.getServletName();
System.out.println("servletName==="+servletName);
System.out.println("-----------"); //获取ServletContext
ServletContext context = config.getServletContext();
String path = context.getContextPath();
System.out.println("path==="+path);
System.out.println("-----------"); //获取某一具体的参数
String name = config.getInitParameter("name");
System.out.println("name==="+name);
System.out.println("-----------"); //获取所有的参数名称
Enumeration<String> names = config.getInitParameterNames();
//遍历取出所有的参数名称
while (names.hasMoreElements()) {
String key = (String) names.nextElement();
String value = config.getInitParameter(key);
System.out.println("key==="+key + " value==="+value); }
}
}

url(http://localhost:8080/HelloServlet/hello)访问后控制台输出

doGet
servletName===hello
-----------
path===/HelloServlet
-----------
name===wxf
-----------
key===name value===wxf
key===age value===24

ServletConfig的作用:可以在web.xml的<init-param>标签中配置一些变量值,并在servlet中根据param-name获取

servlet的ServletConfig接口的更多相关文章

  1. Servlet(7)—ServletConfig接口和SevletContext接口

    ServletConfig接口 1. 可以获取当前Servlet在web.xml中的配置信息(用的不多) 2. 在不使用"硬编码"的情况下,将部署状态信息传递给Servlet.这个 ...

  2. Java EE javax.servlet中的ServletConfig接口

    ServletConfig接口 public interface ServletConfig 实现类:GenericServlet.HttpServlet 一.介绍 一个供servlet容器使用配置对 ...

  3. ServletConfig接口默认是哪里实现的?

    问题:Servlet接口默认是哪里实现的? 答:GenericServlet 1.结构 2.ServletConfig.GenericServlet.HttpServlet的关系如下: public ...

  4. HTTP协议 Servlet入门 Servlet工作原理和生命周期 Servlet细节 ServletConfig对象

    1 HTTP协议特点   1)客户端->服务端(请求request)有三部份     a)请求行--请求行用于描述客户端的请求方式.请求的资源名称,以及使用的HTTP协议版本号 请求行中的GET ...

  5. javaWeb学习总结(3)- Servlet总结(servlet的主要接口、类)

    Servlet总结01——servlet的主要接口.类 (一)servlet类 Servlet主要类.接口的结构如下图所示: 要编写一个Servlet需要实现javax.servlet.Servlet ...

  6. Servlet常用的接口和类

    使用接口和类的作用:Servlet也是依靠继承父类和实现接口来实现的.使用Servlet必须要引入两个包:javax.servlet和javax.servlet.http.所有的Servlet应用都是 ...

  7. Servlet、ServletConfig、ServletContext深入学习

    1.Servlet学习 1.Servlet生命周期 Servlet 加载—>实例化—>服务—>销毁. init(servletConfig):(经过自己的测试发现会先调用这个而不是i ...

  8. 与servlet相关的接口

    (二)与servlet相关的接口 从servlet仅有的5个方法当中,我们知道其涉及3个接口,分别是: ServletConfig ServletRequest ServletResponse 2.1 ...

  9. Servlet笔记4--ServletConfig接口和ServletContext接口

    ServletConfig接口: ServletContext接口: 代码详解: (1)web.xml配置文件: <?xml version="1.0" encoding=& ...

随机推荐

  1. XMLSpy 生成xml模板(转)

    公司中的生成ci需要和xsd中的sequence一致, 由于xsd的过于庞大,且有继承关系, 所以人工比较是不可能的. 现用xmlspy来生成. 1, 在xmlspy中打开xsd 2, 将choice ...

  2. poj Drainage Ditches(最大流入门)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 85250   Accepted: 3316 ...

  3. C的基本数据类型小结

    代码 /** * 基本数据类型 */ #include <stdio.h> #include <limits.h> /* 定义 32 位时的 long 与 unsigned l ...

  4. Java script-数组与字符串方法

    数组: 1.concat() 功能:用于连接两个或多个数组,该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本. 参数:concat(data1,data2,...);所有参数可选,要合并的数 ...

  5. 【串线篇】REST风格的请求格式

    1.什么是rest 答出这两点就够了: 1.1 统一接口 rest其实是基于HTTP的,四种方式. RESTful架构风格规定,数据的元操作,即CRUD(create, read, update和de ...

  6. python读写excel(xlrd、xlwt)

    一.读excel表 读excel用到xlrd模块,写excel用到xlwt模块: # 1.导入模块 import xlrd # 2.打开Excel文件读取数据 workbook = xlrd.open ...

  7. js 在array的遍历操作中修改arry中元素数量 出现的一些奇特的操作

    在js中array是属于复杂类型,在arr1=arr2得赋值操作中,arr1得到的值并不是arr2的value,而是一个指向引用.那么修改arr1的同时arr2读取的值也会同步变化,那么问题来了,上代 ...

  8. maven 使用idea运行按钮install

    根据需要配置 例: clean install 离线 设置vm 跳过测试 -Xms1024m -Xmx1024m -XX:MaxPermSize=1024m 附(离线+跳过测试): mvn clean ...

  9. 关于python接口测试connect error

    接口测试里如果报错出现 socket.gaierror: [Errno 8] nodename nor servname provided, or not known 或者 urllib3.excep ...

  10. interrupt和interrupted和isInterrupted的区别

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11413917.html interrupt Code Demo package org.fool.th ...