ServletContext对象

ServletContext:只有一个对象

获取ServletContext对象的两种方法:

1.通过ServletConfig配置对象获取ServletContext对象

ServletConfig config=getServletConfig()

ServletContext context=config.getServletContext()

2.直接获取

ServletContext context=getServletContext()

获取绝对路径:

String path=context.getRealPath("WEB-INF/a.txt")

String path=context.getRealPath("a.txt")

ServletContext域对象功能:

ServletContext.setAttribute("key",Object)  //覆盖存值

ServletContext.getAttribute("key") //取值

ServletContext.removeAttribute("key") //删除

posted on 2019-11-04 15:37  流光若影  阅读(148)  评论(0编辑  收藏  举报