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");
}