zoukankan      html  css  js  c++  java
  • servletConfig

    servletConfig--代表当前servlet在web.xml中的配置信息

    String getServletName() – 获取当前servlet在web.xml取的名字

     String

    getInitParameter(String name) 获取当前servlet指定名称的初始化参数的值
              Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.

     Enumeration

    getInitParameterNames()获取当前servlet所有初始化参数的名字组成的枚举
              Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.

    ServletContext

    getServletContext() 获取代表当前web应用的servletContext对象
              Returns a reference to the ServletContext in which the caller is executing

    配置web.xml 设置init-param

    <servlet>
        <servlet-name>SConfigServlet</servlet-name>
        <servlet-class>com.itheima.SConfigServlet</servlet-class>
        <init-param>
            <param-name>name1</param-name>
            <param-value>value1</param-value>
        </init-param>
        <init-param>
            <param-name>encode</param-name>
            <param-value>utf-8</param-value>
        </init-param>
      </servlet>

     使用方法

            ServletConfig config = this.getServletConfig();
            //--获取当前Servlet 在web.xml中配置的名称
            String sName = config.getServletName();
            System.out.println(sName);
            //--获取当前Servlet中配置的初始化参数
            String value = config.getInitParameter("name2");
            System.out.println(value);
            
            Enumeration enumration = config.getInitParameterNames();
            while(enumration.hasMoreElements()){
                String name = (String) enumration.nextElement();
                String value = config.getInitParameter(name);
                System.out.println(name+":"+value);
                    }
  • 相关阅读:
    ConcurrentHashMap源码分析
    HashMap源码与相关面试题
    字符串学习笔记(三)---- StringBuilder
    字符串学习笔记(二)---- StringBuffer
    Vue一些基本操作技巧
    PHP代码及命名规范
    Js返回顶部的方法
    linux下镜像网站的几种方法
    单例模式示例
    工厂模式和IOC练习
  • 原文地址:https://www.cnblogs.com/superPerfect/p/4296306.html
Copyright © 2011-2022 走看看