ServletConfig中的方法

ServletConfig中有哪些方法:

    javax.servlet.ServletConfig;

String

getInitParameter(String name):

通过name获取value

Enumeration

getInitParameterNames():

获取所有初始化参数的名字

ServletContext

getServletContext():

获取ServletContext [Servlet上下文] 对象。

String

getServletName():

获取<servlet-name>servletname</servlet-name>

先上代码:

    AServlet.java

  

package com.zhixi.zhang;

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;
    @Override
    public void init(ServletConfig config) throws ServletException {
        //将局部变里config赋值给实例变里config
        //目的:在service方法中也可以使用config
        this.config = config;
        System.out.println("AServlet --> ServletConfig = "+config.toString());
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        //将value打印到网页
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        //获取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");
        
        //获取所有出初始化参数的名字
        Enumeration<String> initParameterNames = config.getInitParameterNames();
        //遍历输出
        while(initParameterNames.hasMoreElements()){
            String name = initParameterNames.nextElement();
            //通过name获取value
            String value = config.getInitParameter(name);
            out.print(name+"="+value);
            out.print("<br>");
        }
        
        out.print("<br>");
        out.print(driver);
        out.print("<br>");
        out.print(url);
        out.print("<br>");
        out.print(user);
        out.print("<br>");
        out.print(password);*/
        
        //获取<servlet-name>servletname</servlet-name>
        String servletName = config.getServletName();
        out.print("<servlet-name>"+servletName+"</servlet-name>");
        out.print("<br>");
        
        //打印getSerrvletContext
        ServletContext servletContext = config.getServletContext();
        out.print(servletContext);//org.apache.catalina.core.ApplicationContextFacade@16d12dde
        
    }

    @Override
    public void destroy() {

    }

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


    public String getServletInfo() {
        return null;
    }


}
View Code

 

    web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>老杜Servlet_04_ServletConfig接口</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
  <servlet>
      <servlet-name>a</servlet-name>
      <servlet-class>com.zhixi.zhang.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/bjpowernode</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>adamin123</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
      <servlet-name>a</servlet-name>
      <url-pattern>/a</url-pattern>
  </servlet-mapping>
    
  <servlet>
      <servlet-name>b</servlet-name>
      <servlet-class>com.zhixi.zhang.BServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>b</servlet-name>
      <url-pattern>/b</url-pattern>
  </servlet-mapping>
</web-app>
View Code

 

 方法分析:

1 getInitParmeter(String name)

//web.xml信息

 

 

 

//java文件

 

 

 

//获取到对应的value信息

 

 

 

2 getInitParameterNames():

 

//输出

3 getServletContext():

 

 

 

4 getServletName()

获取<servlet-name>servletname</servlet-name>

 

 

 

 

 

 

 

ServletConfig到底是什么?

  ServletConfig是一个Servlet对象的配置信息对象,ServletConfig对象中封装了一个Servlet对象的置信息。Servlet对象的配置信息到web.xml文件中。

  一个Servlet对象对应一个ServletConfig对象,100个Servlet对象对应100个ServletConfig对象。

 

 

 

 

posted @ 2020-08-16 21:12  Java小白的搬砖路  阅读(395)  评论(0编辑  收藏  举报