1、ServletContext每个工程对应一个
2、获得ServletContext对象 ----- >servletConfig.getServletContext();
或者this.getServletContext();
全局初始化参数配置 (所有Servlet都可以访问)
* 和ServletConfig配置初始化参数 区分开
1) 配置全局初始化参数
当前web应用所有的组件(servlet & jsp) 都可以访问
在容器启动的时候 初始化参数就有了...
2) 获得ServletContext对象 ServletContext context =getServletContext();
3) 通过 StringgetInitParameter(String name) 获得全局参数信息
String encode = this.getServletConfig().getInitParameter("encode");
String contextPath = this.getServletContext().getContextPath(); //--------->/writeWebCode01
System.out.println(contextPath);
String url = this.getServletContext().getInitParameter("url");//------>jdbc:mysql//localhost:8080/manager
System.out.println(url);
String realPath = this.getServletContext().getRealPath("web.xml");//WEB-INF下的文件路径通过该方法获得
System.out.println(realPath);//磁盘中实际的路径
String mimeType = this.getServletContext().getMimeType(".html");//注意 点不能忘掉
System.out.println(mimeType);// text/html