ServletConfig 对象
问题: 使用 ServletContext 对象可以获取 web.xml 中的全局配置文件, 在 web.xml 中 每个Servlet 也可以进行单独的配置,那么该怎么获取配置信息呢?
解决: 使用 ServletConfig 对象
作用: ServletConfig 对象是 Servlet 的专属配置对象,每个 Servlet 都单独拥有一个 ServletConfig 对象,用来获取 web.xml 中的配置信息。
使用: 获取 ServletConfig 对象
获取 web.xml 中 servlet 的配置信息
* ServletConfig对象学习:
* 问题:
* 如何获取在web.xml中给每个servlet单独配置的数据呢?
* 解决:
* 使用ServletConfig对象
* 使用:
* 获取ServletConfig对象
* 获取web.xml中的配置数据
web.xml
ServletConfigServlet.java
package com.dym.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中的配置数据
*
*/
public class ServletConfigServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
//获取servletConfig对象
ServletConfig sc=this.getServletConfig();
//获取web.xml中的配置数据
String code=sc.getInitParameter("config");
System.out.println(code);
}
}