当前位置:网站首页>ServletContext object

ServletContext object

2022-06-12 10:12:00 A man who is rubbed on the ground by math every day

ServletContext object

every last web There is only one application ServleContext object , also called Application object , From the name we can see , The object is application specific , stay web When the container starts , Will be for each one web The application talks about a corresponding ServletContext object .

This object has two main functions ,

First of all 、 As domain objects to share data , At this point, the data is shared throughout the application .

second 、 The object saves information about the current application . For example, through getServerInfo() Method to get the current server information ,getRealPath(String path) Get the real path of resources and so on .

ServletContext Object acquisition

obtain ServletContext There are many ways for objects . such as :

  1. adopt request Object acquisition

    ServletContext servletContext = request.getServletContext();
    
  2. adopt session Object acquisition

    ServletContext servletContext = request.getSession().getServletContext(); 
    
  3. adopt servletConfig Object acquisition , stay Servlet The standard provides ServletConfig Method

    ServletConfig servletConfig = getServletConfig();
    ServletContext servletContext = servletConfig.getServletContext();
    
  4. Direct access to ,Servlet Class provides direct access to ServletContext Object method

ServletContext servletContext = getServletContext();

Common methods

//  Get the real path of the project 
String realPath = request.getServletContext().getRealPath("/");
//  Get the version information of the current server 
String serverInfo = request.getServletContext().getServerInfo();

ServletContext Domain object ( Not recommended )

ServletContext It can also be used as a domain object , Through to the ServletContext Data access in , You can make the entire application share certain numbers According to the . Of course, it is not recommended to store too much data , because ServletContext Once the data is stored in and not removed manually, it will always be saved .

//  obtain ServletContext object 
ServletContext servletContext = request.getServletContext();
//  Set the domain object 
servletContext.setAttribute("name","zhangsan");
//  Get the domain object 
String name = (String) servletContext.getAttribute("name");
//  Remove domain objects 
servletContext.removeAttribute("name");

Servlet Three domain objects

The larger the scope of the domain object , The more memory you use

  1. request The domain object is valid in one request . Request forwarding is valid , Redirection failed .
  2. session Domain objects are valid in one session . Request forwarding and redirection are valid ,session It is invalid after being destroyed .
  3. servletContext Domain objects are valid throughout the application . After the server is shut down .
原网站

版权声明
本文为[A man who is rubbed on the ground by math every day]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010527416519.html