JavaWeb

一、ServletContext
	ServletContext可以实现多个Servlet的共享数据,因为一个web应用中所有Servlet共享一个ServletContext
	对象.
二、ServletContext所含的部分方法
void removeAttribute(String name)
	从此ServletContext中删除具有给定名称的属性。 删除后,后续调用getAttribute(java.lang.String)
	来检索属性的值将返回null。
void setAttribute(String name,Object object)
	在此ServletContext中将对象绑定到给定的属性名称。 如果指定的名称已用于某个属性,则此方法将
	用新属性替换旧属性。
Enumeration<String> getAttributeNames()
  返回包含此ServletContext中可用的属性名称的Enumeration。可以使用getAttribute(java.lang.String) 
  方法获取指定属性的值。
列子:
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws 
	ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.write("This is doGet!");
		ServletContext sc = this.getServletContext();
		sc.setAttribute("data", "this is a data");
	}

	<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
	<%
		ServletContext sc = this.getServletContext();
		String str = (String) sc.getAttribute("data");
	%>
	
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
	<html>
	  <head>
	  
	  </head>
	  
	  <body>
	    <h1>My Web</h1>
	    <br/>
	    <h2><%=str %></h2>
	  </body>
	</html>

おすすめ

転載: blog.csdn.net/ZWHSOUL/article/details/84037889