ServletContext对象

什么是 ServletContext 对象
ServletContext对象,表示当前整个web应用的上下文对象(全局管理者),它是一个项目的引用,代表了当前项目。

ServletContext 对象什么时候创建?
当项目启动的时候,服务器为每一个web项目创建一个servletcontext对象.
当项目被移除的时候或者服务器关闭的时候servletcontext销毁
注意: 一个 web 应用加载时只会创建一个 ServletContext 对象!!!

获取 ServletContext 对象的方法
1 this .getServletContext();
2 this .getServletConfig().getServletContext();
3 request.getSession().getServletContext();

常用的方法与作用
1 )得到 web 应用的路径
this. getServletContext().getContextPath();
2 )得到 web 应用 全局的配置参数
this .getServletContext().getInitParameter( "name" );
this .getServletContext().getInitParameterNames();
3 )域对象相关的
this .getServletContext().getAttribute( "name" );
this .getServletContext().setAttribute( "name" , "value" );
this .getServletContext().removeAttribute( "name" );
4 )请求转发,配合 forward 的使用。
this. getServletContext().getRequestDispatcher("/内部路径"). forward(request, response);
5 web 应用中加载资源文件
this. getServletContext().getRealPath("path"); // 获得项目部署到服务器上的绝对路径,/表示根目录
this. getServletContext().getResourceAsStream("path");
6 获取文件的mime类型

this.getServletContext().getMimeType("1.txt");


下面通过实例来演示ServletContext的使用


<!-- 配置web应用全局的参数 -->
<context-param>
	<param-name>AAA</param-name>
	<param-value>AAA's value</param-value>
</context-param>
<context-param>
	<param-name>BBB</param-name>
	<param-value>BBB's value</param-value>
</context-param>
/**
 * ServletContext的使用
 */
public class ContextServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取ServletContext对象
		ServletContext context = this.getServletContext();

		// 获取web.xml中配置的context-param信息
		String avalue = context.getInitParameter("AAA");
		System.out.println("AAA=" + avalue); //AAA's value

		// 获取所有的配置参数名和值
		Enumeration<String> names = context.getInitParameterNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			String value = context.getInitParameter(name);
			System.out.println(name + "=" + value);
		}

		// 保存值到ServletContext域中
		context.setAttribute("name", "mChenys");

		// 从ServletContext域中获取值
		String value = (String) context.getAttribute("name");
		System.out.println("name=" + value);

		// 从ServletContext域中移除值
		context.removeAttribute("name");
		System.out.println("移除后name=" + context.getAttribute("name"));

		// 获取路径信息
		String realPath = context.getRealPath("/");
		System.out.println("realPath:"+realPath);//"E:\apache-tomcat-7.0.52\webapps\servlet_all\"

		String contextPath = context.getContextPath();
		System.out.println("contextPath:"+contextPath);// "/servlet_all"
		
		// 获取mine信息
		String mimeType = context.getMimeType("1.txt");//注意1.txt文件是放在src目录下的,部署的时候会自动放到\WEB-INF\classes目录下
		System.out.println("mimeType:"+mimeType);// "text/plain"
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}



ServletContext是Java Web中的一个重要对象,表示整个Web应用程序的上下文环境。它是一个接口,提供了许多方法,用于获取Web应用程序的相关信息,例如应用程序的名称、版本、servlet上下文参数、初始化参数等。 以下是ServletContext对象的一些常用方法和用法: 1. 获取应用程序的名称和版本: String appName = context.getServletContextName(); //获取应用程序名称 String appVersion = context.getMajorVersion() + "." + context.getMinorVersion(); //获取应用程序版本号 2. 获取servlet上下文参数: String paramValue = context.getInitParameter("paramName"); //获取指定参数的值 Enumeration<String> paramNames = context.getInitParameterNames(); //获取所有参数名称 3. 获取应用程序的真实路径: String realPath = context.getRealPath("/"); //获取应用程序的根目录真实路径 4. 获取应用程序的资源: InputStream input = context.getResourceAsStream("/path/to/resource"); //获取指定资源的输入流 URL resourceUrl = context.getResource("/path/to/resource"); //获取指定资源的URL 5. 获取应用程序的Servlet信息: ServletInfo info = context.getServletInfo(); //获取Servlet的信息 6. 获取应用程序的Session管理器: HttpSessionManager sessionMgr = context.getSessionManager(); //获取Session管理器 7. 获取应用程序的Mime类型: String mimeType = context.getMimeType("fileName"); //获取指定文件的Mime类型 8. 获取应用程序的Servlet上下文: ServletContext servletContext = context.getContext("/path/to/servlet"); //获取指定Servlet的上下文 总之,ServletContext对象提供了一种方便的方式来获取Web应用程序的各种信息和资源,使得开发人员可以更方便地开发和管理Web应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值