Servlet类的继承体系,ServletConfig类,ServletConfig所处的全局角色的说明

本文详细介绍了ServletConfig类的功能及使用方法,包括获取Servlet名称、初始化信息和ServletContext对象。同时深入探讨了ServletContext的作用,如获取配置参数、设置全局属性等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ServletConfig类

ServletConfig类,见名知义。它是Servlet类的配置文件类。封装了Servlet的配置文件的信息。

它常用的功能有三个。
1.获取Servlet在web.xml文件中配置的Servlet名称(也就是servlet-name的值<servlet-name>ConfigServlet</servlet-name>)。
2.获取Servlet初始化信息。(web.xml文件中<Servlet>标签中 <init-param>的初始化信息 )

3.获取ServletContext域对象

	/**
	 * init是初始化方法。
	 * 一个Servlet对应一个ServletConfig对象
	 */
	public void init(ServletConfig _config) throws ServletException {
        super.init(config);
		// 获取config对象
		ServletConfig config = _config;
		System.out.println(config);

		// 它常用的功能有三个。
		// 1.获取Servlet在web.xml文件中配置的Servlet名称(也就是servlet-name的值<servlet-name>ConfigServlet</servlet-name>)。
		String servlet_name = config.getServletName();
		System.out.println("servlet-name ==>> " + servlet_name);

		// 2.获取Servlet初始化信息。(web.xml文件中<Servlet>标签中 <init-param>的初始化信息 )
		String param_value = config.getInitParameter("username");
		System.out.println("username 的值:" + param_value);

		// 3.获取ServletContext域对象
		// ServletContext它是一个域对象,并且一个Web工程对应一个ServletContext
		ServletContext context = config.getServletContext();
		System.out.println(context);
	}

web.xml文件中的配置信息:

	<servlet>
		<servlet-name>ConfigServlet</servlet-name>
		<servlet-class>com.atguigu.servlet.ConfigServlet</servlet-class>
		<!-- 给servlet添加初始化参数 -->
		<init-param>
			<!-- param-name 是初始化参数的名  -->
			<param-name>username</param-name>
			<!-- param-value 是初始化参数的值  -->
			<param-value>root</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>ConfigServlet</servlet-name>
		<url-pattern>/configServlet</url-pattern>
	</servlet-mapping>

在浏览器中输入访问地址http://127.0.0.1:8080/servlet/configServlet 访问测试:

什么是ServletContext?

第一:ServletContext是一个接口。
第二:ServletContext是一个域对象!

第三:每个Web工程,都对应一个ServletContext对象!

ServletContext有什么作用?
第一:ServletContext可以获取web.xml文件中的配置上下文参数
第二:ServletContext可以获取web工程在服务器的工程名
第三:ServletContext可以获取web工程中文件夹或文件在服务器硬盘上的绝对路径

第四:ServletContext可以设置、获取web工程的全局属性

ServletContext获取在Web.xml中配置的全局参数
1) Servlet中的示例代码:

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class ContextServlet
 */
public class ContextServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 获取ServletConfig对象
		ServletConfig config = getServletConfig();

		// 获取Servlet的上下文对象
		ServletContext context = config.getServletContext();

		// 获取web.xml文件中配置的参数的值
		// 正确。ServletContext只能获取context-param配置的参数
		String passwordValue = context.getInitParameter("password");
		System.out.println("获取context-param参数:" + passwordValue);

		// ServletContext无法获取Servlet中配置的init-param参数
		String usernameValue = context.getInitParameter("username");
		System.out.println("获取servlet配置的init-param参数:" + usernameValue);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

}

web.xml中的配置信息

	<!-- 配置由ServletContext获取的参数 -->
	<context-param>
		<!-- 配置参数的名 -->
		<param-name>password</param-name>
		<!-- 配置参数的值 -->
		<param-value>root</param-value>
	</context-param>

	<servlet>	
		<!-- 配置Servlet中的别名 -->
		<servlet-name>ContextServlet</servlet-name>
		<!-- 配置Servlet的全类名 -->
		<servlet-class>com.atguigu.servlet.ContextServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<!-- 配置Servlet的别名 -->
		<servlet-name>ContextServlet</servlet-name>
		<!-- 配置Servlet的访问路径 -->
		<url-pattern>/contextServlet</url-pattern>
	</servlet-mapping>

3) 控制台的打印信息:


ServletContext对象获取部署后的工程名


 projectPath 得到的结果:/servlet

ServletContext获取工程目录或文件在服务器硬盘上的绝对路径

获取工程中目录和文件的硬盘绝对路径代码:

		// 获取ServletConfig对象
		ServletConfig config = getServletConfig();

		// 获取Servlet的上下文对象
		ServletContext context = config.getServletContext();
		//获取/表示的路径表示从http://127.0.0.1:8080/工程名/   所表示的硬盘绝对路径
		// /也就是WebContent下的内容
	    String path = context.getRealPath("/");
		System.out.println("/的硬盘路径:" + path); 
	    
		//获取/imgs表示的路径
		// /imgs也就是表示WebContent目录下的imgs目录,发布后的硬盘绝对路径。
		String imgspath = context.getRealPath("/imgs");
		System.out.println("/imgs的硬盘路径:" + imgspath); 
		
		//获取/imgs表示的路径
		// /imgs也就是表示WebContent目录下的imgs目录里wrong.png文件,发布后的硬盘绝对路径。
		String wrongpath = context.getRealPath("/imgs/wrong.png");
		System.out.println("/imgs/wrong.png的硬盘路径:" + wrongpath); 

ServletContext设置全局共享属性

我们可以创建两个Servlet程序。分别是:ContextAttrServlet1和ContextAttrServlet2.

a) 在ContextAttrServlet1 中直接通过ServletContext获取一个没有设置的属性值。

b) 然后在ContextAttrServlet2 中,通过ServletContext设置一个全局的属性值。

c) 然后通过不同的访问顺序测试ServletContext对象管理的全局共享属性。

 

ContextAttrServlet1的代码

ContextAttrServlet1的配置信息,访问路径:http://127.0.0.1:8080/servlet/contextAttrServlet1

  <servlet>
    <servlet-name>ContextAttrServlet1</servlet-name>
    <servlet-class>com.atguigu.servlet.ContextAttrServlet1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ContextAttrServlet1</servlet-name>
    <url-pattern>/contextAttrServlet1</url-pattern>
  </servlet-mapping>

ContextAttrServlet2的代码

ContextAttrServlet2的配置信息,访问路径:http://127.0.0.1:8080/servlet/contextAttrServlet2

  <servlet>
    <servlet-name>ContextAttrServlet2</servlet-name>
    <servlet-class>com.atguigu.servlet.ContextAttrServlet2</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ContextAttrServlet2</servlet-name>
    <url-pattern>/contextAttrServlet2</url-pattern>
  </servlet-mapping>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值