Servlet 8—— ServletConfig

javax.servlet.ServletConfigSUN制定的接口,apacheServletConfig接口的实现类的完整类名是:org.apache.catalina.core.StandardWrapperFacade,但是作为程序员不需要关心具体的类型,只要面向javax.servlet.ServletConfig接口调用即可,程序运行阶段执行的是apacheServletConfig的实现类中的方法。

1、Servlet对象的创建以及方法的调用过程

Class c = Class.forName("com.bjpowernode.javaweb.servlet.ServletConfigTest");
Servlet servlet = (Servlet)c.newInstance();
ServletConfig config = new org.apache.catalina.core.StandardWrapperFacade();
servlet.init(config);
ServletRequest request = 创建了apache的一个request对象;
ServletResponse response = 创建了apache的一个response对象;
servlet.service(request,response);
servlet.service(request,response);
servlet.service(request,response);
servlet.service(request,response);
………………
servlet.destroy();…

2、ServletServletConfig之间的关系?

一个Servlet对象会对应一个ServletConfig对象。

3、ServletConfig对象的本质是什么?

  • ServletConfig实际上是一个Servlet对象相关的配置信息对象。ServletConfig对象中封装了一个Servlet对象的配置信息。一个Servletweb.xml文件中<servlet>标签中配置的信息会自动被封装到ServletConfig对象中,通过ServletConfig对象我们可以获取到当前的Servlet对象的相关配置信息。Servlet对象的配置信息到web.xml文件中。
  • 一个Servlet对象对应一个ServletConfig对象,100个Senvlet对象对应100个ServletConfig对象.。

4、ServletConfig接口中常用的方法

通过初始化参数的name获取初始化参数的value

String value = config.getInitParameter(String name);

获取所有初始化参数的name

Enumeration<String> names = config.getInitParameterNames();

获取ServletContext对象,获取servlet上下文

ServletContext application = config.getServletContext();

5、Tomcat服务器是一个实现了Servlet规范和JSP规范的容器。

6、将init方法上的ServletConfig参数移动到service方法中,因为我们程序员主要编写的方法是service方法,在service方法中我们门可能需要使用ServletConfig! ! ! ! !

  • 在init方法中完成:局部变量config赋值给实例变量config
  • 实现getServletConfig方法,提供公开的get方法目的是供子类使用

下面是一个小例子:

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ServletConfig接口</title>
</head>
<body>
    <a href="/Servlet-04/a">AServlet</a>
    <br><br>
    <a href="/Servlet-04/b">BServlet</a>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <servlet>
    <servlet-name>AServlet</servlet-name>
    <servlet-class>myweb.AServlet</servlet-class>
    <!-- 初始化参数:被封装到ServletConfig对象中 -->
    <init-param>
        <param-name>driver</param-name>
        <param-value>com.mysql.jdbc.Driver</param-value>
    </init-param>
    <init-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://localhost:3306/MySQL</param-value>
    </init-param>
    <init-param>
        <param-name>user</param-name>
        <param-value>root</param-value>
    </init-param>
    <init-param>
        <param-name>password</param-name>
        <param-value>root</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>AServlet</servlet-name>
    <url-pattern>/a</url-pattern>
  </servlet-mapping>
  
   <servlet>
    <servlet-name>BServlet</servlet-name>
    <servlet-class>myweb.BServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>BServlet</servlet-name>
    <url-pattern>/b</url-pattern>
  </servlet-mapping>
</web-app>

AServlet.java

package myweb;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class AServlet implements Servlet {

    private ServletConfig config;

    public AServlet() {
        System.out.println("A");
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        this.config = config;
        // AServlet's ServletConfig =
        // org.apache.catalina.core.StandardWrapperFacade@7f41a6e
        System.out.println("AServlet's ServletConfig = " + config.toString());
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        // 获取ServletConfig
        ServletConfig config = getServletConfig();

        // 通过初始化参数的name获取value
        /*
         * String driver = config.getInitParameter("driver"); String url =
         * config.getInitParameter("url"); String user =
         * config.getInitParameter("user"); String password =
         * config.getInitParameter("password");
         * 
         * System.out.println(driver); System.out.println(url);
         * System.out.println(user); System.out.println(password);
         */

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print("<html>");
        out.print("<head>");
        out.print("<title>ServletConfig</title>");
        out.print("</head>");
        out.print("<body>");

        // 获取所有初始化参数的name
        Enumeration<String> names = config.getInitParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = config.getInitParameter(name);
            out.print(name + " = " + value);
            out.print("<br>");
        }

        // 获取servletName
        String servletName = config.getServletName();
        out.print(servletName);
        out.print("<br>");
        
        // 先记住这个方法,后面讲什么是ServletContext
        ServletContext application = config.getServletContext();
        out.print(application.toString());
        
        out.print("</body>");
        out.print("</html>");

    }

    @Override
    public void destroy() {
    }

    @Override
    public ServletConfig getServletConfig() {
        return config;
    }

    @Override
    public String getServletInfo() {
        return null;
    }
}

BServlet.java

package myweb;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class BServlet implements Servlet {

    private ServletConfig config;

    public BServlet() {
        System.out.println("B");
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        // BServlet's ServletConfig = org.apache.catalina.core.StandardWrapperFacade@622ece09
        // System.out.println("BServlet's ServletConfig = " + config.toString());
        
        // 将局部变量config赋值给实例变量config
        // 目的:在service方法中也可以使用config
        this.config = config;
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        ServletConfig config = getServletConfig();
    }

    @Override
    public void destroy() {
    }

    // 这个方法共子类使用的,在子类中若想获取ServletConfig,可以调用这个方法。
    @Override
    public ServletConfig getServletConfig() {
        return config;
    }

    @Override
    public String getServletInfo() {
        // TODO Auto-generated method stub
        return null;
    }
}

 

 

posted @ 2021-02-01 18:44  我等着你  阅读(63)  评论(0编辑  收藏  举报