ServletConfig 对象

问题:
  使用 ServletContext 对象可以获取 web.xml 中的全局配置文件,在 web.xml 中每个 Servlet 也可以进行单独的配置,那么该怎么获取配置信息呢?
解决:
  使用 ServletConfig 对象作用:
  ServletConfig 对象是 Servlet 的专属配置对象,每个 Servlet 都单独拥有一个 ServletConfig 对象,用来获取 web.xml 中的配置信息。
使用:
  获取 ServletConfig 对象获取 web.xml 中 servlet 的配置信息
 
代码例子:

package com.bjsxt.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* ServletConfig对象学习:
* 问题:
* 如何获取在web.xml中给每个servlet单独配置的数据呢?
* 解决:
* 使用ServletConfig对象
* 使用:
* 获取ServletConfig对象
* 获取web.xml中的配置数据
* @author MyPC
*
*/
public class ServletConfigServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//获取ServletConfig对象
ServletConfig sc=this.getServletConfig();
//获取web.xml中的配置数据
String code=sc.getInitParameter("config");
System.out.println(code);
}
}

web.xml里面配置:

原文地址:https://www.cnblogs.com/lwh-12345/p/13564693.html