ServletContext接口(二)实现多个Servlet对象共享数据

2. 实现多个Servlet对象共享数据

ServletContext接口的方法

方法说明功能描述
Enumeration getAttributeNames()返回一个Enumeration对象,该对象包含了所有存放在ServletContext中的所有域属性名
Object getAttibute(String name)根据参数指定的属性名返回一个与之匹配的域属性值
void removeAttribute(String name)根据指定的域属性名,从ServletContext中删除匹配的域属性
void setAttribute(String name,Object obj)设置ServletContext的域属性其中name是域属性名,obj是域属性值

创建Servlet类TestServlet04和TestServlet05

TestServlet04:

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext context=this.getServletContext();
		//通过setAttribute()方法设置属性值
		context.setAttribute("data", "this servlet save data");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}
	//浏览器测试网址 http://localhost:8080/chapter03/TestServlet04
}

TestServlet05:

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out=response.getWriter();
		ServletContext context=this.getServletContext();
		//通过getAttribute()方法获取属性值
		String data=(String) context.getAttribute("data");
		out.println(data);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}
	//浏览器测试网址 http://localhost:8080/chapter03/TestServlet05
}

运行结果:

先运行TestServlet04将数据存入ServletContext对象,再运行TestServlet05,得到如下结果:
在这里插入图片描述

Servlet数据共享补充

/*
		 * WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
		 * ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.
		 * getServletContext方法获得ServletContext对象。
		 * 由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,
		 * 因此Servlet对象之间可以通过ServletContext对象来实现通讯。 ServletContext对象通常也被称之为context域对象。
		 * 
		 * Servlet1: 
		 * protected void doGet(HttpServletRequest request,
		 * HttpServletResponse response) throws ServletException, IOException { 
		 * String data = "value"; 
		 * ServletContext context = this.getServletConfig().getServletContext();//获得ServletContext对象
		 * context.setAttribute("data", data); //将data存储到ServletContext对象中 }
		 * 
		 * Servlet2:
		 * protected void doGet(HttpServletRequest request, HttpServletResponse
		 * response) throws ServletException, IOException { 
		 * ServletContext context = this.getServletContext(); String data = (String)
		 * context.getAttribute("data");//从ServletContext对象中取出数据
		 * response.getWriter().print("data="+data); }
		 * Servlet2拿到的context与Servlet1设置data的context是同一个对象,所以说servlet中this.
		 * getServletContext(); this.getServletConfig().getServletContext(); 本质上没有区别。
		 */
  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心醉瑶瑾前

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值