ServletConfig对象
作用
ServletConfig对象: 主要是用于加载servlet的初始化参数。在一个web应用可以存在多个ServletConfig对象(一个Servlet对应一个ServletConfig对象)
对象创建和得到
创建时机: 在创建完servlet对象之后,在调用init方法之前创建。
得到对象: 直接从有参数的init方法中得到!!!
servlet的初始化参数配置
<servlet> <servlet-name>ServletConfigDemo</servlet-name> <servlet-class>com.cn.servlet.ServletConfigDemo</servlet-class> <!-- 初始化参数:这些参数会在加载web应用的时候,封装到ServletConfig对象中 --> <init-param> <param-name>path</param-name> <param-value>f:/b.txt</param-value> </init-param> <init-param> <param-name>param1</param-name> <param-value>value1</param-value> </init-param> <init-param> <param-name>param2</param-name> <param-value>value3</param-value> </init-param> </servlet>
注意: servlet的参数只能由当前的这个sevlet获取!!!!
ServletConfig的API:
java.lang.String getInitParameter(java.lang.String name) 根据参数名获取Servlet参数值
java.util.Enumeration getInitParameterNames() 获取Servlet所有初始化参数名
ServletContext getServletContext() 得到servlet上下文对象
java.lang.String getServletName() 得到servlet的名称
Demo:
package com.cn.servlet;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.Enumeration;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ServletConfigDemo extends HttpServlet { /** * 以下的代码,GenericServlet已经写了,无需自己写。 */// private ServletConfig config; /** * 1)tomcat服务器在加载web应用的时候,会把web.xml中该Servlet的初始化参数封装到ServletConfig对象中 * 2)tomcat服务器调用init方法传入ServletConfig对象中 */ /*@Override public void init(ServletConfig config) throws ServletException { this.config = config; }*/ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // File file = new File("f:/a.txt");// File file = new File("f:/b.txt"); /* * 读取Servlet初始化参数 */ ServletConfig config = this.getServletConfig(); String path = config.getInitParameter("path"); /* * 读取内容 */// BufferedReader buffReader = new BufferedReader(new FileReader(file)); BufferedReader buffReader = new BufferedReader(new FileReader(path)); String str = null; while((str = buffReader.readLine()) != null){ System.out.println(str); } //查询当前servlet的所有初始化参数 Enumeration<String> enums = this.getServletConfig().getInitParameterNames(); while(enums.hasMoreElements()){ String parameterName = enums.nextElement(); String parameterValue = this.getServletConfig().getInitParameter(parameterName); System.out.println(parameterName + ":" + parameterValue); } //获取当前servlet的名称 String servletName = this.getServletName(); System.out.println("当前servlet的名字:" + servletName); }}
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <servlet> <servlet-name>InitDemo</servlet-name> <servlet-class>com.cn.servlet.InitDemo</servlet-class> <!-- 让servlet对象自动加载 --> <load-on-startup>1</load-on-startup> <!-- 注意: 整数值越大,创建优先级越低!! --> </servlet> <servlet> <servlet-name>ThreadDemo</servlet-name> <servlet-class>com.cn.servlet.ThreadDemo</servlet-class> </servlet> <servlet> <servlet-name>ServletConfigDemo</servlet-name> <servlet-class>com.cn.servlet.ServletConfigDemo</servlet-class> <!-- 初始化参数:这些参数会在加载web应用的时候,封装到ServletConfig对象中 --> <init-param> <param-name>path</param-name> <param-value>f:/b.txt</param-value> </init-param> <init-param> <param-name>param1</param-name> <param-value>value1</param-value> </init-param> <init-param> <param-name>param2</param-name> <param-value>value3</param-value> </init-param> </servlet> <servlet> <servlet-name>LifeDemo</servlet-name> <servlet-class>com.cn.servlet.LifeDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>InitDemo</servlet-name> <url-pattern>/InitDemo</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ThreadDemo</servlet-name> <url-pattern>/ThreadDemo</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ServletConfigDemo</servlet-name> <url-pattern>/ServletConfigDemo</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>LifeDemo</servlet-name> <url-pattern>/LifeDemo</url-pattern> </servlet-mapping></web-app>
效果: