Servlet technology 10_ServletContext class

What is ServletContext:

  1. ServletContext is an interface, it represents the Servlet context object
  2. A Web project has only one ServletContext object instance
  3. Servlet object is a domain object
  4. 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:

  1. Get the context parameter context-param configured in web.xml
  2. Get the current working path, format: /working path
  3. Obtain the absolute path on the server hard disk after the project is deployed
  4. 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:
Insert picture description here

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:

Insert picture description here

The result is:

Insert picture description here

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:

Insert picture description here

Access this path: (This corresponds to the web directory in our module, which will be mentioned below)

Insert picture description here

Let's restart:
Insert picture description here

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:
Insert picture description here

You can find a 06_servlet1_war_exploded.xml configuration file:
Insert picture description here

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:

Insert picture description here

    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"));
    }

Insert picture description here

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:

Insert picture description here

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"));
    }

Insert picture description here

Create a ServletContext2:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
    ServletContext context = getServletContext();
    System.out.println("ServletContext2 中获取数据域key1的值是:" + context.getAttribute("key1"));
}
  1. Visit contextServlet1 first, then contextServlet1 once:

Insert picture description here

  1. Redeploy (Redeploy) and visit contextServlet1 again:
    Insert picture description here

  2. Restart the server (Restart server) and visit contextServlet1 again:

Insert picture description here

  1. Access contextServlet2:
    Insert picture description here

visible:

  1. ServletContext is created when the web project deployment starts, and destroyed when the web project stops !

  2. 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.
Insert picture description here

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:

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45024585/article/details/108859154