What is ServletContext:
- ServletContext is an interface, it represents the Servlet context object
- A Web project has only one ServletContext object instance
- Servlet object is a domain object
- ServletContext is created when the web project deployment starts, and destroyed when the web project stops
Domain object: an object that can access data like a Map, called a domain object
The domain here refers to the scope of operation for accessing data (the entire Web project)
Save data | Fetch data | delete data | |
---|---|---|---|
Map | put() | get() | remove() |
Domain object | setAttribute() | getAttribute() | removeAttribute() |
The four functions of the ServletContext class:
- Get the context parameter context-param configured in web.xml
- Get the current working path, format: /working path
- Obtain the absolute path on the server hard disk after the project is deployed
- Access data like Map
Get the context parameter context-param configured in web.xml:
First configure web.xml and add the following content to it:
<!-- Context-param是上下文参数(它属于整个web工程,只要是在这个web工程中的,像Servlet 程序、Filter 过滤器、Listener 监听器,都可以得到这些参数) -->
<!-- context-param根据需要,可以配置多组 -->
<context-param>
<param-name>username</param-name>
<param-value>context</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>root</param-value>
</context-param>
The doGet method in ContextServlet.java:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 获取web.xml中配置的上下文参数context-param
ServletContext context = getServletConfig().getServletContext(); //首先,通过ServletConfig获取到ServletContext对象
String username = context.getInitParameter("username");
System.out.println("context-param参数的username的值是:"+username);
String password = context.getInitParameter("password");
System.out.println("context-param参数的password的值是:"+password);
}
Then start, visit:
http://localhost:8080/06_servlet1_war_exploded/contextServlet
operation result:
Note:
By using this method, you cannot get the content in init-param,
init-param can only be obtained by ServletConfig,
The context-param can only be obtained by the ServletContext object.
Get the current working path, format:/working path:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 2. 获取当前的工作路径,格式:/工作路径
System.out.println("当前工程路径:" + context.getContextPath());
}
operation result:
The result is:
Obtain the absolute path on the server hard disk after the project is deployed:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 3. 获取工程部署后在服务器硬盘上的绝对路径
/*
/ 斜杠被服务器解析地址为:http://ip:port/工程名/
*/
System.out.println("工程部署的路径是:" + context.getRealPath("/"));
}
operation result:
Access this path: (This corresponds to the web directory in our module, which will be mentioned below)
Let's restart:
Get a path:
C:\Users\Point\.IntelliJIdea2019.3\system\tomcat\Tomcat_9_0_37_Javaweb
This is the copy content of Tomcat after IDEA integrates Tomcat.
Access this path:
You can find a 06_servlet1_war_exploded.xml configuration file:
The previous 06_servlet1_war_exploded is the project path of the project we wrote, corresponding to the web directory in the module we wrote (see above: Get the absolute path on the server hard disk after the project is deployed).
The web was copied as a project and changed its name to 06_servlet1_war_exploded in the past.
There will be some bytecodes compiled in src in the classes folder in WEB-INF.
in conclusion:
/ 斜杠被服务器解析地址为:http://ip:port/工程名/
This project name corresponds to C:\Users\Point\IdeaProjects\Javaweb\out\artifacts\06_servlet1_war_exploded\ on the disk
This location on the disk corresponds to the web directory in IDEA,
That is, it is mapped to the WEB directory in the IDEA code
From this, we can also get the absolute path of the file in the web directory:
Deploy the files and pictures:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("工程部署的路径是:" + context.getRealPath("/"));
System.out.println("工程下css目录的绝对路径是:" + context.getRealPath("/css"));
System.out.println("工程下imgs目录的1.jpg绝对路径是:" + context.getRealPath("/css/1.jpg"));
}
Access data like Map:
Here is a simpler way to call the ServletContext object:
Call the getServletContext method directly:
ServletContext context = getServletContext();
The getServletContext method is created in GenericServlet:
The ServletContext that calls the ServletConfig object first and then calls the GenericServlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取ServletContext对象方法一:getServletConfig().getServletContext();
//获取ServletContext对象方法二:直接调用getServletContext()
ServletContext context = getServletContext();
System.out.println("保存之前:ServletContext1 中获取数据域key1的值是:" + context.getAttribute("key1"));
context.setAttribute("key1","value1");
System.out.println("ServletContext1 中获取数据域key1的值是:" + context.getAttribute("key1"));
}
Create a ServletContext2:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("ServletContext2 中获取数据域key1的值是:" + context.getAttribute("key1"));
}
- Visit contextServlet1 first, then contextServlet1 once:
-
Redeploy (Redeploy) and visit contextServlet1 again:
-
Restart the server (Restart server) and visit contextServlet1 again:
- Access contextServlet2:
visible:
-
ServletContext is created when the web project deployment starts, and destroyed when the web project stops !
-
The scope of operation for accessing data (the entire Web project)
Deployment (Redeploy) :
It will stop the original project first, and then start the whole project, that is, the server does not need to be restarted, the project is restarted.
If it is to restart the server (Restart server):
The original ServletContext data is no longer there, and it needs to be ServletContext context = getServletContext()
obtained first before the previous 保存之前:ServletContext1 中获取数据域key1的值
data can be obtained .
A Web project has only one ServletContext object instance test:
When accessing contextServlet1 and contextServlet2, the addresses of context objects obtained are the same: