5.ServletConfig接口

1.每个servlet都有一个对应的ServletConfig

2.使用一个或多个<init -param>元素进行配置

3.ServletConfig的作用:将servlet的配置信息封装

4.ServletConfig对象属于每一个确定的servlet

5.ServletConfig接口常用方法

String getInitParameter(String name) 根据初始化名返回初始化值
Enumeration getInitParameterNames() 返回Enumeration对象(所有的初始化参数名的集合)
ServletContext getServletContext() 返回代表当前Web应用的ServletContext对象
String getServletName() 返回<servlet -name>元素的值

(1)getInitParameter(String name)方法

存入
注意:<init -parm>写在< servlet>中<servlet -class>的下面,写在外面会冒红

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>AServlet</servlet-name>
        <servlet-class>servlet.AServlet</servlet-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>AServlet</servlet-name>
        <url-pattern>/AS</url-pattern>
    </servlet-mapping>

</web-app>

获取

package servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class AServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        ServletConfig config=this.getServletConfig();
        //获得ServletConfig对象
        String param=config.getInitParameter("encoding");
        //获得参数名为encoding对应的参数值
        out.print("encoding="+param);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

(2)Enumeration getInitParameterNames()方法

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>AServlet</servlet-name>
        <servlet-class>servlet.AServlet</servlet-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </servlet>
    <servlet>
        <servlet-name>BServlet</servlet-name>
        <servlet-class>servlet.BServlet</servlet-class>

    </servlet>
    <servlet-mapping>
        <servlet-name>AServlet</servlet-name>
        <url-pattern>/AS</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>BServlet</servlet-name>
        <url-pattern>/BS</url-pattern>
    </servlet-mapping>

</web-app>
package servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

public class BServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            ServletConfig config = this.getServletConfig();
            Enumeration<String> enu = config.getInitParameterNames();//获取到所有参数名的集合
            while (enu.hasMoreElements()) {
                String name = enu.nextElement();
                String va = config.getInitParameter(name);
                System.out.println(name + ": " + va);
            }

        }
    }
上一篇:Servlet的三种实现方式(详解)


下一篇:ServletConfig接口