ServletConfig与ServletContext

ServletConfig: 封装了 Serlvet 的配置信息, 并且可以获取 ServletContext 对象

ServletConfig接口的方法:

                     getInitParameterNames
                     getInitParameter
                     getServletName
                     getServletContext

1. 配置 Serlvet 的初始化参数

<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.atguigu.javaweb.HelloServlet</servlet-class>

<!-- 配置 Serlvet 的初始化参数。 且节点必须在 load-on-startup 节点的前面 -->
<init-param>
<!-- 参数名 -->
<param-name>user</param-name>
<!-- 参数值 -->
<param-value>root</param-value>
</init-param>

<init-param>
<param-name>password</param-name>
<param-value>1230</param-value>
</init-param>

<load-on-startup>-1</load-on-startup>

</servlet>

2.获取初始化参数:

            > getInitParameter(String name): 获取指定参数名的初始化参数

            > getInitParameterNames(): 获取参数名组成的 Enumeration 对象.

            > getServletName: 获取 Serlvet 的配置名称(了解)  

    
   //获取初始化参数
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("init");
        //getInitParameter(String name): 获取指定参数名的初始化参数
        String user = servletConfig.getInitParameter("user");
        System.out.println("user:" + user);//root
        
        //getInitParameterNames(): 获取参数名组成的 Enumeration 对象. 
        Enumeration<String> names = servletConfig.getInitParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = servletConfig.getInitParameter(name);//获取value值
            System.out.println("**"+name + value);
            //**password123456
            //**userroot
        }
    }

       

3.ServletContext接口 

         1). Servlet引擎为每个WEB应用程序都创建一个对应的ServletContext对象,ServletContext对象被包含在ServletConfig对象中,

             调用   ServletConfig.getServletContext方法可以返回ServletContext对象的引用。
         2). 由于一个WEB应用程序中的所有Servlet都共享同一个ServletContext对象,

              所以,ServletContext对象被称之为 application 对象(Web应用程序对象)。

         3).功能:

              ①获取WEB应用程序的初始化参数

                    设置初始化参数

    <!--配置当前web应用的初始化参数 全局的  -->
    <context-param>
    <param-name>driver</param-name>
    <param-value>com.mysql.jdbc.Driver</param-value>
    </context-param>
    
    <context-param>
    <param-name>jdbcUrl</param-name>
    <param-value>jdbc:mysql:///test</param-value>
    </context-param>

                      获取参数

        // servletContext对象
        ServletContext servletContext = servletConfig.getServletContext();

        String driver = servletContext.getInitParameter("driver");
        System.out.println("driver:"+driver);
        Enumeration<String> names2 = servletContext.getInitParameterNames();
        while (names2.hasMoreElements()) {
            String name2 =  names2.nextElement();
            String value2 = servletContext.getInitParameter(name2);
            System.out.println("$$"+name2+value2);
            //driver:com.mysql.jdbc.Driver
            //$$drivercom.mysql.jdbc.Driver
            //$$jdbcUrljdbc:mysql:///test
        }

               ②. 获取当前 WEB 应用的某一个文件在服务器上的绝对路径, 而不是部署前的路径

                    getRealPath(String path);           

                     String realPath = servletContext.getRealPath("/note.txt");
                     System.out.println(realPath);

                ③. 获取当前 WEB 应用的名称:

                      getContextPath()

                      String contextPath = servletContext.getContextPath();
                      System.out.println(contextPath);

                ④. 获取当前 WEB 应用的某一个文件对应的输入流.

                      getResourceAsStream(String path): path 的 / 为当前 WEB 应用的根目录.

                      InputStream is2 = servletContext.getResourceAsStream("/WEB-INF/classes/jdbc.properties");

                      Sytem.out.println(is2)

                ⑤. 和 attribute 相关的几个方法:

        

理解:1.可以由 SerlvetConfig 获取:

                             ServletContext servletContext = servletConfig.getServletContext();

          2. 该对象代表当前 WEB 应用: 可以认为 SerlvetContext 是当前 WEB 应用的一个大管家.

              可以从中获取到当前 WEB 应用的各个方面的信息.

      

All that work will definitely pay off
原文地址:https://www.cnblogs.com/afangfang/p/12713590.html