一:ServletContext概述 服务器会为每个web项目创建一个ServletContext对象: 1. ServletContext对象的创建是在服务器启动时完成的; 2. ServletContext对象的销毁是在服务器关闭时完成的。 ServletContext对象的作用是在整个Web应用的动态资源之间共享数据!例如在AServlet中向ServletContext对象中保存一个值,然后在BServlet中就可以获取这个值,这就是共享数据了。 二:获取ServletContext 1. ServletConfig#getServletContext(); 2. GenericServlet#getServletContext(); 3. HttpSession#getServletContext() 4. ServletContextEvent#getServletContext() 5. HttpServlet#getServletContext(); 在Servlet中获取ServletContext对象: 1. 在void init(ServletConfig config)中:ServletContext context = config.getServletContext();,ServletConfig类的getServletContext()方法可以用来获取ServletContext对象; 在GenericeServlet或HttpServlet中获取ServletContext对象: 2. GenericServlet类有getServletContext()方法,所以可以直接使用this.getServletContext()来获取; 三:域对象的功能 ServletContext是JavaWeb四大域对象之一: 1. PageContext; 2. ServletRequest; 3. HttpSession; 4. ServletContext; 其中ServletContext是最大的域对象。 域对象
ServletContext的基本操作
最新推荐文章于 2025-07-09 15:45:22 发布