ServletContext全局唯一,它是属于整个Web应用程序的,我们可以通过getServletContext()来获取到此对象。

此对象也能设置附加值:

ServletContext context = getServletContext();
context.setAttribute("test", "我是重定向之前的数据");
resp.sendRedirect("time");
System.out.println(getServletContext().getAttribute("test"));

因此我们可以在各个请求之间共享设定的属性。

还可以进行请求转发和获取 webapp 根目录下的资源。

context.getRequestDispatcher("/time").forward(req, resp);