Servlet-ServletContext
- ServletContext对象又称呼为上下文对象,或者叫做应用域对象
- 容器会为每一个app创建一个独立的唯一的ServletContext对象
- ServletContext对象为所有的Servlet所共享
- ServletContext可以为所有的Servlet提供初始配置参数
ServletContext是单例模式
1 2 3 4 5 6 7
| public abstract class GenericServlet implements Servlet, ServletConfig, Serializable { private transient ServletConfig config;
public ServletContext getServletContext() { return this.getServletConfig().getServletContext(); } }
|
配置初始参数
与ServletConfig不同,ServletContext的标签不写在Servlet中,而是写在根标签下
这些初始参数由Tomcat读取并创建一个Context对象,这个对象作用于每一个Servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" version="6.0"> <context-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </context-param> <context-param> <param-name>username</param-name> <param-value>xiaobai</param-value> </context-param> <servlet> <servlet-name>ServletConfig</servlet-name> <servlet-class>com.xiaobai.servlet.ServletConfigTest</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletConfig</servlet-name> <url-pattern>/ServletConfig</url-pattern> </servlet-mapping> </web-app>
|
获取初始参数
Context对象可由Config对象调用getServletContext()方法得到
也可由req对象调用getServletContext()方法得到
1 2 3 4 5 6 7 8
| public class ServletContextTest extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext servletContext = getServletContext(); ServletContext servletContext1 = getServletConfig().getServletContext(); ServletContext servletContext2 = req.getServletContext(); } }
|
虽然在这里我们创建了三个servletContext对象,但其实都指向了一个内存地址
ServletContext的方法
- getInitParameter(“keyA”):根据key获取value
- getInitParameterNames():返回Enumeration<>类型的对象,此对象中有所有的初始参数的key
Enumeration<>的方法
- hasMoreElements():判断有没有下一个参数,如果有返回true,没有则返回false、
- nextElement():取出当前元素,向下移动游标
不难看出,Context的使用方法和Config一样。但不同的是,无论哪一个servlet来获取Context的初始参数都是一样的
获取磁盘路经*
操作初始参数只是Context的用法之一
ServletContext的方法
- getRealPath():获取一个指向项目部署位置下的某个文件/目录的磁盘真实路径
1 2 3 4 5 6 7 8 9 10
| public class ServletContextTest extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext servletContext = getServletContext();
String path = servletContext.getRealPath("upload"); System.out.println(path);
} }
|
获得上下文路径*
后续学习在项目中使用相对和绝对路经寻找目标资源
在使用绝对路经寻找目标资源时,就需要动态获取上下文路径
ServletContext的方法:
- getContextPath():获取当前项目上下文路径(访问路径)
1 2 3 4 5 6 7 8 9 10
| public class ServletContextTest extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext servletContext = getServletContext();
String contextPath = servletContext.getContextPath(); System.out.println(contextPath);
} }
|