代表整个web应用,可以和程序的容器进行通信
@WebServlet("/servletContextDemo1")
public class ServletContextDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通过request直接获取
ServletContext sc1=request.getServletContext();
//通过HttpServlet获取
ServletContext sc2=this.getServletContext();
System.out.println(sc1==sc2); //true
System.out.println(sc1);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}在互联网通信过程中定义的一种文件数据类型,HTTP协议也遵循这种数据类型
大类型/小类型,例如:
所有用户所有请求的数据
这里获取的真实路径是指在程序运行在Tomcat服务器上时的文件路径,而不是指我们当前工作空间的真实路径
我们传入的路径是以web文件夹为基准的相对路径,这里需要注意,如果要获取与web文件夹同级的src文件夹中的文件,有两种方式,一种是采用类加载器的方式,局限性较大,另一种方式是当程序运行在Tomcat服务器上时,src中的内容会被放置在web文件夹下的WEB-INF文件夹中的classes文件夹中,可以通过这种相对关系来获取