特点
- 在一个web项目当中,有且仅有这一个
servletcontext
对象 - 生命周期。当服务器启动初始化该对象直到服务器关闭自动销毁
获取方式
- 通过
this
对象获取
ServletContext servletContext = this.getServletContext();
- 通过
HttpServletRequest
对象获取
ServletContext servletContext1 = req.getServletContext();
- 通过
session
对象获取
HttpSession session = req.getSession();
ServletContext servletContext2 = session.getServletContext();
例子
package servletcontext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(value = "/a")
public class GetServletContext extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1.通过this对象获取
ServletContext servletContext = this.getServletContext();
// 2.通过HttpServletRequest对象获取
ServletContext servletContext1 = req.getServletContext();
// 3.通过session对象获取
HttpSession session = req.getSession();
ServletContext servletContext2 = session.getServletContext();
// 通过打印。得出都是用的同一个对象
System.out.println("servletContext:"+servletContext);
System.out.println("servletContext1:"+servletContext1);
System.out.println("servletContext2:"+servletContext2);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
运行结果
servletContext:org.apache.catalina.core.ApplicationContextFacade@54a22e00
servletContext1:org.apache.catalina.core.ApplicationContextFacade@54a22e00
servletContext2:org.apache.catalina.core.ApplicationContextFacade@54a22e00
常用方法
返回值 | 函数名 | 说明 |
---|---|---|
String | getRealPath("/") | 获取项目真实路径 |
String | getContextPath() | 获取项目上下文路径 |
void | setAttribute(String jian,String zhi) | 设置一个键值对进行存储 |
Object | getAttribute(String jian) | 通过键获取存储的值 |
void | removeAttribute(String jian) | 移除该键 |
样例
package servletcontext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(value = "/b")
public class ServletContextMethod extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
// 1.获取项目真实路径
String path1 = servletContext.getRealPath("/");
// 2.获取项目上下文路径
String path2 = servletContext.getContextPath();
System.out.println(path1);
System.out.println(path2);
// 3.设置全局容器
servletContext.setAttribute("username","xiaoming");
String name = (String)servletContext.getAttribute("username");
servletContext.removeAttribute("username");
String name1 = (String)servletContext.getAttribute("username");
System.out.println("没移除之前username:"+name);
if(name1 == null){
System.out.println("该键已被移除");
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
运行结果
D:\IDEA\workspace\MyServlet\out\artifacts\MyServlet_war_exploded
/MyServlet_war_exploded
没移除之前username:xiaoming
该键已被移除
应用场景
通过以上的介绍,servletcontext对象可用于当计数器,设置该项目被访问的次数
作用域总结
- Cookie数据
一次请求 - Session数据
一次会话 - ServletContext数据
整个web项目过程,直到服务器关闭