Config对象
ServletConfig接口
Servlet引擎将代表Servlet容器的对象(Servlet是在Servlet容器中运行的程序,它在有些情况下可能需要访问Servlet容器或借助Servlet容器访问外部的资源,所以Servlet引擎需要将表示Servlet容器的对象传递给Servlet)和Servlet配置参数信息(在web.xml中为某个Servlet设置的友好名称和初始化参数等信息)一并封装到一个称之为ServletConfig对象中,并在初始化Servlet实例对象时传递给该Servlet
ServletConfig对象
作用:1)获得字符集编码 2)获得数据库连接信息 3)获得配置文件,查看SpringMvc案例的web.xml文件
在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。在配置后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init()方法时,将ServletConfig对象传递给servlet。
在web.xml中配置的Servlet初始化参数
<servlet>
<servlet-name>ConfigTest</servlet-name>
<servlet-name>com.lxm.ConfigText</servlet-class>
<init-param>
<param-name>初始化参数名称</param-name>
<param-value>初始化参数值</param-value>
<init-param>
</servlet>
getInitParameterNames方法
在web.xml文件中可以为Servlet设置若干个初始化参数,getInitParameterNames()方法用于返回一个集合对象,该对象中包含在web.xml文件中为当前Servlet设置的所有初始化参数的名称。
getInitParameter(key)方法
getInitParameter()方法用于返回在web.xml文件中为Servlet所设置的某个名称的初始化参数的值,如果指定名称的初始化参数不存在,则返回值为NULL。
getServletName()方法
getServletName()方法用于返回Servlet在web.xml文件中注册名称。对于没有在web.xml文件中注册的Servlet,getServletName()方法返回的结果时该Servlet的类名。
getServletContext()方法
在Servlet程序中,每个web应用程序(Servlet容器)都用一个各自的ServletContext对象来表示,ServletConfig对象中包含了ServletContext对象的引用,getServletContext()方法用于返回ServletConfig对象中所包含的ServletContext对象的应用