ServletContext简介(七)

一、什么是ServletContext对象

ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象内部封装是该web应用的信息,ServletContext对象一个web应用只有一个。

一个web应用有一个或多个servlet对象。

ServletContext对象的生命周期?

创建:该web应用被加载(服务器启动或发布web应用(前提,服务器启动状态))

销毁:web应用被卸载(服务器关闭,移除该web应用)

二、怎样获得ServletContext对象

1)ServletContext servletContext = config.getServletContext();  --一般不用,并且,config是在Servlet的init()方法中。在继承了HttpServlet的类中,我们一般只保留doGet()和doPost()方法。

2)ServletContext servletContext =this.getServletContext();   --常用

实例:

package com.ken.context;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ContextServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletContext context = this.getServletContext();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
我们来看看getServletContext()方法在哪里定义的。



可以看出来,getServletContext()方法,最终还是通过ServletConfig对象拿到的。

三、ServletContext的作用

3.1 获得web应用全局的初始化参数

3.1.1 web.xml

	<!-- 配置全局的初始化参数 -->
	<context-param>
		<param-name>driver</param-name>
		<param-value>con.mysql.jdbc.Driver</param-value>
	</context-param>
	<context-param>
		<param-name>username</param-name>
		<param-value>root</param-value>
	</context-param>

3.1.2 Servlet

package com.ken.context;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ContextServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1. 获得ServletContext对象
		ServletContext context = this.getServletContext();
		// 2. 获得初始化参数
		String driver = context.getInitParameter("driver");
		String username = context.getInitParameter("username");
		System.out.println("driver==========" + driver);
		System.out.println("username==========" + username);
	}

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

3.1.3 运行效果


我们的Spring,不就要用到

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

3.2 获得web应用中任何资源的绝对路径(重要 重要 重要)

3.2.1 servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	// 1. 获得ServletContext对象
	ServletContext context = this.getServletContext();
	// 2. 获得初始化参数
	String driver = context.getInitParameter("driver");
	String username = context.getInitParameter("username");
	System.out.println("driver==========" + driver);
	System.out.println("username==========" + username);

	// 3.获得a b c d.txt的绝对路径
	// a.txt
	String realPath_A = context.getRealPath("a.txt");
	System.out.println(realPath_A);
	// b.txt
	String realPath_B = context.getRealPath("/WEB-INF/b.txt");
	System.out.println(realPath_B);
	// c.txt--我们写在src里的东西,会在tomcat中变成classes下面的东西
	String realPath_C = context.getRealPath("/WEB-INF/classes/c.txt");
	System.out.println(realPath_C);
	// d.txt--获取不到

	// 在读取src(classes)下的资源时可以用类加载器-------专门加载classes下的文件
	// getResource()参数是一个相对地址 相对classes
	String path = ContextServlet.class.getClassLoader().getResource("c.txt").getPath();
	System.out.println(path);
}

3.2.2 运行效果


3.3 ServletContext是一个域对象(重要 重要 重要)

什么是域对象?什么是域?

存储数据的区域就是域对象

ServletContext域对象的作用范围:整个web应用(所有的web资源都可以随意向servletcontext域中存取数据,数据可以共享)

上面这个所有的web资源,不光是servlet,还有jsp,也就是所有动态web资源(能取数据的)。


实例:

我们先往域对象中存入数据,再取出

3.3.1 存入

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1. 获得ServletContext对象
		ServletContext context = this.getServletContext();
		// 2. 获得初始化参数
		String driver = context.getInitParameter("driver");
		String username = context.getInitParameter("username");
		System.out.println("driver==========" + driver);
		System.out.println("username==========" + username);

		// 3.获得a b c d.txt的绝对路径
		// a.txt
		String realPath_A = context.getRealPath("a.txt");
		System.out.println(realPath_A);
		// b.txt
		String realPath_B = context.getRealPath("/WEB-INF/b.txt");
		System.out.println(realPath_B);
		// c.txt--我们写在src里的东西,会在tomcat中变成classes下面的东西
		String realPath_C = context.getRealPath("/WEB-INF/classes/c.txt");
		System.out.println(realPath_C);
		// d.txt--获取不到

		// 在读取src(classes)下的资源时可以用类加载器-------专门加载classes下的文件
		// getResource()参数是一个相对地址 相对classes
		String path = ContextServlet.class.getClassLoader().getResource("c.txt").getPath();
		System.out.println(path);

		// 4. 域对象----向servletContext中存数据
		context.setAttribute("name", "zhangsan");
	}

3.3.2 取出

package com.ken.context;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ContextServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 域对象----从servletContext中存数据
		String name = (String) this.getServletContext().getAttribute("name");
		System.out.println(name);
	}

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

3.3.3 运行效果

先访问第一个servlet,就会存入域对象。然后,访问第二个servlet,就可以从域对象中取出数据。


四、ServletContext的应用

统计登录用户的数量

4.1 在init()方法中创建变量count


4.2 获取、操作、保存


4.3 运行效果



域对象的通用的方法:

setAtrribute(String name,Object obj);

getAttribute(String name);

removeAttribute(String name);

源码下载


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ServletContext是Java Web中的一个重要接口,它代表了Web应用程序在服务器中的上下文环境。在一个Web应用程序中,每个Servlet都可以访问同一个ServletContext对象,从而实现Servlet之间的数据共享和通信。 在Java中,可以使用ServletConfig对象的getServletContext()方法来获取ServletContext对象,然后使用该对象的方法来实现一系列操作,例如: 1. 获取Web应用程序的初始化参数:可以使用ServletContext对象的getInitParameter()方法来获取Web应用程序的初始化参数,例如数据库连接等配置信息。 ```java String username = context.getInitParameter("username"); ``` 2. 获取Web应用程序的真实路径:可以使用ServletContext对象的getRealPath()方法来获取Web应用程序的真实路径,例如获取Web应用程序中的一个文件的绝对路径。 ```java String path = context.getRealPath("/WEB-INF/config.properties"); ``` 3. 在ServletContext中保存数据:可以使用ServletContext对象的setAttribute()方法来在ServletContext中保存数据,从而实现Servlet之间的数据共享。 ```java context.setAttribute("key", value); ``` 4. 从ServletContext中获取数据:可以使用ServletContext对象的getAttribute()方法来从ServletContext中获取数据,例如获取其他Servlet保存的数据。 ```java Object value = context.getAttribute("key"); ``` 需要注意的是,ServletContext对象的作用域为整个Web应用程序,因此需要注意数据的安全性和可靠性。另外,ServletContext对象是在Web应用程序启动时创建的,因此可以在Servlet的init()方法中获取ServletContext对象并进行初始化操作,例如读取配置文件等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值