ServletConfig和ServletContext

ServletConfig接口

Tomcat初始化一个Servlet时,会将该Servlet的配置信息封装在一个ServletConfig对象中,通过调用init(ServletConfig config)方法将ServletConfig对象传递给Servlet。

常用方法:

String getInitParameter(String name)//根据初始化参数名返回对应的初始化参数值
Enumeration getInitParameterNames() //返回一个Enumeration对象,其中包含了所有的初始化参数名
ServletContext getServletContext() //返回一个代表当前Web应用的ServletContext对象
String getServletName() //返回Servlet的名字,即web.xml中<servlet-name>元素的值

可以使用<init-param>设置servlet初始化参数

设置初始化参数a = 1,b = 1

<?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>myservlet</servlet-name>
        <servlet-class>com.ch.MyServlet</servlet-class>
        <init-param>
            <param-name>a</param-name>
            <param-value>1</param-value>
        </init-param>
        <init-param>
            <param-name>b</param-name>
            <param-value>1</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>myservlet</servlet-name>
        <url-pattern>/servlet</url-pattern>
    </servlet-mapping>
</web-app>

java代码如下:

import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;
public class MyServlet implements Servlet {
    public void init(ServletConfig servletConfig) throws ServletException {
        String s = servletConfig.getInitParameter("a"); //getInitParameter()方法
        String name = servletConfig.getServletName(); //getServletName()方法
        Enumeration e = servletConfig.getInitParameterNames(); //getInitParameterNames()方法

        System.out.println(servletConfig.getInitParameter("a"));
        System.out.println(name);
        while(e.hasMoreElements()) {
            String str = (String) e.nextElement();
            System.out.print(str + " ");
        }
    }
    public ServletConfig getServletConfig() { return null; }
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {}
    public String getServletInfo() { return null; }
    public void destroy() {}
}
/*
输出:
1
myservlet
a b
*/

ServletContext接口

Servlet容器启动时,会为每个web应用创建一个唯一的ServletContext对象代表当前web应用,该对象不仅封装了当前web应用所有信息,而且实现了多个servlet之间数据的共享。

获取当前web应用的初始化参数

String getInitParameter(String name)
Enumeration getInitParameterNames()

可以使用<context-param>设置初始化参数

初始化参数a = 1

<?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">
    <context-param>
        <param-name>a</param-name>
        <param-value>1</param-value>
    </context-param>

    <servlet>
        <servlet-name>myservlet</servlet-name>
        <servlet-class>com.ch.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>myservlet</servlet-name>
        <url-pattern>/servlet</url-pattern>
    </servlet-mapping>
</web-app>
import javax.servlet.*;
import java.io.IOException;
public class MyServlet implements Servlet {
    public void init(ServletConfig servletConfig) throws ServletException {
        ServletContext servletContext = servletConfig.getServletContext(); //获取getServletContext对象
        System.out.println(servletContext.getInitParameter("a")); //获取初始化参数
    }
    public ServletConfig getServletConfig() { return null; }
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {}
    public String getServletInfo() { return null; }
    public void destroy() {}
}
//输出:1

实现多个Servlet应用共享数据

一个web应用的多个servlet共享一个ServletContext对象,因此ServletContext对象的属性域可以被该web应用的所有servlet访问

常用方法:

Enumeration<String> getAttributeNames() 
Object getAttribute(String var1) 
void setAttribute(String var1, Object var2)
void removeAttribute(String var1)

两个类:MyServlet设置属性a = 1,Test_Servlet输出属性a

//MyServlet
import javax.servlet.*;
import java.io.IOException;
public class MyServlet implements Servlet {
    public void init(ServletConfig servletConfig) throws ServletException {
        ServletContext servletContext = servletConfig.getServletContext(); //获取getServletContext对象
        servletContext.setAttribute("a", 1);
    }
    public ServletConfig getServletConfig() { return null; }
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {}
    public String getServletInfo() { return null; }
    public void destroy() {}
}
//Test_Servlet
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Test_Servlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext(); //获取getServletContext对象
        System.out.println(servletContext.getAttribute("a"));
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
    }
}
//输出:1
上一篇:springBoot启动报 `NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/Stri


下一篇:Servlet详解