zoukankan      html  css  js  c++  java
  • ServletConfig对象详解

    在Servlet 的配置文件中,可以用一个或多个<init-param>标签为servlet配置一些初始化参数。

    当servlet配置了初始化参数之后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给Servlet。进而,程序员通过Servlet对象得到当前servlet的初始化参数信息。

    获取ServletConfig中初始化信息步骤:

    1 . 创建私有变量:

    private ServletConfig config = null;

    2、重写init方法,令 this.config = config,从而获取ServletConfig对象

    3、获取<init-param>中的配置信息

    //获取初始化参数
    String value1 = this.config.getInitParameter("x1");
    //获得配置文档中<inti-param>标签下name对应的value
    String value2 = this.config.getInitParameter("x2");
    //获取所有初始化参数
    Enumeration e = this.config.getInitParameterNames();
    while(e.hasMoreElements()){
          String name = (String) e.nextElement();
          String value = this.config.getInitParameter(name);
          System.out.println(name+"="+value);
    }

    4、开发中ServletConfig的作用有:

       获取字符集编码:

    String charset = this.config.getInitParameter("charset");

      获得数据库连接信息:

    String url = this.config.getInitParameter("url");
    String username = this.config.getInitParameter("username");
    String password = this.config.getInitParameter("password");

      获得配置文件:

    String configFile = this.config.getInitParameter("config");
  • 相关阅读:
    spring 事务
    Servlet详解之两个init方法的作用
    被request.getLocalAddr()苦闷了很久
    Java获取IP地址:request.getRemoteAddr()警惕
    MongoDB笔记
    hexo+github搭建博客
    Python处理Excel(使用openpyxl库)
    Wireshark使用学习
    查看开启操作系统端口
    记录Centos7服务器搭建过程
  • 原文地址:https://www.cnblogs.com/lashou/p/6068775.html
Copyright © 2011-2022 走看看