ServletContext,ServletContext资源共享

ServletContext作用
1.获取全局初始化参数
2.资源共享(servlet通信)
3.获取资源文件
在这里插入图片描述

ServletContext资源共享

	 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//创建对象
		ServletContext sc = this.getServletContext();
		//往里写内容
		sc.setAttribute("first", "1111");
		
	}




	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//创建对象
		ServletContext sc = this.getServletContext();
		//从context中获取
		Object attribute = sc.getAttribute("first");
		System.out.println(attribute);
		
	}




	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//创建对象
		ServletContext sc = getServletContext();
		//删除context中的内容
		sc.removeAttribute("first");
	}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44063001/article/details/88110562