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. 如何多个router 进行合并?

    有时间可能有多个人开发,如果在共用router, 势必会造成合并冲突,可以分开多个router.js ,然后进行合并 // router0.jsconst studyRouter = [ { path ...

  2. automapper实体中的映射和聚合根中的使用

    一,如下例子: using AutoMapper; using System; using System.Collections.Generic; using System.Linq; using S ...

  3. 图形设计 X11

    显示适配器驱动程序安装范例 AMD驱动加载 Intel驱动加载

  4. 使用 jQuery 实现当前页面高亮显示的通栏导航条

    index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...

  5. centos 升级python

    1.下载python3.6.1的包 wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz 2.解压 tar -zxvf Pytho ...

  6. 阿里云ssh免密登陆突然无效

    [root@node03 ~]# ssh-copy-id node02 root@node02's password: sh: .ssh/authorized_keys: Permission den ...

  7. xshell 挪动文件夹

    cp -rp /home/d001 /home/Documents 复制/home下d001到/home下Documents -r 是遍历目录,即复制整个目录-p 是保留原有属性 查看的命令是cat ...

  8. Java的传值调用

    (本文非引战或diss,只是说出自己的理解,欢迎摆正心态观看或探讨) 引子 之所以写这篇文章是因为前些天写了一篇<Java中真的只有值传递么?>探讨了网上关于Java只有值传递的说法,当时 ...

  9. python locust_TaskSet声明任务的典型方法是使用task装饰器的两种方法

    为TaskSet声明任务的典型方法是使用task装饰器.该min_wait和MAX_WAIT属性也可以在使用taskset类中重写. from locust import Locust, TaskSe ...

  10. python 去除字符串两端字符串

    转载:http://blog.sina.com.cn/s/blog_940224600100w8l0.html Python中的strip用于去除字符串的首位字符,同理,lstrip用于去除左边的字符 ...