• JavaWeb项目获得xml中的初始化参数


    web.xml文件

    web.xml文件位置

    web.xml文件的通常在项目中 webapp/WEB-INF/web.xml 路径下

    web.xml的作用

    可以在web.xml中配置web容器中一些需要的信息

    1、使用标签 context-param 可以指定web容器的一些初始化参数

    例如:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>

    2、使用标签 listener 可以指定容器的 ServletContextListener

    3、使用标签  servlet 和 servlet-mapping 可以指定容器的 HttpServlet

    例如:

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>name-test</param-name>
            <param-value>value-test</param-value>
        </init-param>
    </servlet>

    4、使用标签  filter 和  filter-mapping 可以指定容器的 Filter

    <filter>
        <filter-name>name-filter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>name-test</param-name>
            <param-value>value-test</param-value>
        </init-param>
    </filter>

    等等

    获得web.xml中的初始化参数

    1、使用 context-param 配置的参数获得方式

    ServletContext.getInitParameter("param-name");

    2、使用 servlet 中 init-param 配置的参数获得方式

    ServletConfig.getInitParameter("param-name")

    3、使用 filter 中 init-param 配置的参数获得方式

    FilterConfig.getInitParameter("param-name")

  • 相关阅读:
    python初学者学习工具安装教程&安装步骤详解
    Django面试题
    数据库-面试题
    Python面试题
    Python 内置函数&filter()&map()&reduce()&sorted()
    Python匿名函数(lambda函数)
    Python中两大神器&exec() &eval()
    面向对象&从这里开始我们将不再是纯小白
    软件开发程序猿日常必备,现用现查&日志记录
    如何去写项目的readme&链接
  • 原文地址:https://www.cnblogs.com/halu126/p/14985635.html
Copyright © 2020-2023  润新知